({
id,
title,
description,
date,
}: EditReferenceProps)
| 30 | >; |
| 31 | |
| 32 | export default function EditReference({ |
| 33 | id, |
| 34 | title, |
| 35 | description, |
| 36 | date, |
| 37 | }: EditReferenceProps) { |
| 38 | const trpc = useTRPC(); |
| 39 | const { handleSubmit, register, control, formState, reset } = useForm<IForm>({ |
| 40 | resolver: zodResolver(validator), |
| 41 | defaultValues: { |
| 42 | id, |
| 43 | title: title ?? '', |
| 44 | description: description ?? undefined, |
| 45 | datetime: new Date(date).toISOString(), |
| 46 | }, |
| 47 | }); |
| 48 | const queryClient = useQueryClient(); |
| 49 | |
| 50 | const mutation = useMutation( |
| 51 | trpc.reference.update.mutationOptions({ |
| 52 | onSuccess() { |
| 53 | toast('Success', { description: 'Reference updated.' }); |
| 54 | reset(); |
| 55 | queryClient.invalidateQueries( |
| 56 | trpc.reference.getReferences.pathFilter(), |
| 57 | ); |
| 58 | queryClient.invalidateQueries( |
| 59 | trpc.reference.getChartReferences.pathFilter(), |
| 60 | ); |
| 61 | popModal(); |
| 62 | }, |
| 63 | onError: handleError, |
| 64 | }), |
| 65 | ); |
| 66 | |
| 67 | return ( |
| 68 | <ModalContent> |
| 69 | <ModalHeader title="Edit reference" /> |
| 70 | <form |
| 71 | className="flex flex-col gap-4" |
| 72 | onSubmit={handleSubmit((values) => { |
| 73 | mutation.mutate(values); |
| 74 | })} |
| 75 | > |
| 76 | <InputWithLabel label="Title" {...register('title')} /> |
| 77 | <InputWithLabel label="Description" {...register('description')} /> |
| 78 | <Controller |
| 79 | control={control} |
| 80 | name="datetime" |
| 81 | render={({ field }) => ( |
| 82 | <InputDateTime {...field} label="Date and time" /> |
| 83 | )} |
| 84 | /> |
| 85 | <ButtonContainer> |
| 86 | <Button type="button" variant="outline" onClick={() => popModal()}> |
| 87 | Cancel |
| 88 | </Button> |
| 89 | <Button type="submit" disabled={!formState.isDirty}> |
nothing calls this directly
no test coverage detected