Skip to content

Commit abd414b

Browse files
committed
Rename modelRepositories/Factory to Config
1 parent 5256954 commit abd414b

File tree

4 files changed

+82
-150
lines changed

4 files changed

+82
-150
lines changed

src/jsMain/kotlin/todo/Config.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package todo
2+
3+
import component.FileBackupComponent
4+
import component.UndoComponent
5+
import platform.handleError
6+
import component.firebase.*
7+
import component.repository.LocalStorageRepository
8+
import io.ktor.client.HttpClient
9+
import io.ktor.client.request.get
10+
import kotlinx.coroutines.GlobalScope
11+
import kotlinx.coroutines.launch
12+
import org.w3c.dom.get
13+
import firebase.app.User
14+
import firebase.app.initializeApp
15+
import todo.model.ToDo
16+
import todo.model.ToDoJS
17+
import todo.model.toNormal
18+
import kotlin.browser.localStorage
19+
import kotlin.js.json
20+
21+
object Config {
22+
// To use firebase, add your app at https://console.firebase.google.com/
23+
private val firebaseConfig = json(
24+
"apiKey" to "ZZZFirebaseApiKeyZZZ",
25+
"authDomain" to "ZZZAppIdZZZ.firebaseapp.com",
26+
"databaseURL" to "https://ZZZAppIdZZZ.firebaseio.com",
27+
"projectId" to "ZZZAppIdZZZ",
28+
"storageBucket" to "",
29+
"messagingSenderId" to "ZZZFirebaseMessagingSenderIdZZZ"
30+
)
31+
private val firebaseApp = if ((firebaseConfig["authDomain"] as String?)?.contains("ZZZ") != false) {
32+
null
33+
} else {
34+
initializeApp(firebaseConfig)
35+
}
36+
var user: User? = null
37+
38+
val toDoRepository = LocalStorageRepository<ToDo, ToDoJS>("toDos", "toDos") { it.toNormal() }
39+
private val allRepositories = listOf(toDoRepository)
40+
val fileBackupComponent = FileBackupComponent(appNameForFilesystem, allRepositories = allRepositories)
41+
42+
init {
43+
if (toDoRepository.localStorageKeys.all { localStorage[it] == null }) {
44+
GlobalScope.launch {
45+
val client = HttpClient()
46+
val initialData = client.get<String>("initial-data.json")
47+
fileBackupComponent.initializeData(initialData)
48+
}
49+
}
50+
51+
UndoComponent.watch(toDoRepository)
52+
firebaseApp?.auth()?.useDeviceLanguage()
53+
firebaseApp?.auth()?.onUserChanged { _, newUser ->
54+
if (newUser == null) {
55+
println("newUser == null so calling signInAnonymously()")
56+
firebaseApp.auth().signInAnonymously().catch { handleError(it) }
57+
}
58+
val providerData = newUser?.providerData?.joinToString(";", "[", "]")
59+
console.log("uid=${newUser?.uid} isAnonymous=${newUser?.isAnonymous} displayName=${newUser?.displayName} email=${newUser?.email} photoURL=${newUser?.anyPhotoURL} $providerData")
60+
}
61+
}
62+
}

src/jsMain/kotlin/todo/ToDoScreen.kt

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package todo
22

