Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions app/routes/days.add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type ActionFunctionArgs,
type LoaderFunctionArgs
} from '@remix-run/node'
import {useNavigate} from '@remix-run/react'
import {useNavigate, useLoaderData} from '@remix-run/react'
import {invariant} from '@arcath/utils'

import {getPrisma} from '~/lib/prisma.server'
Expand All @@ -18,7 +18,11 @@ export const loader = async ({request}: LoaderFunctionArgs) => {
return redirect('/login')
}

return {}
const prisma = getPrisma()

const days = await prisma.dayType.findMany({orderBy: {name: 'asc'}})

return {days}
}

export const action = async ({request}: ActionFunctionArgs) => {
Expand All @@ -33,23 +37,56 @@ export const action = async ({request}: ActionFunctionArgs) => {
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}})

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={[
{
Expand Down