forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateDiscussion.tsx
65 lines (59 loc) · 1.86 KB
/
CreateDiscussion.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
import { PlusIcon } from '@heroicons/react/outline';
import * as z from 'zod';
import { Button } from '@/components/Elements';
import { Form, FormDrawer, InputField, TextAreaField } from '@/components/Form';
import { Authorization, ROLES } from '@/lib/authorization';
import { CreateDiscussionDTO, useCreateDiscussion } from '../api/createDiscussion';
const schema = z.object({
title: z.string().min(1, 'Required'),
body: z.string().min(1, 'Required'),
});
export const CreateDiscussion = () => {
const createDiscussionMutation = useCreateDiscussion();
return (
<Authorization allowedRoles={[ROLES.is_admin, ROLES.is_mod]}>
<FormDrawer
isDone={createDiscussionMutation.isSuccess}
triggerButton={
<Button size="sm" startIcon={<PlusIcon className="h-4 w-4" />}>
Create Discussion
</Button>
}
title="Create Discussion"
submitButton={
<Button
form="create-discussion"
type="submit"
size="sm"
isLoading={createDiscussionMutation.isLoading}
>
Submit
</Button>
}
>
<Form<CreateDiscussionDTO['data'], typeof schema>
id="create-discussion"
onSubmit={async (values) => {
await createDiscussionMutation.mutateAsync({ data: values });
}}
schema={schema}
>
{({ register, formState }) => (
<>
<InputField
label="Title"
error={formState.errors['title']}
registration={register('title')}
/>
<TextAreaField
label="Body"
error={formState.errors['body']}
registration={register('body')}
/>
</>
)}
</Form>
</FormDrawer>
</Authorization>
);
};