Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(admin): survey 유효성 검사 추가 #3

Open
wants to merge 2 commits into
base: feat/admin-data
Choose a base branch
from
Open
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
51 changes: 29 additions & 22 deletions apps/survey-admin/src/app/hooks/query/useServey.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import { useSupabaseContext } from '@ssoon-servey/supabase';

export type Option = {
text: string;
};

export type SurveyItem = {
title: string;
type: 'radio' | 'select' | 'checkbox' | 'textarea';
required: boolean;
options: Option[];
};

export type SurveySection = {
title?: string;
items: SurveyItem[];
};

export type Survey = {
title: string;
description?: string;
sections: SurveySection[];
};
import { z } from 'zod';

const OptionSchema = z.object({
text: z.string().min(1, { message: '텍스트를 입력해주세요' }),
});

const SurveyItemSchema = z.object({
title: z.string().min(1, { message: '항목의 제목을 입력해주세요' }),
type: z.enum(['radio', 'select', 'checkbox', 'textarea']),
required: z.boolean(),
options: z.array(OptionSchema),
});

const SurveySectionSchema = z.object({
title: z.string().optional(),
items: z.array(SurveyItemSchema),
});

const SurveySchema = z.object({
title: z.string().min(1, { message: '설문 제목을 입력해주세요' }),
description: z.string().optional(),
sections: z.array(SurveySectionSchema),
});

export type Option = z.infer<typeof OptionSchema>;
export type SurveyItem = z.infer<typeof SurveyItemSchema>;
export type SurveySection = z.infer<typeof SurveySectionSchema>;
export type Survey = z.infer<typeof SurveySchema>;

export const useCreateSurvey = () => {
const { supabase } = useSupabaseContext();
Expand Down Expand Up @@ -103,6 +109,7 @@ export const useCreateSurvey = () => {
};

const mutate = async (payload: Survey) => {
SurveySchema.parse(payload);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3: 이렇게 검사하게되면 UI에 어떻게 노출될까요? 에러를 반환하고 form상에 노출하는게 가장 좋아보여요

const { title, description, sections } = payload;

const survey = await createSurvey(title, description);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"immer": "^10.0.3",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-router-dom": "6.11.2"
"react-router-dom": "6.11.2",
"zod": "^3.22.4"
},
"devDependencies": {
"@babel/core": "^7.14.5",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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