33
import bootstrap.*
44
import component.ButtonBar
5-
import platform.toDate
5+
import component.bootstrap.textInput
66
import platform.toProviderDate
77
import react.*
88
import react.router.dom.RouteResultHistory
99
import component.repository.ID
1010
import platform.launchHandlingErrors
11-
import todo.model.Factory
11+
import platform.toJsDate
1212
import todo.model.ToDo
1313
import util.emptyToNull
1414
import kotlin.js.Date
@@ -33,18 +33,23 @@ interface ToDoState : RState {
3333
class ToDoScreen(props: ToDoProps) : RComponent<ToDoProps, ToDoState>(props) {
3434

3535
override fun ToDoState.init(props: ToDoProps) {
36-
original = props.id?.let { Factory.toDoRepository.find(it) }
36+
original = props.id?.let { Config.toDoRepository.find(it) }
3737
name = original?.name ?: ""
38-
dueDate = original?.dueDate?.toDate()
38+
dueDate = original?.dueDate?.toJsDate()
3939
notes = original?.notes ?: ""
4040
validated = false
4141
}
4242

4343
private fun save(event: React.SubmitEvent) {
4444
if (event.currentTarget?.checkValidity() == true && state.name.isNotBlank()) {
45-
val updatedToDo = ToDo(state.name, state.dueDate?.toProviderDate(), state.notes.emptyToNull(), id = state.original?.id)
45+
val updatedToDo = ToDo(
46+
name = state.name,
47+
dueDate = state.dueDate?.toProviderDate(),
48+
notes = state.notes.emptyToNull(),
49+
id = state.original?.id
50+
)
4651
launchHandlingErrors("save $updatedToDo") {
47-
Factory.toDoRepository.save(updatedToDo)
52+
Config.toDoRepository.save(updatedToDo)
4853
}
4954
props.history.goBack()
5055
}
@@ -60,7 +65,7 @@ class ToDoScreen(props: ToDoProps) : RComponent<ToDoProps, ToDoState>(props) {
6065
private fun delete() {
6166
state.original?.id?.let { toDoId ->
6267
launchHandlingErrors("delete $toDoId") {
63-
Factory.toDoRepository.remove(toDoId)
68+
Config.toDoRepository.remove(toDoId)
6469
}
6570
}
6671
props.history.goBack()
@@ -76,33 +81,11 @@ class ToDoScreen(props: ToDoProps) : RComponent<ToDoProps, ToDoState>(props) {
7681
attrs.onSubmit = { save(it); it.preventDefault() }
7782
attrs.validated = state.validated
7883
attrs.onSubmit = { event -> save(event); event.preventDefault() }
79-
child(Form.Group::class) {
80-
attrs.controlId = "name"
81-
child(Form.Label::class) { +"Description" }
82-
child(InputGroup::class) {
83-
textInputControl {
84-
attrs.value = state.name
85-
attrs.required = true
86-
attrs.placeholder = "(some description)"
87-
attrs.size = 40
88-
attrs.onAnyChange = { it.target?.let { setState { name = it.value } } }
89-
attrs.required = true
90-
}
91-
child(Form.Control.Feedback::class) {
92-
attrs.type = "invalid"
93-
+"Please Specify a Name"
94-
}
95-
}
84+
textInput("To Do", state.name, "(some description)", required = true) {
85+
setState { name = it ?: "" }
9686
}
97-
child(Form.Group::class) {
98-
attrs.controlId = "note"
99-
child(Form.Label::class) { +"Notes" }
100-
textInputControl {
101-
attrs.value = state.notes
102-
attrs.placeholder = "(some notes)"
103-
attrs.size = 60
104-
attrs.onAnyChange = { it.target?.let { setState { notes = it.value } } }
105-
}
87+
textInput("Notes", state.notes, "(some notes)") {
88+
setState { notes = it ?: "" }
10689
}
10790
child(Form.Group::class) {
10891
child(ButtonToolbar::class) {

src/jsMain/kotlin/todo/ToDosScreen.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import react.router.dom.routeLink
1111
import component.repository.Closeable
1212
import component.repository.onListChanged
1313
import platform.launchHandlingErrors
14-
import todo.model.Factory
1514
import todo.model.ToDo
1615

1716
interface ToDosProps : RProps {
@@ -31,19 +30,19 @@ class ToDosScreen(props: ToDosProps) : RComponent<ToDosProps, ToDosState>(props)
3130
private val resources = mutableListOf<Closeable>()
3231

3332
override fun ToDosState.init(props: ToDosProps) {
34-
list = Factory.toDoRepository.list()
33+
list = Config.toDoRepository.list()
3534
}
3635

3736
override fun componentDidMount() {
38-
resources.add(Factory.toDoRepository.onListChanged { _, newList -> setState { list = newList } })
37+
resources.add(Config.toDoRepository.onListChanged { _, newList -> setState { list = newList } })
3938
}
4039

4140
override fun componentWillUnmount() {
4241
resources.forEach { it.close() }
4342
}
4443

4544
private suspend fun delete(todo: ToDo) {
46-
Factory.toDoRepository.remove(todo)
45+
Config.toDoRepository.remove(todo)
4746
}
4847

4948
override fun RBuilder.render() {
@@ -104,7 +103,7 @@ class ToDosScreen(props: ToDosProps) : RComponent<ToDosProps, ToDosState>(props)
104103
child(Col::class) {
105104
attrs.xs = 14
106105
attrs.sm = 18
107-
backupButton()
106+
backupButton(Config.fileBackupComponent)
108107
routeLink(to = "/toDos/new") {
109108
child(Button::class) {
110109
+"Add ToDo"

src/jsMain/kotlin/todo/model/modelRepositories.kt

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)