refactor(client-side): create reusable types
mostly contains interfaces for client-side forms
This commit is contained in:
parent
8ac524fb9a
commit
c0c11ba1ea
56
lib/client/chapterSchema.ts
Normal file
56
lib/client/chapterSchema.ts
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import * as yup from "yup";
|
||||||
|
import { FormChapter, FormStory } from "./types/FormStory";
|
||||||
|
|
||||||
|
export const cs = yup.object<FormChapter>().shape({
|
||||||
|
chapterTitle: yup.string().ensure().min(1).trim().required("Chapter title cannot be blank."),
|
||||||
|
summary: yup.string().ensure().min(10).required("Summary cannot be blank"),
|
||||||
|
notes: yup.string().ensure(),
|
||||||
|
bands: yup.array().ensure().of(yup.number()).min(1, "One or more bands must be selected."),
|
||||||
|
characters: yup.array().ensure().min(1, "One or more characters must be selected"),
|
||||||
|
relationships: yup.array().ensure().of(
|
||||||
|
yup.array().ensure().of(
|
||||||
|
yup.string()
|
||||||
|
).min(2, "Pairings must have at least two characters!").max(3, "Pairings can have no more than three characters!")
|
||||||
|
),
|
||||||
|
nsfw: yup.boolean().oneOf([true, false]),
|
||||||
|
loggedInOnly: yup.boolean()
|
||||||
|
.when("nsfw", ([nsfw], schema) => {
|
||||||
|
return nsfw ? schema.oneOf([true], "If your story contains adult content, you MUST restrict the ability to read it to logged-in users only. Failure to comply may result in a takedown.") : schema.oneOf([true, false])
|
||||||
|
}),
|
||||||
|
hidden: yup.boolean().oneOf([true, false]),
|
||||||
|
pot: yup.string().oneOf(["pasteOrType", "upload"]).required("Story content is required!"),
|
||||||
|
storytext: yup.string().when("pot",
|
||||||
|
([pot], schema) => {
|
||||||
|
return pot === "pasteOrType" ? schema.test("numWords", "Story must be at least 50 words",
|
||||||
|
(value:any, context) => {
|
||||||
|
return value?.split(/\W+/).length > 50 || false
|
||||||
|
}).required("Story text can't be blank!") : schema.min(0)
|
||||||
|
}),
|
||||||
|
// hello Celeste
|
||||||
|
docFile: yup.mixed().when("pot", ([pot], schema) => {
|
||||||
|
return pot === "upload" ? yup.mixed().test("exists", "You need to upload a file!", (value) => {
|
||||||
|
// console.debug("file: ", value)
|
||||||
|
return !!value;
|
||||||
|
}).test("fileType", "Supported files are *.docx and *.doc", (value) => {
|
||||||
|
let ext;
|
||||||
|
if(typeof value == "string") {
|
||||||
|
ext = value?.split(".").reverse()[0]
|
||||||
|
} else {
|
||||||
|
ext = (value as File)?.name?.split(".").reverse()[0]
|
||||||
|
}
|
||||||
|
// console.log(ext)
|
||||||
|
let reg = /(docx|doc)$/i
|
||||||
|
return reg.test(ext)
|
||||||
|
}) : yup.mixed()
|
||||||
|
} /* {
|
||||||
|
is: "upload",
|
||||||
|
then: ,
|
||||||
|
otherwise: yup.mixed()
|
||||||
|
} */)
|
||||||
|
})
|
||||||
|
|
||||||
|
export const storySchema = yup.object().shape({
|
||||||
|
title: yup.string().ensure().min(5).required("Title cannot be blank"),
|
||||||
|
chapters: yup.array().ensure().of(cs),
|
||||||
|
completed: yup.boolean().oneOf([true, false])
|
||||||
|
})
|
45
lib/client/types/FormStory.ts
Normal file
45
lib/client/types/FormStory.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
export interface FormChapter {
|
||||||
|
chapterTitle: string;
|
||||||
|
index: number;
|
||||||
|
summary: string;
|
||||||
|
notes: string;
|
||||||
|
genre: string[];
|
||||||
|
bands: number[];
|
||||||
|
characters: string[];
|
||||||
|
relationships: string[][];
|
||||||
|
nsfw: boolean;
|
||||||
|
loggedInOnly: boolean;
|
||||||
|
hidden: boolean;
|
||||||
|
content: string;
|
||||||
|
fileName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FormStory {
|
||||||
|
title: string;
|
||||||
|
chapters: FormChapter[];
|
||||||
|
ficmas?: number | null;
|
||||||
|
challenge?: number | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultChapter: FormChapter = {
|
||||||
|
chapterTitle: "",
|
||||||
|
index: 1,
|
||||||
|
summary: "",
|
||||||
|
notes: "",
|
||||||
|
genre: [],
|
||||||
|
bands: [],
|
||||||
|
characters: [],
|
||||||
|
relationships: [],
|
||||||
|
nsfw: false,
|
||||||
|
loggedInOnly: true,
|
||||||
|
hidden: false,
|
||||||
|
content: "",
|
||||||
|
fileName: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
export const defaultStory: FormStory = {
|
||||||
|
title: "",
|
||||||
|
chapters: [],
|
||||||
|
ficmas: null,
|
||||||
|
challenge: null,
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user