Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import { Button } from "@/components/ui/button";

type CreatePageHeaderProps = {
isSaved: boolean;
isSubmitting: boolean;
onSave: () => void;
onCancel: () => void;
};

export function CreatePageHeader({
isSaved,
isSubmitting,
onSave,
onCancel,
}: CreatePageHeaderProps) {
Expand All @@ -27,10 +29,10 @@ export function CreatePageHeader({
<div className="flex items-center gap-2 sm:gap-3">
<Button
onClick={onSave}
disabled={isSaved}
disabled={isSaved || isSubmitting}
className="h-12 w-[112px] rounded-[12px] bg-[#3863f6] px-0 text-[14px] font-semibold leading-5 tracking-[-0.14px] text-white shadow-[0_0_14px_rgba(138,138,138,0.08)] sm:h-14 sm:w-[140px] sm:text-[16px] sm:leading-[24px] sm:tracking-[-0.16px]"
>
๊ฐœ์„คํ•˜๊ธฐ
{isSubmitting ? "๊ฐœ์„ค ์ค‘..." : "๊ฐœ์„คํ•˜๊ธฐ"}
</Button>
<Button
onClick={onCancel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { UseFormReturn } from "react-hook-form";
import { UseMutationResult } from "@tanstack/react-query";
import { createElement } from "react";
import { createElement, useRef } from "react";

import {
LectureCreatePayload,
Expand Down Expand Up @@ -36,6 +36,7 @@ export const useLectureCreateForm = ({
onSuccess,
}: UseLectureCreateFormParams) => {
const { openModal } = useModal();
const submitLockRef = useRef(false);

const openAlertModal = (title: string, description?: string) => {
openModal(
Expand All @@ -49,57 +50,66 @@ export const useLectureCreateForm = ({
);
};

const handleSave = lectureForm.handleSubmit(
(lectureData) => {
const hasInvalidSchedule = schedules.some(
(id) => !scheduleSchema.safeParse(scheduleData[id]).success
);
const onValidSubmit = (lectureData: LectureFormInput) => {
if (submitLockRef.current || createLecture.isPending) return;

if (hasInvalidSchedule) {
openAlertModal(
"์‹œ๊ฐ„ํ‘œ ์ž…๋ ฅ ์˜ค๋ฅ˜",
"์š”์ผ/์‹œ์ž‘/์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
);
return;
}

const lectureTimes = schedules.map((id) => ({
day: scheduleData[id]?.day || "",
startTime: scheduleData[id]?.startTime || "",
endTime: scheduleData[id]?.endTime || "",
}));

const enrollments = lectureData.students.map((student) => ({
school: student.school,
schoolYear: student.studentGrade,
studentName: student.name,
studentPhone: student.phone,
parentPhone: student.parentPhone,
}));

const payload: LectureCreatePayload = {
title: lectureData.name,
schoolYear: lectureData.schoolYear,
subject: lectureData.subject,
status: mapLectureStatusToApi(lectureData.status as LectureStatus),
startAt: lectureData.startDate
? new Date(lectureData.startDate).toISOString()
: null,
lectureTimes,
enrollments,
};

createLecture.mutate(payload, {
onSuccess,
onError: () => {
openAlertModal("์ €์žฅ ์‹คํŒจ", "์ €์žฅ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
},
});
},
() => {
openAlertModal("ํ•„์ˆ˜ ์ž…๋ ฅ๊ฐ’ ํ™•์ธ", "ํ•„์ˆ˜ ์ž…๋ ฅ๊ฐ’์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.");
const hasInvalidSchedule = schedules.some(
(id) => !scheduleSchema.safeParse(scheduleData[id]).success
);

if (hasInvalidSchedule) {
openAlertModal(
"์‹œ๊ฐ„ํ‘œ ์ž…๋ ฅ ์˜ค๋ฅ˜",
"์š”์ผ/์‹œ์ž‘/์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๋ชจ๋‘ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."
);
return;
}
);

const lectureTimes = schedules.map((id) => ({
day: scheduleData[id]?.day || "",
startTime: scheduleData[id]?.startTime || "",
endTime: scheduleData[id]?.endTime || "",
}));

const enrollments = lectureData.students.map((student) => ({
school: student.school,
schoolYear: student.studentGrade,
studentName: student.name,
studentPhone: student.phone,
parentPhone: student.parentPhone,
}));

const payload: LectureCreatePayload = {
title: lectureData.name,
schoolYear: lectureData.schoolYear,
subject: lectureData.subject,
status: mapLectureStatusToApi(lectureData.status as LectureStatus),
startAt: lectureData.startDate
? new Date(lectureData.startDate).toISOString()
: null,
lectureTimes,
enrollments,
};

submitLockRef.current = true;
createLecture.mutate(payload, {
onSuccess,
onError: () => {
openAlertModal("์ €์žฅ ์‹คํŒจ", "์ €์žฅ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
},
onSettled: () => {
submitLockRef.current = false;
},
});
};

const onInvalidSubmit = () => {
openAlertModal("ํ•„์ˆ˜ ์ž…๋ ฅ๊ฐ’ ํ™•์ธ", "ํ•„์ˆ˜ ์ž…๋ ฅ๊ฐ’์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.");
};

const handleSave = () => {
void lectureForm.handleSubmit(onValidSubmit, onInvalidSubmit)();
};

const handleCancel = (isSaved: boolean, onBack: () => void) => {
if (isSaved) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const useLectureCreatePage = () => {
}, [resetCreateState]);

const createLectureMutation = useCreateLecture();
const isSubmitting = createLectureMutation.isPending;

const { handleSave, handleCancel } = useLectureCreateForm({
lectureForm,
Expand All @@ -85,6 +86,7 @@ export const useLectureCreatePage = () => {
return {
state: {
isSaved,
isSubmitting,
},
form: {
lectureForm,
Expand Down
1 change: 1 addition & 0 deletions src/app/(dashboard)/educators/lectures/create/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function LectureCreatePage() {
<div className="min-h-screen bg-[#f4f6fa]">
<CreatePageHeader
isSaved={state.isSaved}
isSubmitting={state.isSubmitting}
onSave={actions.save}
onCancel={actions.cancel}
/>
Expand Down