|
| 1 | +import {getAuth} from '@clerk/remix/ssr.server' |
| 2 | +import { |
| 3 | + getFormProps, |
| 4 | + getInputProps, |
| 5 | + getTextareaProps, |
| 6 | + useForm, |
| 7 | +} from '@conform-to/react' |
| 8 | +import {getZodConstraint, parseWithZod} from '@conform-to/zod' |
| 9 | +import type {ActionFunctionArgs, LoaderFunctionArgs} from '@remix-run/node' |
| 10 | +import { |
| 11 | + Form, |
| 12 | + json, |
| 13 | + Link, |
| 14 | + redirect, |
| 15 | + useActionData, |
| 16 | + useLoaderData, |
| 17 | + useNavigation, |
| 18 | +} from '@remix-run/react' |
| 19 | +import {AlertCircle, Loader2} from 'lucide-react' |
| 20 | +import {z} from 'zod' |
| 21 | +import {ErrorList} from '~/components/error-list' |
| 22 | +import {TagsFilter} from '~/components/tags-filter' |
| 23 | +import {Alert, AlertDescription, AlertTitle} from '~/components/ui/alert' |
| 24 | +import {Button} from '~/components/ui/button' |
| 25 | +import {Input} from '~/components/ui/input' |
| 26 | +import {Label} from '~/components/ui/label' |
| 27 | +import {Textarea} from '~/components/ui/textarea' |
| 28 | +import {getTags} from '~/db/models/tags' |
| 29 | + |
| 30 | +export const schema = z.object({ |
| 31 | + tweetUrl: z |
| 32 | + .string({required_error: 'Tweet URL is required'}) |
| 33 | + .trim() |
| 34 | + .min(1, {message: 'Tweet URL is required'}), |
| 35 | + description: z.string().optional(), |
| 36 | +}) |
| 37 | + |
| 38 | +export async function loader(args: LoaderFunctionArgs) { |
| 39 | + const {userId} = await getAuth(args) |
| 40 | + |
| 41 | + if (!userId) { |
| 42 | + return redirect('/sign-in') |
| 43 | + } |
| 44 | + |
| 45 | + const tags = await getTags(userId) |
| 46 | + return {tags} |
| 47 | +} |
| 48 | + |
| 49 | +export async function action({request}: ActionFunctionArgs) { |
| 50 | + const formData = await request.formData() |
| 51 | + const result = parseWithZod(formData, {schema}) |
| 52 | + if (result.status !== 'success') { |
| 53 | + return json( |
| 54 | + { |
| 55 | + status: 'error', |
| 56 | + result: result.reply(), |
| 57 | + } as const, |
| 58 | + {status: result.status === 'error' ? 400 : 200}, |
| 59 | + ) |
| 60 | + } |
| 61 | + |
| 62 | + return redirect('/') |
| 63 | +} |
| 64 | + |
| 65 | +export default function Tweet() { |
| 66 | + const {tags} = useLoaderData<typeof loader>() |
| 67 | + const actionData = useActionData<typeof action>() |
| 68 | + const navigation = useNavigation() |
| 69 | + const submitting = navigation.state === 'submitting' |
| 70 | + const [form, fields] = useForm({ |
| 71 | + id: 'new-tweet', |
| 72 | + constraint: getZodConstraint(schema), |
| 73 | + lastResult: actionData?.result, |
| 74 | + onValidate({formData}) { |
| 75 | + return parseWithZod(formData, {schema}) |
| 76 | + }, |
| 77 | + }) |
| 78 | + |
| 79 | + return ( |
| 80 | + <Form method="POST" {...getFormProps(form)}> |
| 81 | + {form.errors?.length ? ( |
| 82 | + <div className="mb-4"> |
| 83 | + <Alert variant="destructive"> |
| 84 | + <AlertCircle className="h-4 w-4" /> |
| 85 | + <AlertTitle>Something went wrong</AlertTitle> |
| 86 | + <AlertDescription> |
| 87 | + <ErrorList id={form.errorId} errors={form.errors} /> |
| 88 | + </AlertDescription> |
| 89 | + </Alert> |
| 90 | + </div> |
| 91 | + ) : null} |
| 92 | + |
| 93 | + <TagsFilter tags={tags} /> |
| 94 | + |
| 95 | + <div className="mb-4 mt-4"> |
| 96 | + <Label htmlFor={fields.tweetUrl.id}>Tweet URL</Label> |
| 97 | + <Input {...getInputProps(fields.tweetUrl, {type: 'text'})} /> |
| 98 | + <ErrorList id={fields.tweetUrl.id} errors={fields.tweetUrl.errors} /> |
| 99 | + </div> |
| 100 | + <div className="mb-4"> |
| 101 | + <Label htmlFor={fields.description.id}>Description</Label> |
| 102 | + <Textarea {...getTextareaProps(fields.description)} /> |
| 103 | + <ErrorList |
| 104 | + id={fields.description.id} |
| 105 | + errors={fields.description.errors} |
| 106 | + /> |
| 107 | + </div> |
| 108 | + |
| 109 | + <div className="flex items-center justify-end gap-2"> |
| 110 | + <Button asChild variant="destructive"> |
| 111 | + <Link to="/">Cancel</Link> |
| 112 | + </Button> |
| 113 | + <Button type="submit" variant="default" disabled={submitting}> |
| 114 | + {submitting ? <Loader2 className="mr-2 animate-spin" /> : null} |
| 115 | + Save |
| 116 | + </Button> |
| 117 | + </div> |
| 118 | + </Form> |
| 119 | + ) |
| 120 | +} |
0 commit comments