-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.ts
45 lines (41 loc) · 1.28 KB
/
settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {setState, state} from "../app.js"
import {handleError} from "./error.js"
import {put} from "./storage.js"
import {groomTasks} from "./tasks.js"
import {download, textFileReader} from "./text-file-manager.js"
import type {BaseTask} from "../app.js"
import type {EventHandler} from "../../../purity.js"
export const downloadUserData = async (): Promise<void> => {
try {
const fileName = `TODO-${new Date()
.toISOString()
.replaceAll(/[:.]/g, "_")}.backup.json`
download(fileName, JSON.stringify(groomTasks(state.tasks)))
window.alert("Downloading your backup file")
closeSettings()
} catch (err) {
handleError(err)
}
}
export const uploadUserData: EventHandler = async ({target: {files}}) => {
const [file] = files as FileList
try {
const text = await textFileReader(file)
const tasks: BaseTask[] = JSON.parse(text)
if (
window.confirm(
`Are you sure you want to replace current todo list in your storage (${state.tasks.length} items) with new one (${tasks.length} items)?`
)
) {
setState(() => ({tasks}))
put({tasks})
closeSettings()
}
} catch (err) {
handleError(err)
}
}
export const closeSettings = (): void =>
setState(() => ({isSettingsModalOpen: false}))
export const openSettings = (): void =>
setState(() => ({isSettingsModalOpen: true}))