-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2.bulk-update.tsx
87 lines (77 loc) · 2.56 KB
/
2.bulk-update.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
// deno-lint-ignore-file require-await no-explicit-any
import { HTMX, HTMXComponents, Fragment } from "../mod.tsx"
import { DummyDb } from "./dummydb.ts"
const { component, partial, serve, routes } = new HTMXComponents('@reggi/example-bulk-update')
const db = new DummyDb([
{ firstName: 'Thomas', lastName: 'Reggi', email: '[email protected]', active: true },
{ firstName: 'Rick', lastName: 'Dekkard', email: '[email protected]', active: true },
{ firstName: 'Joe', lastName: 'Smith', email: '[email protected]', active: true },
{ firstName: 'Angie', lastName: 'MacDowell', email: '[email protected]', active: true },
{ firstName: 'Fuqua', lastName: 'Tarkenton', email: '[email protected]', active: false },
]);
const updateActive = async (req: Request, active: boolean) => {
if (req.method === 'PUT') {
(await req.formData()).getAll('ids').forEach((id) => db.setValue(id, 'active', active))
}
}
const Activate = partial('/activate', async (_, ctx) => {
updateActive(ctx.request, true)
return <PeopleTableBody></PeopleTableBody>
})
const Deactivate = partial('/deactivate', async (_, ctx) => {
updateActive(ctx.request, false)
return <PeopleTableBody></PeopleTableBody>
})
const PeopleTableBody = partial('/people-table-body', async () => {
const people = await db.all()
return (
<Fragment>
{people.map((person: any) => (
<tr class="">
<td><input type='checkbox' name='ids' value={person.id}/></td>
<td>{person.firstName} {person.lastName}</td>
<td>{person.email}</td>
<td>{person.active ? 'Active' : 'Inactive'}</td>
</tr>
))}
</Fragment>
)
})
export const People = component('/people', async () => {
return (
<Fragment>
<form id="checked-contacts">
<table>
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Email</th>
<th>Status</th>
</tr>
</thead>
<tbody id="tbody">
<PeopleTableBody/>
</tbody>
</table>
</form>
<HTMX.div include="#checked-contacts" target="#tbody" swapInner>
<Activate.button.put class="btn">Activate</Activate.button.put>
<Deactivate.button.put class="btn">Deactivate</Deactivate.button.put>
</HTMX.div>
</Fragment>
)
})
export const nav = (
<Fragment>
<h1>Bulk Update Examples</h1>
<ul>
<li><People.anchor.href boost identifier='2'>People</People.anchor.href></li>
</ul>
</Fragment>
)
if (!Deno.env.get('NO_SERVE')) {
component('/', () => nav)
await serve()
}
export { routes }