-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c45363e
commit 053d51b
Showing
6 changed files
with
242 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use client'; | ||
|
||
import { ErrorMessage, Field, Form, Formik } from 'formik'; | ||
import { useRef } from 'react'; | ||
import * as Yup from 'yup'; | ||
import { createEvent } from '../../../../db/events'; | ||
|
||
const validator = Yup.object({ | ||
name: Yup.string() | ||
.required('Required') | ||
.min(5, 'Must be at least 5 characters') | ||
.max(50, 'Must be at most 50 characters') | ||
.matches( | ||
/^(\w|\d)+( (\w|\d)+)*$/, | ||
'Must only contain alphanumeric characters and spaces' | ||
), | ||
slug: Yup.string().matches( | ||
/^[a-z0-9]+(?:-[a-z0-9]+)*$/, | ||
'Must be pascal-case' | ||
), | ||
directusId: Yup.number().required('Required').min(0), | ||
startTime: Yup.date().required('Required'), | ||
endTime: Yup.date().required('Required'), | ||
staffingSlotSize: Yup.number().required('Required').min(15), | ||
}); | ||
|
||
export default function Page() { | ||
const slugInput = useRef<HTMLInputElement>(null); | ||
|
||
return ( | ||
<Formik | ||
initialValues={{ | ||
name: '', | ||
slug: '', | ||
directusId: 0, | ||
startTime: '', | ||
endTime: '', | ||
staffingSlotSize: 15, | ||
}} | ||
onSubmit={(v) => createEvent(v)} | ||
validationSchema={validator} | ||
validateOnBlur | ||
validateOnMount | ||
validateOnChange | ||
> | ||
<Form> | ||
<label htmlFor="name">Name:</label> | ||
<Field id="name" name="name" placeholder="My event" /> | ||
<ErrorMessage name="name" /> | ||
<br /> | ||
|
||
<label htmlFor="slug">Slug (leave empty for default):</label> | ||
<Field | ||
id="slug" | ||
name="slug" | ||
placeholder="my-event" | ||
innerRef={slugInput} | ||
/> | ||
<ErrorMessage name="slug" /> | ||
<br /> | ||
|
||
<label htmlFor="directusId">Directus id:</label> | ||
<Field id="directusId" name="directusId" type="number" /> | ||
<ErrorMessage name="directusId" /> | ||
<br /> | ||
|
||
<label htmlFor="startTime">Start:</label> | ||
<Field id="startTime" name="startTime" type="datetime-local" /> | ||
<ErrorMessage name="startTime" /> | ||
<br /> | ||
|
||
<label htmlFor="endTime">End:</label> | ||
<Field id="endTime" name="endTime" type="datetime-local" /> | ||
<ErrorMessage name="endTime" /> | ||
<br /> | ||
|
||
<label htmlFor="staffingSlotSize">Staffing slot size:</label> | ||
<Field id="staffingSlotSize" name="staffingSlotSize" type="number" /> | ||
<ErrorMessage name="staffingSlotSize" /> | ||
<br /> | ||
|
||
<button type="submit">Create</button> | ||
</Form> | ||
</Formik> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use server'; | ||
|
||
import prisma from '../db'; | ||
import { hasValidAdminSession } from '../session'; | ||
import { ApiError, ApiResult, Err, Ok } from '../utils'; | ||
|
||
function convertNameToSlug(name: string) { | ||
return name.replaceAll(/[_ ]/g, '-').toLocaleLowerCase(); | ||
} | ||
|
||
export async function createEvent(event: { | ||
name: string; | ||
slug: string; | ||
directusId: number; | ||
startTime: string; | ||
endTime: string; | ||
}): Promise<ApiResult<Event>> { | ||
if (!(await hasValidAdminSession())) { | ||
return Err(ApiError.Forbidden); | ||
} | ||
|
||
console.log(event.startTime, typeof event.startTime); | ||
|
||
const e: Event = await prisma.event.create({ | ||
data: { | ||
name: event.name, | ||
slug: event.slug || convertNameToSlug(event.name), | ||
directusId: event.directusId.toString(), | ||
mailTemplate: '', | ||
eventStartTime: new Date(event.startTime).toISOString(), | ||
eventEndTime: new Date(event.endTime).toISOString(), | ||
staffingTimeSlotSize: 30, | ||
data: {}, | ||
}, | ||
}); | ||
|
||
return Ok(e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters