-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathedit_book.tsx
185 lines (175 loc) · 5.97 KB
/
edit_book.tsx
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { Component, For, Suspense, createResource, createSignal } from 'solid-js';
import BaseModal from './base';
import { Book, bookIntoUpdateBook, Note, User } from '../../core/types';
import { createStore } from 'solid-js/store';
import { toSlug } from '../../core/helpers';
import { useApi } from '../../contexts/ApiProvider';
import { useNavigate } from '@solidjs/router';
import { apiErrorIntoToast, useToast } from '../../contexts/ToastProvider';
import { ApiError } from '../../core/api';
import Icon from '../icon';
import { LoadingSpin } from '../loading';
type DeletedNotesProps = {
bookId: string
restoreNote: (note: Note) => void
}
const DeletedNotes: Component<DeletedNotesProps> = (props) => {
const { api } = useApi()
const { pushToast } = useToast()
const [notes, { mutate }] = createResource(props.bookId, async (bookId) => {
let result = await api().getNotesByBookId(bookId, true)
if (result instanceof ApiError) {
pushToast(apiErrorIntoToast(result, "getting removed notes"))
return []
}
return result
})
const deleteNote = async (noteId: string, i: number) => {
let result = await api().deleteNote(noteId, true)
if (result instanceof ApiError) pushToast(apiErrorIntoToast(result, "deleting note"))
else {
let new_notes = notes()
if (new_notes) {
new_notes?.splice(i, 1)
mutate([...new_notes])
}
}
}
const restoreNote = async (noteId: string, i: number) => {
let result = await api().restoreNoteById(noteId)
if (result instanceof ApiError) pushToast(apiErrorIntoToast(result, "restoring note"))
else {
let new_notes = notes()
if (new_notes) {
props.restoreNote(new_notes[i])
new_notes?.splice(i, 1)
mutate([...new_notes])
}
}
}
return (
<div>
<h4 class="text-bold mb-2">Removed Notes</h4>
<ul class="max-h-40 h-40 overflow-y-auto bg-base-200">
<Suspense fallback={<LoadingSpin />}>
<For each={notes()}>
{(note, i) =>
<li class="flex items-center p-2 gap-2 rounded">
<span class="mr-auto">{note.name}</span>
<div class="join">
<button
onclick={() => restoreNote(note.id, i())}
type="button"
class="join-item btn btn-outline btn-sm"
>Restore</button>
<button
onclick={() => deleteNote(note.id, i())}
type="button"
class="join-item btn btn-outline btn-sm btn-error"
><Icon name="trash" /></button>
</div>
</li>
}
</For>
</Suspense>
</ul>
</div>
)
}
type UpdateBookModalProps = {
onClose: (book?: Book) => void
onDeleteClose: (bookId: string) => void
user: User
book: Book
restoreNote: (note: Note) => void
}
const UpdateBookModal: Component<UpdateBookModalProps> = (props) => {
const { api } = useApi()
const { pushToast } = useToast()
const navigate = useNavigate()
const [form, setForm] = createStore(bookIntoUpdateBook(props.book))
const [loading, setLoading] = createSignal(false)
const onSubmit = async (ev: Event) => {
ev.preventDefault()
setLoading(true)
let result = await api().updateBook(props.book.id, form)
setLoading(false)
if (result instanceof ApiError) pushToast(apiErrorIntoToast(result, "saving book"))
else {
navigate(`/${props.user.username}/${form.slug}`)
props.onClose({
...props.book,
name: form.name || props.book.name,
slug: form.slug || props.book.slug,
isPublic: form.isPublic || props.book.isPublic
})
}
}
const onDelete = async () => {
setLoading(true)
let result = await api().deleteBook(props.book.id)
setLoading(false)
if (result instanceof ApiError) pushToast(apiErrorIntoToast(result, "deleting book"))
else {
navigate(`/${props.user.username}`)
props.onDeleteClose(props.book.id)
}
}
return (
<BaseModal title="Update Book">
<form onsubmit={onSubmit}>
<label class="form-control">
<span class="label"><span class="label-text">Title</span></span>
<input
oninput={(ev) => setForm({
name: ev.currentTarget.value,
})}
value={form.name}
class="input input-bordered w-full"
type="text"
placeholder="e.g. My Amazing Book"
required
/>
</label>
<label class="form-control">
<span class="label"><span class="label-text">Slug</span></span>
<input
oninput={(ev) => setForm({
slug: toSlug(ev.currentTarget.value)
})}
value={form.slug}
class="input input-bordered input-sm w-full"
type="text"
placeholder="e.g. my-amazing-book"
pattern="(?:[a-z0-9]|-)+"
required
/>
</label>
<div class="form-control">
<label class="label cursor-pointer">
<span class="label-text">Public</span>
<input
onchange={() => setForm({ isPublic: !form.isPublic })}
checked={form.isPublic}
class="checkbox"
type="checkbox"
/>
</label>
</div>
<DeletedNotes bookId={props.book.id} restoreNote={props.restoreNote} />
<div class="modal-action">
<button onclick={onDelete} class="btn btn-outline btn-error" disabled={loading()} type="button">
<Icon name="trash" />
Delete
</button>
<button class="btn btn-primary" disabled={loading()} classList={{ loading: loading() }} type="submit">
<Icon name="save" />
Save
</button>
<button onclick={() => props.onClose()} class="btn" type="button">Cancel</button>
</div>
</form>
</BaseModal>
);
};
export default UpdateBookModal;