151 lines
4.2 KiB
TypeScript
151 lines
4.2 KiB
TypeScript
import * as Yup from "yup";
|
|
import { Question } from "../../../../types/Item";
|
|
import { toast } from "react-toastify";
|
|
|
|
export const getInitialValues = (objectToEdit: Question): any => {
|
|
const tags = objectToEdit?.tags?.map((item: any, index: number) => {
|
|
return { ...item };
|
|
});
|
|
|
|
return {
|
|
id: objectToEdit?.id ?? null,
|
|
content: objectToEdit?.content ?? "",
|
|
content_image: objectToEdit?.content_image ?? "",
|
|
subject_id: objectToEdit?.subject_id ?? "",
|
|
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
|
|
hint: objectToEdit?.hint ?? null,
|
|
isBase: 0,
|
|
parent_id: objectToEdit?.parent_id ?? "",
|
|
answers: objectToEdit?.answers ?? null,
|
|
tags: tags ?? [],
|
|
};
|
|
};
|
|
|
|
export const getValidationSchema = () => {
|
|
// validate input
|
|
return Yup.object().shape({
|
|
content_image: Yup.string().nullable(),
|
|
content: Yup.string().required("validation.required"),
|
|
answers: Yup.array().of(
|
|
Yup.object().shape({
|
|
content: Yup.string().required("validation.required"),
|
|
content_image: Yup.string().nullable(),
|
|
isCorrect: Yup.boolean(),
|
|
}),
|
|
).min(2).test(
|
|
"at-least-one-correct",
|
|
"At least one answer must be correct",
|
|
(answers: any) => {
|
|
return answers?.some(
|
|
(answer: any) =>
|
|
answer?.isCorrect === true || answer?.isCorrect === 1,
|
|
);
|
|
},
|
|
),
|
|
});
|
|
};
|
|
|
|
export const getInitialValuesBase = (objectToEdit: Question): any => {
|
|
const newQuestions = objectToEdit?.Questions?.map((item: any) => {
|
|
const tags = item?.tags?.map((tag: any) => ({
|
|
id: tag?.id,
|
|
name: tag?.name,
|
|
}));
|
|
const newAnswers = item?.answers?.map((item:any)=>{
|
|
return {
|
|
...item,
|
|
content : item?.content ?? null
|
|
|
|
}
|
|
})
|
|
console.log(newAnswers,"newAnswers");
|
|
|
|
return {
|
|
...item,
|
|
answer:newAnswers,
|
|
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
|
|
hint: objectToEdit?.hint ?? "",
|
|
isBase: 0,
|
|
tags,
|
|
};
|
|
});
|
|
|
|
const questions = newQuestions ?? [{answers:[]}];
|
|
|
|
return {
|
|
id: objectToEdit?.id ?? null,
|
|
content: objectToEdit?.content ?? "",
|
|
content_image: objectToEdit?.content_image ?? "",
|
|
subject_id: objectToEdit?.subject_id ?? "",
|
|
isBase: 1,
|
|
parent_id: objectToEdit?.parent_id ?? "",
|
|
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
|
|
hint: objectToEdit?.hint ?? null,
|
|
Questions: questions,
|
|
};
|
|
};
|
|
|
|
export const getValidationSchemaBase = () => {
|
|
// validate input
|
|
return Yup.object().shape({
|
|
content_image: Yup.string().nullable(),
|
|
content: Yup.string().required("validation.required"),
|
|
Questions: Yup.array().of(
|
|
Yup.object().shape({
|
|
image: Yup.string().nullable(),
|
|
content: Yup.string().required("validation.required"),
|
|
answers: Yup.array()
|
|
.of(
|
|
Yup.object().shape({
|
|
content: Yup.string().required("validation.required"),
|
|
answer_image: Yup.string().nullable(),
|
|
isCorrect: Yup.boolean(),
|
|
}),
|
|
).min(2)
|
|
.test(
|
|
"at-least-one-correct",
|
|
"At least one answer must be correct",
|
|
(answers: any) => {
|
|
|
|
return answers.some(
|
|
(answer: any) =>
|
|
answer.isCorrect === true || answer.isCorrect === 1,
|
|
);
|
|
},
|
|
),
|
|
}),
|
|
).min(1),
|
|
});
|
|
};
|
|
|
|
export function processTags(DataToSend: any) {
|
|
console.log(DataToSend?.tags);
|
|
|
|
const oldTags = DataToSend?.tags
|
|
?.map((item: any, index: number) => {
|
|
if (typeof item === "number" || typeof item?.id === "number") {
|
|
return item?.id ?? item;
|
|
}
|
|
})
|
|
.filter((item: any) => item !== undefined);
|
|
|
|
const newTags = DataToSend?.tags
|
|
?.map((item: any, index: number) => {
|
|
console.log(item);
|
|
|
|
if (
|
|
(typeof item === "string" && item !== "") ||
|
|
(typeof item?.id === "string" && item?.id !== "")
|
|
) {
|
|
console.log(item);
|
|
|
|
return { name: item?.id ?? item };
|
|
}
|
|
})
|
|
.filter((item: any) => item !== undefined);
|
|
console.log(newTags);
|
|
console.log(oldTags);
|
|
|
|
return { new: newTags, old: oldTags };
|
|
}
|