Skip to content

Commit

Permalink
πŸ”¨ Refactor(#47): μ‹ μ²­μ„œ μˆ˜μ • νŽ˜μ΄μ§€ λ¦¬νŒ©ν„°λ§
Browse files Browse the repository at this point in the history
  • Loading branch information
JIY00N2 committed Dec 20, 2023
1 parent 8faa3ab commit 412be48
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import type { ChangeEvent } from "react";
import { useState } from "react";
import { usePathname, useRouter } from "next/navigation";
import { useToast } from "@/components/ui/use-toast";
import { usePathname } from "next/navigation";
import { TextArea } from "@radix-ui/themes";
import { useSuspenseQuery } from "@tanstack/react-query";
import editApplication from "@/services/application/editApplication";
import getApplicationDetails from "@/services/application/getApplicationDetails";
import getSteadyDetails from "@/services/steady/getSteadyDetails";
import Button, { buttonSize } from "@/components/_common/Button";
import { Question, Title } from "@/components/containers/application";
import EditButtons from "@/components/containers/applicationEdit/EditButtons";
import {
getApplicationDetailsKey,
getSteadyDetailsKey,
Expand All @@ -23,15 +21,12 @@ const ApplicationEditPage = ({
}) => {
const applicationId = params.application_id;
const steadyId = params.steady_id;
const router = useRouter();
const pathname = usePathname();
const pageType = pathname.split("/")[2];
const { toast } = useToast();
const { data: applicationData, refetch: applicationRefetch } =
useSuspenseQuery({
queryKey: getApplicationDetailsKey(applicationId),
queryFn: () => getApplicationDetails(applicationId),
});
const { data: applicationData } = useSuspenseQuery({
queryKey: getApplicationDetailsKey(applicationId),
queryFn: () => getApplicationDetails(applicationId),
});

const { data: steadyDetailsData } = useSuspenseQuery({
queryKey: getSteadyDetailsKey(steadyId),
Expand All @@ -46,33 +41,6 @@ const ApplicationEditPage = ({
})),
);

const handleClickEdit = async () => {
const checkInvalidAnswers = survey.some((item) => !item.answer.length);
if (checkInvalidAnswers) {
toast({
description: "μž…λ ₯λ˜μ§€ μ•Šμ€ ν•­λͺ©μ΄ μžˆμŠ΅λ‹ˆλ‹€.",
variant: "red",
});
} else {
try {
await editApplication(applicationId, {
answers: survey.map((item) => item.answer),
});
toast({
description: "μŠ€ν…Œλ”” μ‹ μ²­μ„œ μˆ˜μ • 성곡!",
variant: "green",
});
await applicationRefetch();
router.replace(`/steady/detail/${steadyId}`);
} catch (error) {
toast({
description: "μŠ€ν…Œλ”” μ‹ μ²­μ„œ μˆ˜μ • μ‹€νŒ¨!",
variant: "red",
});
}
}
};

const handleChangeAnswer = (
event: ChangeEvent<HTMLTextAreaElement>,
index: number,
Expand Down Expand Up @@ -113,18 +81,11 @@ const ApplicationEditPage = ({
))}
</Title>
<div className="flex items-center justify-end gap-20">
<Button
className={`${buttonSize.sm} bg-st-white`}
onClick={() => router.back()}
>
μ·¨μ†Œ
</Button>
<Button
onClick={handleClickEdit}
className={`${buttonSize.sm} bg-st-primary text-st-white`}
>
μˆ˜μ • μ™„λ£Œ
</Button>
<EditButtons
survey={survey}
applicationId={applicationId}
steadyId={steadyId}
/>
</div>
</>
);
Expand Down
71 changes: 71 additions & 0 deletions src/components/containers/applicationEdit/EditButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useRouter } from "next/navigation";
import { useToast } from "@/components/ui/use-toast";
import { useSuspenseQuery } from "@tanstack/react-query";
import editApplication from "@/services/application/editApplication";
import getApplicationDetails from "@/services/application/getApplicationDetails";
import Button, { buttonSize } from "@/components/_common/Button";
import { getApplicationDetailsKey } from "@/constants/queryKeys";

interface EditButtonsProps {
survey: {
question: string;
answer: string;
}[];
applicationId: string;
steadyId: string;
}

const EditButtons = ({ survey, applicationId, steadyId }: EditButtonsProps) => {
const { toast } = useToast();
const router = useRouter();
const { refetch: applicationRefetch } = useSuspenseQuery({
queryKey: getApplicationDetailsKey(applicationId),
queryFn: () => getApplicationDetails(applicationId),
});

const handleClickEdit = async () => {
const checkInvalidAnswers = survey.some((item) => !item.answer.length);
if (checkInvalidAnswers) {
toast({
description: "μž…λ ₯λ˜μ§€ μ•Šμ€ ν•­λͺ©μ΄ μžˆμŠ΅λ‹ˆλ‹€.",
variant: "red",
});
} else {
try {
await editApplication(applicationId, {
answers: survey.map((item) => item.answer),
});
toast({
description: "μŠ€ν…Œλ”” μ‹ μ²­μ„œ μˆ˜μ • 성곡!",
variant: "green",
});
await applicationRefetch();
router.replace(`/steady/detail/${steadyId}`);
} catch (error) {
toast({
description: "μŠ€ν…Œλ”” μ‹ μ²­μ„œ μˆ˜μ • μ‹€νŒ¨!",
variant: "red",
});
}
}
};

return (
<>
<Button
className={`${buttonSize.sm} bg-st-white`}
onClick={() => router.back()}
>
μ·¨μ†Œ
</Button>
<Button
onClick={handleClickEdit}
className={`${buttonSize.sm} bg-st-primary text-st-white`}
>
μˆ˜μ • μ™„λ£Œ
</Button>
</>
);
};

export default EditButtons;

0 comments on commit 412be48

Please sign in to comment.