Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/FE/ai-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
dooohun authored Feb 6, 2025
2 parents 6af270e + caad447 commit 85af271
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
12 changes: 11 additions & 1 deletion .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
3 changes: 2 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"react-error-boundary": "^4.1.2",
"react-router-dom": "^6.27.0",
"recharts": "^2.13.3",
"socket.io-client": "^4.8.1"
"socket.io-client": "^4.8.1",
"zod": "^3.24.1"
},
"devDependencies": {
"@chromatic-com/storybook": "^3.2.2",
Expand Down
19 changes: 15 additions & 4 deletions packages/client/src/pages/quiz-create/ui/QuizActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import { useParams } from 'react-router-dom';
import { useState } from 'react';
import Modal from '@/shared/ui/modal';
import AiQuizModal from '@/pages/quiz-list/ui/AiQuizModal';
import { quizzesSchema } from '@/shared/validation/quizSchema';
import { z } from 'zod';
import { toastController } from '@/features/toast/model/toastController';

export default function QuizActions() {
const { quizzes, currentQuizIndex, setQuizzes, setCurrentQuizIndex } = useQuizContext();
const createQuizMutation = useCreateQuiz();
const { classId } = useParams();
const [aiModal, setAiModal] = useState(false);
const toast = toastController();

const addNewQuiz = () => {
setQuizzes((prev) => [...prev, { ...INITIAL_QUIZ_VALUE }]);
Expand All @@ -33,10 +37,17 @@ export default function QuizActions() {
};

const handleCreateQuiz = () => {
const quizzesData = {
quizzes: quizzes,
};
createQuizMutation.mutate({ quizData: quizzesData, classId: Number(classId) });
try {
quizzesSchema.parse(quizzes);
const quizzesData = {
quizzes: quizzes,
};
createQuizMutation.mutate({ quizData: quizzesData, classId: Number(classId) });
} catch (error) {
if (error instanceof z.ZodError) {
toast.error(error.errors[0].message);
}
}
};

const onAIModalClose = () => {
Expand Down
30 changes: 30 additions & 0 deletions packages/client/src/shared/validation/quizSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from "zod";

const choiceSchema = z.object({
content: z.string().min(1, "선택지 내용을 입력해야 합니다.").max(255, "선택지 내용은 255자 이하여야 합니다."),
isCorrect: z.boolean(),
position: z.number().int().nonnegative(),
});

const quizSchema = z.object({
content: z.string().min(1, "퀴즈 제목을 입력해야 합니다.").max(255, "퀴즈 제목은 255자 이하여야 합니다."),
quizType: z.enum(["TF", "MC"]),
timeLimit: z.number().int().nonnegative(),
point: z.number().int().nonnegative(),
position: z.number().int().nonnegative(),
choices: z.array(choiceSchema).min(2)
.superRefine((choices, ctx) => {
const hasCorrectChoice = choices.some((choice) => choice.isCorrect);

if (!hasCorrectChoice) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "적어도 하나의 정답을 선택해야 합니다.",
})
}
})
})

const quizzesSchema = z.array(quizSchema).min(1);

export { quizSchema, choiceSchema, quizzesSchema };
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8276,6 +8276,7 @@ __metadata:
vite-plugin-svgr: "npm:^4.3.0"
vite-tsconfig-paths: "npm:^5.1.4"
vitest: "npm:^3.0.2"
zod: "npm:^3.24.1"
peerDependencies:
eslint: ^9.13.0
languageName: unknown
Expand Down Expand Up @@ -18363,3 +18364,10 @@ __metadata:
checksum: 10c0/38f91ca116a38561cf184c29e035e9453b12c30eaf574e0993107a4a5331882b58c9a7f7b97f63910664028089fbde3296d0b3682d1ccb2ad96929e68f1b2b89
languageName: node
linkType: hard

"zod@npm:^3.24.1":
version: 3.24.1
resolution: "zod@npm:3.24.1"
checksum: 10c0/0223d21dbaa15d8928fe0da3b54696391d8e3e1e2d0283a1a070b5980a1dbba945ce631c2d1eccc088fdbad0f2dfa40155590bf83732d3ac4fcca2cc9237591b
languageName: node
linkType: hard

0 comments on commit 85af271

Please sign in to comment.