--- lib/domain/content-form.ts
+++ lib/domain/content-form.ts
... | ... | @@ -9,10 +9,11 @@ |
| 9 | 9 |
MIN_COMIC_CUTS, |
| 10 | 10 |
QUIZ_ANSWER_OPTIONS, |
| 11 | 11 |
QUIZ_LEVEL_OPTIONS, |
| 12 |
- findSchoolLevel, |
|
| 13 |
- gradeOptionsOf, |
|
| 12 |
+ gradesOf, |
|
| 14 | 13 |
type ContentKeyword, |
| 14 |
+ type GradesBySchool, |
|
| 15 | 15 |
} from '@/lib/domain/content'; |
| 16 |
+import type { CommonCode } from '@/lib/domain/common-code';
|
|
| 16 | 17 |
|
| 17 | 18 |
export const TITLE_MAX_LENGTH = 200; |
| 18 | 19 |
export const SUBJECT_MAX_LENGTH = 200; |
... | ... | @@ -32,8 +33,16 @@ |
| 32 | 33 |
export const INITIAL_CONTENT_FORM_STATE: ContentFormState = { status: 'idle' };
|
| 33 | 34 |
|
| 34 | 35 |
export type GradeSelection = {
|
| 35 |
- schoolLevel: string; |
|
| 36 |
+ /** `COM_SCHUL_CD` 코드값. */ |
|
| 37 |
+ schoolCode: string; |
|
| 38 |
+ /** `GRD_CD` 코드값 — 백엔드가 받는 것도 이 값이다. */ |
|
| 36 | 39 |
grades: string[]; |
| 40 |
+}; |
|
| 41 |
+ |
|
| 42 |
+/** 학년 검증에 필요한 코드표. Server Action이 저장 직전에 다시 조회해 넘긴다. */ |
|
| 43 |
+export type SchoolGradeOptions = {
|
|
| 44 |
+ schools: readonly CommonCode[]; |
|
| 45 |
+ gradesBySchool: GradesBySchool; |
|
| 37 | 46 |
}; |
| 38 | 47 |
|
| 39 | 48 |
export type ShortsFormValues = GradeSelection & {
|
... | ... | @@ -87,22 +96,28 @@ |
| 87 | 96 |
|
| 88 | 97 |
function validateGrades( |
| 89 | 98 |
values: GradeSelection, |
| 99 |
+ codes: SchoolGradeOptions, |
|
| 90 | 100 |
errors: ContentFormErrors |
| 91 | 101 |
): void {
|
| 92 |
- const level = findSchoolLevel(values.schoolLevel); |
|
| 93 |
- if (!level) {
|
|
| 94 |
- errors.schoolLevel = '학교급을 선택해 주세요.'; |
|
| 102 |
+ if (!codes.schools.some((school) => school.code === values.schoolCode)) {
|
|
| 103 |
+ errors.schoolCode = '학교를 선택해 주세요.'; |
|
| 95 | 104 |
return; |
| 96 | 105 |
} |
| 97 |
- if (level.gradeCount === 0) {
|
|
| 106 |
+ |
|
| 107 |
+ const allowed = gradesOf(codes.gradesBySchool, values.schoolCode); |
|
| 108 |
+ // 학년 코드가 하나도 없는 학교(전체연령 등)는 학년을 고르지 않는다. |
|
| 109 |
+ if (allowed.length === 0) {
|
|
| 98 | 110 |
return; |
| 99 | 111 |
} |
| 100 |
- const allowed = gradeOptionsOf(values.schoolLevel); |
|
| 101 | 112 |
if (values.grades.length === 0) {
|
| 102 | 113 |
errors.grades = '학년을 하나 이상 선택해 주세요.'; |
| 103 | 114 |
return; |
| 104 | 115 |
} |
| 105 |
- if (values.grades.some((grade) => !allowed.includes(grade))) {
|
|
| 116 |
+ if ( |
|
| 117 |
+ values.grades.some( |
|
| 118 |
+ (grade) => !allowed.some((option) => option.code === grade) |
|
| 119 |
+ ) |
|
| 120 |
+ ) {
|
|
| 106 | 121 |
errors.grades = '선택할 수 없는 학년이 포함돼 있습니다.'; |
| 107 | 122 |
} |
| 108 | 123 |
} |
... | ... | @@ -131,10 +146,11 @@ |
| 131 | 146 |
} |
| 132 | 147 |
|
| 133 | 148 |
export function validateShortsForm( |
| 134 |
- values: ShortsFormValues |
|
| 149 |
+ values: ShortsFormValues, |
|
| 150 |
+ codes: SchoolGradeOptions |
|
| 135 | 151 |
): ValidationResult<ShortsFormValues> {
|
| 136 | 152 |
const errors: ContentFormErrors = {};
|
| 137 |
- validateGrades(values, errors); |
|
| 153 |
+ validateGrades(values, codes, errors); |
|
| 138 | 154 |
|
| 139 | 155 |
const title = requireText( |
| 140 | 156 |
values.title, |
... | ... | @@ -169,10 +185,11 @@ |
| 169 | 185 |
} |
| 170 | 186 |
|
| 171 | 187 |
export function validateComicForm( |
| 172 |
- values: ComicFormValues |
|
| 188 |
+ values: ComicFormValues, |
|
| 189 |
+ codes: SchoolGradeOptions |
|
| 173 | 190 |
): ValidationResult<ComicFormValues> {
|
| 174 | 191 |
const errors: ContentFormErrors = {};
|
| 175 |
- validateGrades(values, errors); |
|
| 192 |
+ validateGrades(values, codes, errors); |
|
| 176 | 193 |
|
| 177 | 194 |
const name = requireText( |
| 178 | 195 |
values.name, |
... | ... | @@ -213,10 +230,11 @@ |
| 213 | 230 |
} |
| 214 | 231 |
|
| 215 | 232 |
export function validateQuizForm( |
| 216 |
- values: QuizFormValues |
|
| 233 |
+ values: QuizFormValues, |
|
| 234 |
+ codes: SchoolGradeOptions |
|
| 217 | 235 |
): ValidationResult<QuizFormValues> {
|
| 218 | 236 |
const errors: ContentFormErrors = {};
|
| 219 |
- validateGrades(values, errors); |
|
| 237 |
+ validateGrades(values, codes, errors); |
|
| 220 | 238 |
|
| 221 | 239 |
if (!QUIZ_LEVEL_OPTIONS.some((option) => option.value === values.level)) {
|
| 222 | 240 |
errors.level = '난이도를 선택해 주세요.'; |
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?