|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { FormProvider, useForm } from 'react-hook-form'; |
| 4 | + |
| 5 | +import Fieldset from '@/components/form/Fieldset'; |
| 6 | +import Form from '@/components/form/Form'; |
| 7 | +import { useRouter } from '@/i18n/routing'; |
| 8 | +import { EditorImage } from '@/types/form'; |
| 9 | +import { contentToFormData } from '@/utils/formData'; |
| 10 | +import { handleServerResponse } from '@/utils/serverActionError'; |
| 11 | + |
| 12 | +export interface IntroFormData { |
| 13 | + description: string; |
| 14 | + image: EditorImage; |
| 15 | +} |
| 16 | + |
| 17 | +interface Props { |
| 18 | + cancelPath: string; |
| 19 | + defaultValues: IntroFormData; |
| 20 | + onSubmit: (formData: FormData) => Promise<unknown>; |
| 21 | +} |
| 22 | + |
| 23 | +export default function IntroEditor({ cancelPath, defaultValues, onSubmit: _onSubmit }: Props) { |
| 24 | + const router = useRouter(); |
| 25 | + const formMethods = useForm<IntroFormData>({ defaultValues }); |
| 26 | + const { handleSubmit } = formMethods; |
| 27 | + |
| 28 | + const onCancel = () => router.push(cancelPath); |
| 29 | + |
| 30 | + const onSubmit = handleSubmit(async ({ description, image }) => { |
| 31 | + const requestObject = { |
| 32 | + description, |
| 33 | + sequence: 0, |
| 34 | + name: 'name', |
| 35 | + removeImage: defaultValues.image !== null && image === null, |
| 36 | + }; |
| 37 | + |
| 38 | + const formData = contentToFormData('EDIT', { requestObject, image }); |
| 39 | + const resp = await _onSubmit(formData); |
| 40 | + handleServerResponse(resp, { successMessage: '수정 완료되었습니다.' }); |
| 41 | + }); |
| 42 | + |
| 43 | + return ( |
| 44 | + <FormProvider {...formMethods}> |
| 45 | + <Form> |
| 46 | + <Fieldset.HTML> |
| 47 | + <Form.HTML name="description" options={{ required: true }} /> |
| 48 | + </Fieldset.HTML> |
| 49 | + |
| 50 | + <Fieldset.Image> |
| 51 | + <label className="mb-3 whitespace-pre-wrap text-sm font-normal tracking-wide text-neutral-500"> |
| 52 | + 학생회 조직도 |
| 53 | + </label> |
| 54 | + <Form.Image name="image" /> |
| 55 | + </Fieldset.Image> |
| 56 | + |
| 57 | + <Form.Action onCancel={onCancel} onSubmit={onSubmit} /> |
| 58 | + </Form> |
| 59 | + </FormProvider> |
| 60 | + ); |
| 61 | +} |
0 commit comments