-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdays.add.tsx
More file actions
111 lines (92 loc) · 2.76 KB
/
Copy pathdays.add.tsx
File metadata and controls
111 lines (92 loc) · 2.76 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
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
import {
redirect,
type ActionFunctionArgs,
type LoaderFunctionArgs
} from '@remix-run/node'
import {useNavigate, useLoaderData} from '@remix-run/react'
import {invariant} from '@arcath/utils'
import {getPrisma} from '~/lib/prisma.server'
import {checkSession} from '~/lib/session'
import {Page, FormElement, Actions} from '~/lib/ui'
import {INPUT_CLASSES} from '~/lib/utils'
export const loader = async ({request}: LoaderFunctionArgs) => {
const result = await checkSession(request)
if (!result) {
return redirect('/login')
}
const prisma = getPrisma()
const days = await prisma.dayType.findMany({orderBy: {name: 'asc'}})
return {days}
}
export const action = async ({request}: ActionFunctionArgs) => {
const result = await checkSession(request)
if (!result) {
return redirect('/login')
}
const prisma = getPrisma()
const formData = await request.formData()
const name = formData.get('name') as string | undefined
const copyFrom = formData.get('copyFrom') as string | undefined
invariant(name)
invariant(copyFrom)
const dayType = await prisma.dayType.create({data: {name}})
if (copyFrom !== '-') {
const schedules = await prisma.schedule.findMany({
where: {dayTypeId: copyFrom === '_' ? undefined : copyFrom}
})
await prisma.schedule.createMany({
data: schedules.map(({time, weekDays, zoneId, audioId}) => {
return {dayTypeId: dayType.id, time, weekDays, zoneId, audioId}
})
})
}
return redirect(`/calendar`)
}
const AddDay = () => {
const navigate = useNavigate()
const {days} = useLoaderData<typeof loader>()
return (
<Page title="Add Day">
<form method="post">
<FormElement label="Name" helperText="Descriptive name for the day.">
<input name="name" className={INPUT_CLASSES} />
</FormElement>
<FormElement
label="Copy From"
helperText="The day type to copy the schedule from"
>
<select name="copyFrom" className={INPUT_CLASSES}>
<option value="-" selected>
None
</option>
<option value="_">Default</option>
{days.map(({id, name}) => {
return (
<option key={id} value={id}>
{name}
</option>
)
})}
</select>
</FormElement>
<Actions
actions={[
{
label: 'Cancel',
color: 'bg-stone-200',
onClick: e => {
e.preventDefault()
navigate('/calendar')
}
},
{
label: 'Add Day',
color: 'bg-green-300'
}
]}
/>
</form>
</Page>
)
}
export default AddDay