Skip to content

Commit

Permalink
♻️ AboutEditor에서 server action을 주입받게 수정 (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeolyi authored Dec 8, 2024
1 parent 1ce477c commit 45099ae
Show file tree
Hide file tree
Showing 175 changed files with 433 additions and 312 deletions.
10 changes: 6 additions & 4 deletions actions/about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import {
FETCH_TAG_HISTORY,
FETCH_TAG_OVERVIEW,
} from '@/constants/network';
import { redirectKo } from '@/i18n/routing';
import { FutureCareers } from '@/types/about';
import { getPath } from '@/utils/page';
import {
contact,
directions,
Expand All @@ -37,7 +34,11 @@ import {
history,
overview,
studentClubs,
} from '@/utils/segmentNode';
} from '@/constants/segmentNode';
import { redirectKo } from '@/i18n/routing';
import { FutureCareers } from '@/types/about';
import { getPath } from '@/utils/page';
import { decodeFormDataFileName } from '@/utils/string';

import { withErrorHandler } from './errorHandler';

Expand All @@ -46,6 +47,7 @@ import { withErrorHandler } from './errorHandler';
const overviewPath = getPath(overview);

export const putOverviewAction = withErrorHandler(async (formData: FormData) => {
decodeFormDataFileName(formData, 'newAttachments');
await putOverview(formData);
revalidateTag(FETCH_TAG_OVERVIEW);
redirectKo(overviewPath);
Expand Down
2 changes: 1 addition & 1 deletion actions/academics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
} from '@/types/academics';
import { WithLanguage } from '@/types/language';
import { getPath } from '@/utils/page';
import { graduateScholarship, undergraduateScholarship } from '@/utils/segmentNode';
import { graduateScholarship, undergraduateScholarship } from '@/constants/segmentNode';
import { decodeFormDataFileName } from '@/utils/string';

import { withErrorHandler } from './errorHandler';
Expand Down
2 changes: 1 addition & 1 deletion actions/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { deleteNews, patchNews } from '@/apis/v1/news/[id]';
import { FETCH_TAG_NEWS } from '@/constants/network';
import { redirectKo } from '@/i18n/routing';
import { getPath } from '@/utils/page';
import { news } from '@/utils/segmentNode';
import { news } from '@/constants/segmentNode';
import { decodeFormDataFileName } from '@/utils/string';

const newsPath = getPath(news);
Expand Down
2 changes: 1 addition & 1 deletion actions/notice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { FETCH_TAG_NOTICE } from '@/constants/network';
import { redirectKo } from '@/i18n/routing';
import { errorToStr } from '@/utils/error';
import { getPath } from '@/utils/page';
import { notice } from '@/utils/segmentNode';
import { notice } from '@/constants/segmentNode';
import { decodeFormDataFileName } from '@/utils/string';

const noticePath = getPath(notice);
Expand Down
2 changes: 1 addition & 1 deletion actions/people.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { redirect } from '@/i18n/routing';
import { Language, WithLanguage } from '@/types/language';
import { FacultyStatus } from '@/types/people';
import { getPath } from '@/utils/page';
import { emeritusFaculty, faculty, staff } from '@/utils/segmentNode';
import { emeritusFaculty, faculty, staff } from '@/constants/segmentNode';

import { withErrorHandler } from './errorHandler';

Expand Down
2 changes: 1 addition & 1 deletion actions/recruitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { putFacultyRecruitment } from '@/apis/v1/recruit';
import { FETCH_TAG_RECRUITMENT } from '@/constants/network';
import { redirect } from '@/i18n/routing';
import { getPath } from '@/utils/page';
import { facultyRecruitment } from '@/utils/segmentNode';
import { facultyRecruitment } from '@/constants/segmentNode';

import { withErrorHandler } from './errorHandler';

Expand Down
2 changes: 1 addition & 1 deletion actions/research.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { FETCH_TAG_CENTER, FETCH_TAG_GROUP, FETCH_TAG_LAB } from '@/constants/ne
import { redirectKo } from '@/i18n/routing';
import { WithLanguage } from '@/types/language';
import { getPath } from '@/utils/page';
import { researchCenters, researchGroups, researchLabs } from '@/utils/segmentNode';
import { researchCenters, researchGroups, researchLabs } from '@/constants/segmentNode';
import { decodeFormDataFileName } from '@/utils/string';

import { withErrorHandler } from './errorHandler';
Expand Down
2 changes: 1 addition & 1 deletion actions/seminar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { deleteSeminar, patchSeminar } from '@/apis/v1/seminar/[id]';
import { FETCH_TAG_SEMINAR } from '@/constants/network';
import { redirectKo } from '@/i18n/routing';
import { getPath } from '@/utils/page';
import { seminar } from '@/utils/segmentNode';
import { seminar } from '@/constants/segmentNode';
import { decodeFormDataFileName } from '@/utils/string';

const seminarPath = getPath(seminar);
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/10-10-project/manager/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HTMLViewer from '@/components/form/html/HTMLViewer';
import PageLayout from '@/components/layout/pageLayout/PageLayout';
import { getPath } from '@/utils/page';
import { greetings } from '@/utils/segmentNode';
import { greetings } from '@/constants/segmentNode';

export default async function TenTenManager() {
return (
Expand Down
147 changes: 56 additions & 91 deletions app/[locale]/about/components/AboutEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,115 +3,80 @@
import { useState } from 'react';
import { FormProvider, useForm } from 'react-hook-form';

import {
putContactAction,
putGreetingsAction,
putHistoryAction,
putOverviewAction,
} from '@/actions/about';
import Fieldset from '@/components/form/Fieldset';
import Form from '@/components/form/Form';
import LanguagePicker from '@/components/form/LanguagePicker';
import { PostEditorFile, PostEditorImage } from '@/components/form/types';
import PageLayout from '@/components/layout/pageLayout/PageLayout';
import { EditorFile, EditorImage } from '@/components/form/types';
import { useRouter } from '@/i18n/routing';
import { AboutContent } from '@/types/about';
import { Language, WithLanguage } from '@/types/language';
import { errorToStr } from '@/utils/error';
import { Language } from '@/types/language';
import { contentToFormData, getAttachmentDeleteIds } from '@/utils/formData';
import { getPath } from '@/utils/page';
import { contact, greetings, history, overview, SegmentNode } from '@/utils/segmentNode';
import { handleServerAction } from '@/utils/serverActionError';
import { errorToast, successToast } from '@/utils/toast';
import { handleServerResponse } from '@/utils/serverActionError';

interface Props {
data: WithLanguage<AboutContent>;
node: SegmentNode;
showAttachments?: boolean;
}

interface FormData {
export interface AboutFormData {
htmlKo: string;
htmlEn: string;
image: PostEditorImage;
files: PostEditorFile[];
image: EditorImage;
files: EditorFile[];
}

export default function AboutEditor({ data, node, showAttachments = false }: Props) {
interface Props {
cancelPath: string;
defaultValues: AboutFormData;
onSubmit: (formData: FormData) => Promise<unknown>;
showAttachments?: boolean;
}

export default function AboutEditor({
cancelPath,
defaultValues,
onSubmit: _onSubmit,
showAttachments = false,
}: Props) {
const router = useRouter();
const formMethods = useForm<FormData>({
defaultValues: {
htmlKo: data.ko.description,
htmlEn: data.en.description,
image: data.ko.imageURL ? { type: 'UPLOADED_IMAGE', url: data.ko.imageURL } : null,
files: data.ko.attachments.map((attachment) => ({ file: attachment, type: 'UPLOADED_FILE' })),
},
});
const formMethods = useForm<AboutFormData>({ defaultValues });
const { handleSubmit } = formMethods;

const [language, setLanguage] = useState<Language>('ko');

const onCancel = () => router.push(getPath(node));
const onCancel = () => router.push(cancelPath);

const onSubmit = handleSubmit(async ({ htmlKo, htmlEn, image, files }) => {
try {
const submitAction = ABOUT_SUBMIT_ACTION[node.segment];

const requestObject = {
ko: {
description: htmlKo,
deleteIds: getAttachmentDeleteIds(files, data.ko.attachments),
},
en: {
description: htmlEn,
deleteIds: getAttachmentDeleteIds(files, data.en.attachments),
},
removeImage: data.ko.imageURL !== null && image === null,
};

const formData = contentToFormData('EDIT', { requestObject, image, attachments: files });

handleServerAction(submitAction(formData));
successToast(`${node.name}을(를) 수정했습니다.`);
} catch (e) {
errorToast(errorToStr(e));
}
console.log(files, defaultValues);
const requestObject = {
ko: { description: htmlKo, deleteIds: getAttachmentDeleteIds(files, defaultValues.files) },
en: { description: htmlEn, deleteIds: [] },
removeImage: defaultValues.image !== null && image === null,
};

const formData = contentToFormData('EDIT', { requestObject, image, attachments: files });
const resp = await _onSubmit(formData);
handleServerResponse(resp, { successMessage: '수정 완료되었습니다.' });
});

return (
<PageLayout title={`${node.name} 편집`} titleType="big" hideNavbar>
<FormProvider {...formMethods}>
<Form>
<LanguagePicker onChange={setLanguage} selected={language} />

<Fieldset.HTML>
{language === 'ko' && <Form.HTML name="htmlKo" options={{ required: true }} />}
{language === 'en' && <Form.HTML name="htmlEn" />}
</Fieldset.HTML>

<Fieldset.Image>
<label className="mb-3 whitespace-pre-wrap text-sm font-normal tracking-wide text-neutral-500">
글 우측 상단에 들어가는 이미지입니다.
</label>
<Form.Image name="image" />
</Fieldset.Image>

{showAttachments && (
<Fieldset.File>
<Form.File name="files" />
</Fieldset.File>
)}

<Form.Action onCancel={onCancel} onSubmit={onSubmit} />
</Form>
</FormProvider>
</PageLayout>
<FormProvider {...formMethods}>
<Form>
<LanguagePicker onChange={setLanguage} selected={language} />

<Fieldset.HTML>
{language === 'ko' && <Form.HTML name="htmlKo" options={{ required: true }} />}
{language === 'en' && <Form.HTML name="htmlEn" />}
</Fieldset.HTML>

<Fieldset.Image>
<label className="mb-3 whitespace-pre-wrap text-sm font-normal tracking-wide text-neutral-500">
글 우측 상단에 들어가는 이미지입니다.
</label>
<Form.Image name="image" />
</Fieldset.Image>

{showAttachments && (
<Fieldset.File>
<Form.File name="files" />
</Fieldset.File>
)}

<Form.Action onCancel={onCancel} onSubmit={onSubmit} />
</Form>
</FormProvider>
);
}

const ABOUT_SUBMIT_ACTION = {
[overview.segment]: putOverviewAction,
[greetings.segment]: putGreetingsAction,
[history.segment]: putHistoryAction,
[contact.segment]: putContactAction,
};
28 changes: 25 additions & 3 deletions app/[locale]/about/contact/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import { putContactAction } from '@/actions/about';
import { getContact } from '@/apis/v1/about/contact';
import { contact } from '@/utils/segmentNode';
import PageLayout from '@/components/layout/pageLayout/PageLayout';
import { contact } from '@/constants/segmentNode';
import { getEditorFile, getEditorImage } from '@/utils/formData';
import { getPath } from '@/utils/page';

import AboutEditor from '../../components/AboutEditor';
import AboutEditor, { AboutFormData } from '../../components/AboutEditor';

const path = getPath(contact);

export default async function ContactEditPage() {
const [koData, enData] = await Promise.all([getContact('ko'), getContact('en')]);

return <AboutEditor data={{ ko: koData, en: enData }} node={contact} />;
const defaultValues: AboutFormData = {
htmlKo: koData.description,
htmlEn: enData.description,
image: getEditorImage(koData.imageURL),
files: getEditorFile(koData.attachments),
};

return (
<PageLayout title="연락처 편집" titleType="big" hideNavbar>
<AboutEditor
cancelPath={path}
defaultValues={defaultValues}
onSubmit={putContactAction}
showAttachments
/>
</PageLayout>
);
}
2 changes: 1 addition & 1 deletion app/[locale]/about/contact/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PageLayout from '@/components/layout/pageLayout/PageLayout';
import { Language } from '@/types/language';
import { getMetadata } from '@/utils/metadata';
import { getPath } from '@/utils/page';
import { contact } from '@/utils/segmentNode';
import { contact } from '@/constants/segmentNode';

interface ContactPageProps {
params: Promise<{ locale: Language }>;
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/about/directions/DirectionsDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import LoginVisible from '@/components/common/LoginVisible';
import HTMLViewer from '@/components/form/html/HTMLViewer';
import { Direction } from '@/types/about';
import { getPath } from '@/utils/page';
import { directions } from '@/utils/segmentNode';
import { directions } from '@/constants/segmentNode';
import { replaceSpaceWithDash } from '@/utils/string';

const directionsPath = getPath(directions);
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/about/directions/LocationGuide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useTranslations } from 'next-intl';

import { Link } from '@/i18n/routing';
import { getPath } from '@/utils/page';
import { staff } from '@/utils/segmentNode';
import { staff } from '@/constants/segmentNode';

const staffPath = getPath(staff);

Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/about/directions/edit/DirectionEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Direction } from '@/types/about';
import { Language, WithLanguage } from '@/types/language';
import { errorToStr } from '@/utils/error';
import { getPath } from '@/utils/page';
import { directions } from '@/utils/segmentNode';
import { directions } from '@/constants/segmentNode';
import { handleServerAction } from '@/utils/serverActionError';
import { errorToast, successToast } from '@/utils/toast';

Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/about/directions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Language } from '@/types/language';
import { findItemBySearchParam } from '@/utils/findSelectedItem';
import { getMetadata } from '@/utils/metadata';
import { getPath } from '@/utils/page';
import { directions } from '@/utils/segmentNode';
import { directions } from '@/constants/segmentNode';

import DirectionsDetails from './DirectionsDetails';
import LocationGuide from './LocationGuide';
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/about/facilities/FacilitiesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Distance from '@/public/image/distance.svg';
import { Facility } from '@/types/about';
import { errorToStr } from '@/utils/error';
import { getPath } from '@/utils/page';
import { facilities } from '@/utils/segmentNode';
import { facilities } from '@/constants/segmentNode';
import { handleServerAction } from '@/utils/serverActionError';
import { errorToast, successToast } from '@/utils/toast';

Expand Down
6 changes: 3 additions & 3 deletions app/[locale]/about/facilities/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FormProvider, useForm } from 'react-hook-form';
import { postFacilityAction, putFacilityAction } from '@/actions/about';
import Fieldset from '@/components/form/Fieldset';
import LanguagePicker from '@/components/form/LanguagePicker';
import { PostEditorImage } from '@/components/form/types';
import { EditorImage } from '@/components/form/types';
import Form from '@/components/form/Form';
import HTMLEditor from '@/components/form/html/HTMLEditor';
import PageLayout from '@/components/layout/pageLayout/PageLayout';
Expand All @@ -16,14 +16,14 @@ import { Language, WithLanguage } from '@/types/language';
import { errorToStr } from '@/utils/error';
import { contentToFormData } from '@/utils/formData';
import { getPath } from '@/utils/page';
import { facilities } from '@/utils/segmentNode';
import { facilities } from '@/constants/segmentNode';
import { handleServerAction } from '@/utils/serverActionError';
import { errorToast, successToast } from '@/utils/toast';

const facilitiesPath = getPath(facilities);

interface FormData extends WithLanguage<Facility> {
imageURL: PostEditorImage | null;
imageURL: EditorImage | null;
}

export default function FacilityCreator() {
Expand Down
Loading

0 comments on commit 45099ae

Please sign in to comment.