-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-form.ts
More file actions
76 lines (68 loc) · 1.87 KB
/
input-form.ts
File metadata and controls
76 lines (68 loc) · 1.87 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {render, sanitize} from "../../../index.js"
import {setState, state} from "../app.js"
import {IMAGES} from "../config/images.js"
import {handleError} from "../services/error.js"
import {fetchAndNormalizeImages} from "../services/images.js"
import {resetInput} from "../services/input-form.js"
import {patchTask, prepareTask} from "../services/tasks.js"
import type {EventHandler} from "../../../purity.js"
const inputFormStyle = () => render`
<style id="task-form-style">
form#task-form {
width: 100%;
max-width: 100%;
height: 3rem;
min-height: 3rem;
}
form#task-form input {
width: 100%;
height: 100%;
background-color: var(--input-bg);
color: var(--input-color);
border: var(--input-border);
border-radius: 0;
}
form#task-form input:focus {
outline: var(--input-focus-outline);
outline-offset: -2px;
}
</style>
`
const createTask: EventHandler = async e => {
e.preventDefault()
const description: string = sanitize(e.target.task.value)
const task = prepareTask(description)
if (state.tasks.some(({id}) => id === task.id)) {
window.alert("There is already a task with the same id in the list")
return
}
resetInput()
setState(({tasks}) => ({
view: "active",
tasks: [{...task, isImageLoading: true}, ...tasks],
}))
try {
const image = await fetchAndNormalizeImages(task)
patchTask({...task, image})
} catch (err) {
handleError(err)
patchTask({
...task,
image: {link: IMAGES.UNDEFINED_TASK, queries: {}},
})
}
}
export const inputForm = (): string => render`
<form id="task-form" ::submit=${createTask}>
<input
name="task"
::input=${e => {
setState(() => ({input: e.target.value}))
}}
value=""
placeholder="Task description"
autocomplete="off"
/>
</form>
${inputFormStyle()}
`