import * as Yup from "yup"; import { Question } from "../../../types/Item"; import { getLocalStorage } from "../../../utils/LocalStorage"; import { QUESTION_OBJECT_KEY } from "../../../config/AppKey"; export const getInitialValues = (objectToEdit: Question): any => { const tags = objectToEdit?.tags?.map((item: any, index: number) => { return { ...item, key: index }; }); console.log(objectToEdit); return { id: objectToEdit?.id ?? null, content: objectToEdit?.content ?? "", image: objectToEdit?.image ?? "", subject_id: objectToEdit?.subject_id ?? "", canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0, hint: objectToEdit?.hint ?? "", isBase: 0, parent_id: objectToEdit?.parent_id ?? "", answers: objectToEdit?.answers ?? [], tags: tags ?? [], }; }; export const getValidationSchema = () => { // validate input return 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(), }), ) .nullable("Params are required"), }); }; 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, key: `${tag?.id}_key_${tag?.name}`, })); return { ...item, canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0, hint: item?.hint ?? "", tags, }; }); const questions = newQuestions ?? []; return { id: objectToEdit?.id ?? null, content: objectToEdit?.content ?? "", image: objectToEdit?.image ?? "", subject_id: objectToEdit?.subject_id ?? "", isBase: 1, parent_id: objectToEdit?.parent_id ?? "", canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0, hint: objectToEdit?.hint ?? "", Questions: questions, }; }; export const getValidationSchemaBase = () => { // validate input return Yup.object().shape({ 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(), }), ) .nullable("required"), }), ), }); }; export function processTags(DataToSend: any) { const oldTags = DataToSend?.tags ?.map((item: any, index: number) => { if (typeof item?.id === "number") { return item?.id; } }) .filter((item: any) => item !== undefined); const newTags = DataToSend?.tags ?.map((item: any, index: number) => { if (typeof item?.id === "string" && item?.name !== "") { return { name: item?.name }; } }) .filter((item: any) => item !== undefined); return { new: newTags, old: oldTags }; }