end question

This commit is contained in:
karimaldeen 2024-08-18 12:04:44 +03:00
parent e020fb69c1
commit fa8e8a7099
26 changed files with 124 additions and 140 deletions

View File

@ -125,6 +125,13 @@ const AddPage: React.FC = () => {
Questions?.map((item: Question) => {
const tags = processTags(item);
console.log(item);
const answers = item?.answers?.map((item:any,index:number)=>{
return {
order:index,
...item
}
})
console.log(answers);
mutate({
...item,
@ -132,18 +139,28 @@ const AddPage: React.FC = () => {
subject_id: subject_id,
tags,
lessons_ids: [lesson_id],
answers
});
});
console.log(newBseQuestionId, "newBseQuestionId");
});
} else {
const tags = processTags(DataToSend);
console.log(values,"values");
const answers = values?.answers?.map((item:any,index:number)=>{
return {
order:index,
...item
}
})
mutate({
...values,
subject_id: subject_id,
tags,
lessons_ids: [lesson_id],
canAnswersBeShuffled,
answers
});
}
};
@ -167,8 +184,8 @@ const AddPage: React.FC = () => {
}
}, [isSuccess, isSuccessAsync]);
let cleanedQuestionOptions = cleanObject(SavedQuestionData);
let noChange = hasItems(cleanedQuestionOptions);
let cleanedAnswers = cleanObject(SavedQuestionData);
let noChange = hasItems(cleanedAnswers);
useSaveOnDisconnect(noChange, QUESTION_OBJECT_KEY, SavedQuestionData);

View File

@ -152,25 +152,25 @@ const EditPage: React.FC = () => {
console.log(updatedObject, "updatedObject");
const tags = processTags(updatedObject);
const oldQuestionOptions = [] as any;
const newQuestionOptions = [] as any;
const oldanswers = [] as any;
const newanswers = [] as any;
updatedObject?.QuestionOptions?.forEach((item: any) => {
updatedObject?.answers?.forEach((item: any) => {
if (item?.id) {
oldQuestionOptions.push(item);
oldanswers.push(item);
} else {
newQuestionOptions.push(item);
newanswers.push(item);
}
});
const QuestionOptions = {
old: oldQuestionOptions,
new: newQuestionOptions,
const answers = {
old: oldanswers,
new: newanswers,
};
console.log(QuestionOptions);
console.log(answers);
mutate({
...updatedObject,
QuestionOptions,
answers,
tags,
});
} else {
@ -197,26 +197,26 @@ const EditPage: React.FC = () => {
}
console.log(updatedObject);
const oldQuestionOptions = [] as any;
const newQuestionOptions = [] as any;
const oldanswers = [] as any;
const newanswers = [] as any;
updatedObject?.QuestionOptions?.forEach((item: any) => {
updatedObject?.answers?.forEach((item: any) => {
if (item?.id) {
console.log(item);
oldQuestionOptions.push(item);
oldanswers.push(item);
} else {
newQuestionOptions.push(item);
newanswers.push(item);
}
});
const QuestionOptions = {
old: oldQuestionOptions,
new: newQuestionOptions,
const answers = {
old: oldanswers,
new: newanswers,
};
console.log(QuestionOptions, "QuestionOptions");
console.log(answers, "answers");
mutate({ ...updatedObject, QuestionOptions, tags });
mutate({ ...updatedObject, answers, tags });
}
};

View File

@ -15,18 +15,18 @@ const CheckboxField = ({
const formik = useFormikContext<any>();
const [t] = useTranslation();
const CheckboxhandleChange = (value: any, index: number) => {
const allAreZero = formik?.values?.QuestionOptions?.some(
const allAreZero = formik?.values?.answers?.some(
(item: any) => item.isCorrect === 1,
);
if (allAreZero) {
formik?.values.QuestionOptions.forEach((item: any, index: number) => {
formik.setFieldValue(`QuestionOptions[${index}].isCorrect`, 0);
formik?.values.answers.forEach((item: any, index: number) => {
formik.setFieldValue(`answers[${index}].isCorrect`, 0);
});
}
formik.setFieldValue(
`QuestionOptions[${name}].isCorrect`,
`answers[${name}].isCorrect`,
value?.target?.checked ? 1 : 0,
);
};
@ -35,7 +35,7 @@ const CheckboxField = ({
<Checkbox
onChange={onChange || CheckboxhandleChange}
disabled={isDisabled}
checked={formik.values?.QuestionOptions?.[name]?.isCorrect === 1}
checked={formik.values?.answers?.[name]?.isCorrect === 1}
className={className}
>
{t(`input.${label ? label : name}`)}

View File

@ -18,13 +18,13 @@ const ChoiceFields = ({ index, data }: { index: number; data: Choice }) => {
const handleDeleteChoice = () => {
console.log(index);
console.log(formik.values.QuestionOptions[index]);
console.log(formik.values.answers[index]);
const updatedQuestionOptions = formik.values.QuestionOptions.filter(
const updatedAnswers = formik.values.answers.filter(
(_: any, i: any) => i !== index,
);
formik.setFieldValue("QuestionOptions", updatedQuestionOptions);
formik.setFieldValue("answers", updatedAnswers);
};
return (

View File

@ -11,8 +11,8 @@ const Choices = () => {
// Check if the item was dropped outside the list
if (!result.destination) return;
// Create a new array from the current QuestionOptions
const items = Array.from(formik?.values?.QuestionOptions);
// Create a new array from the current answers
const items = Array.from(formik?.values?.answers);
// Remove the item from the original position
const [reorderedItem] = items.splice(result.source.index, 1);
@ -29,7 +29,7 @@ const Choices = () => {
// Update the formik state with the new order
console.log(updatedItems, "updatedItems");
formik.setFieldValue("QuestionOptions", updatedItems);
formik.setFieldValue("answers", updatedItems);
};
return (
@ -37,7 +37,7 @@ const Choices = () => {
<Droppable droppableId="choices">
{(provided) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
{formik?.values?.QuestionOptions?.map(
{formik?.values?.answers?.map(
(item: Choice, index: number) => {
// Use a unique identifier for draggableId
const draggableId = item.name

View File

@ -12,10 +12,10 @@ const File = ({
className,
props,
}: any) => {
const newName = `QuestionOptions[${name}].answer_image`;
const newName = `answers[${name}].answer_image`;
const { formik, t, isError, errorMsg } = useFormField(newName, props);
let imageUrl = formik?.values?.QuestionOptions[name]?.answer_image ?? null;
let imageUrl = formik?.values?.answers[name]?.answer_image ?? null;
// console.log(imageUrl);
const fileList: UploadFile[] = useMemo(() => {
@ -46,7 +46,7 @@ const File = ({
formik.setFieldValue(newName, null);
} else {
formik.setFieldValue(
`QuestionOptions[${name}].answer_image`,
`answers[${name}].answer_image`,
value?.file?.originFileObj,
);
}

View File

@ -18,7 +18,7 @@ const HintField = ({
id,
className,
}: any) => {
const newName = `QuestionOptions[${name}].hint`;
const newName = `answers[${name}].hint`;
const { formik, isError, errorMsg, t } = useFormField(newName, props);
const TextFilehandleChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,

View File

@ -18,7 +18,7 @@ const TextField = ({
id,
className,
}: any) => {
const newName = `QuestionOptions[${name}].answer`;
const newName = `answers[${name}].content`;
const { formik, isError, errorMsg, t } = useFormField(newName, props);
const TextFilehandleChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,

View File

@ -33,9 +33,9 @@ const Form = () => {
const handleAddChoice = (parent_index: number) => {
console.log(parent_index);
formik.setFieldValue(`Questions.[${parent_index}].QuestionOptions`, [
formik.setFieldValue(`Questions.[${parent_index}].answers`, [
...((formik?.values as any)?.Questions?.[parent_index]
.QuestionOptions as Choice[]),
.answers as Choice[]),
{
answer: null,
@ -54,9 +54,9 @@ const Form = () => {
image: "",
parent: "",
isBase: 0,
max_mark: 1,
min_mark_to_pass: 1,
QuestionOptions: [{ answer: null, answer_image: null, isCorrect: 0 }],
// max_mark: 1,
// min_mark_to_pass: 1,
answers: [{ answer: null, answer_image: null, isCorrect: 0 }],
tags: [],
},
]);
@ -106,7 +106,7 @@ const Form = () => {
<Choices parent_index={parent_index} />
{formik?.values?.Questions?.[parent_index]?.QuestionOptions
{formik?.values?.Questions?.[parent_index]?.answers
?.length < 5 && (
<p className="add_new_button">
<FaCirclePlus

View File

@ -19,12 +19,12 @@ const CheckboxField = ({
const allAreZero = formik?.values?.Questions?.[
parent_index
]?.QuestionOptions?.some((item: any) => item.isCorrect === 1);
]?.answers?.some((item: any) => item.isCorrect === 1);
if (allAreZero) {
formik?.values?.Questions?.[parent_index]?.QuestionOptions.forEach(
formik?.values?.Questions?.[parent_index]?.answers.forEach(
(item: any, index: number) => {
formik.setFieldValue(
`Questions[${parent_index}].QuestionOptions[${index}].isCorrect`,
`Questions[${parent_index}].answers[${index}].isCorrect`,
0,
);
},
@ -32,7 +32,7 @@ const CheckboxField = ({
}
formik.setFieldValue(
`Questions[${parent_index}].QuestionOptions[${name}].isCorrect`,
`Questions[${parent_index}].answers[${name}].isCorrect`,
value?.target?.checked ? 1 : 0,
);
};
@ -42,7 +42,7 @@ const CheckboxField = ({
onChange={onChange || CheckboxhandleChange}
disabled={isDisabled}
checked={
formik.values?.Questions?.[parent_index]?.QuestionOptions?.[name]
formik.values?.Questions?.[parent_index]?.answers?.[name]
?.isCorrect === 1
}
className={className}

View File

@ -26,7 +26,7 @@ const ChoiceFields = ({
const handleDeleteChoice = () => {
const arrayLength =
formik.values.Questions?.[parent_index].QuestionOptions?.length;
formik.values.Questions?.[parent_index].answers?.length;
console.log(arrayLength);
@ -37,12 +37,12 @@ const ChoiceFields = ({
return;
}
const updatedQuestionOptions = formik.values.Questions?.[
const updatedAnswers = formik.values.Questions?.[
parent_index
].QuestionOptions.filter((_: any, i: any) => i !== index);
].answers.filter((_: any, i: any) => i !== index);
formik.setFieldValue(
`Questions[${parent_index}].QuestionOptions`,
updatedQuestionOptions,
`Questions[${parent_index}].answers`,
updatedAnswers,
);
};
return (

View File

@ -14,10 +14,10 @@ const Choices = ({ parent_index }: { parent_index: number }) => {
if (!result.destination) return;
console.log(formik?.values?.Questions?.[parent_index]?.QuestionOptions);
console.log(formik?.values?.Questions?.[parent_index]?.answers);
// Create a new array from the current QuestionOptions
const items = Array.from(formik?.values?.Questions?.[parent_index]?.QuestionOptions);
// Create a new array from the current answers
const items = Array.from(formik?.values?.Questions?.[parent_index]?.answers);
console.log(items);
// Remove the item from the original position
const [reorderedItem] = items.splice(result.source.index, 1);
@ -36,7 +36,7 @@ const Choices = ({ parent_index }: { parent_index: number }) => {
// Update the formik state with the new order
console.log(updatedItems, "updatedItems");
formik.setFieldValue(`Questions.${parent_index}.QuestionOptions`, updatedItems);
formik.setFieldValue(`Questions.${parent_index}.answers`, updatedItems);
};
return (
@ -47,7 +47,7 @@ const Choices = ({ parent_index }: { parent_index: number }) => {
<div {...provided.droppableProps} ref={provided.innerRef}>
{(
(formik?.values as any)?.Questions?.[parent_index]
?.QuestionOptions || []
?.answers || []
).map((item: Choice, index: number) => {
const draggableId = item.name
? item.name.toString()

View File

@ -13,11 +13,11 @@ const File = ({
parent_index,
props,
}: any) => {
const newName = `Questions[${parent_index}].QuestionOptions[${name}].answer_image`;
const newName = `Questions[${parent_index}].answers[${name}].answer_image`;
const { formik, t, isError, errorMsg } = useFormField(newName, props);
let imageUrl =
formik?.values?.Questions?.[parent_index]?.QuestionOptions[name]
formik?.values?.Questions?.[parent_index]?.answers[name]
?.answer_image ?? null;
// console.log(imageUrl);
@ -49,7 +49,7 @@ const File = ({
formik.setFieldValue(newName, null);
} else {
formik.setFieldValue(
`Questions[${parent_index}].QuestionOptions[${name}].answer_image`,
`Questions[${parent_index}].answers[${name}].answer_image`,
value?.file?.originFileObj,
);
}

View File

@ -16,7 +16,7 @@ const HintField = ({
id,
className,
}: any) => {
const newName = `Questions[${parent_index}].QuestionOptions[${name}].hint`;
const newName = `Questions[${parent_index}].answers[${name}].hint`;
const { formik, isError, errorMsg, t } = useFormField(newName, props);
const TextFilehandleChange = (

View File

@ -18,7 +18,7 @@ const TextField = ({
parent_index,
className,
}: any) => {
const newName = `Questions[${parent_index}].QuestionOptions[${name}].answer`;
const newName = `Questions[${parent_index}].answers[${name}].content`;
const { formik, isError, errorMsg, t } = useFormField(newName, props);
const TextFilehandleChange = (
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,

View File

@ -30,9 +30,9 @@ const Form = () => {
const handleAddChoice = (parent_index: number) => {
console.log(parent_index);
console.log(formik?.values?.Questions);
formik.setFieldValue(`Questions.[${parent_index}].QuestionOptions`, [
formik.setFieldValue(`Questions.[${parent_index}].answers`, [
...((formik?.values as any)?.Questions?.[parent_index]
.QuestionOptions as Choice[]),
.answers as Choice[]),
{
answer: null,
@ -53,7 +53,7 @@ const Form = () => {
isBase: 0,
max_mark: 1,
min_mark_to_pass: 1,
QuestionOptions: [{ answer: null, answer_image: null, isCorrect: 0 }],
answers: [{ answer: null, answer_image: null, isCorrect: 0 }],
tags: [],
},
]);
@ -103,7 +103,7 @@ const Form = () => {
{(
(formik?.values as any)?.Questions?.[parent_index]
?.QuestionOptions || []
?.answers || []
).map((item: Choice, index: number) => {
return (
<ChoiceFields
@ -114,7 +114,7 @@ const Form = () => {
/>
);
})}
{formik?.values?.Questions?.[parent_index]?.QuestionOptions
{formik?.values?.Questions?.[parent_index]?.answers
?.length < 5 && (
<p className="add_new_button">
<FaCirclePlus

View File

@ -26,10 +26,10 @@ const QuestionFIeld = ({ index, data }: { index: number; data: Choice }) => {
if (DeleteQuestionId?.id) {
setDeletedQuestions([...DeletedQuestions, DeleteQuestionId]);
}
const updatedQuestionOptions = formik.values.Questions.filter(
const updatedAnswers = formik.values.Questions.filter(
(_: any, i: any) => i !== index,
);
formik.setFieldValue(`Questions`, updatedQuestionOptions);
formik.setFieldValue(`Questions`, updatedAnswers);
};
return (

View File

@ -1,33 +0,0 @@
import React, { useState } from "react";
import MathJax from "react-mathjax";
const MathInput: React.FC = () => {
const [input, setInput] = useState<string>(
"a^2+b^2=c^2 (x+a)^n=x=(-b±√(b^2-4ac))/2a ∑_(k=0)^n▒〖(n¦k) x^k a^(n-k) 〗",
);
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const formattedInput = event.target.value.replace("_", " _ ");
console.log(event.target.value);
setInput(formattedInput);
};
return (
<MathJax.Provider>
<div>
<input
type="text"
id="mathInput"
value={input}
onChange={handleChange}
/>
<div>
<MathJax.Node formula={input} />
</div>
</div>
</MathJax.Provider>
);
};
export default MathInput;

View File

@ -28,10 +28,10 @@ const Form = () => {
}, [formik?.values]);
const handleAddChoice = () => {
formik.setFieldValue("QuestionOptions", [
...((formik?.values as any)?.QuestionOptions as Choice[]),
formik.setFieldValue("answers", [
...((formik?.values as any)?.answers as Choice[]),
{
answer: null,
content: null,
answer_image: null,
isCorrect: 0,
},
@ -69,7 +69,7 @@ const Form = () => {
/>
</div>
<Choices />
{formik?.values?.QuestionOptions?.length < 5 && (
{formik?.values?.answers?.length < 5 && (
<p className="add_new_button">
<FaCirclePlus onClick={handleAddChoice} size={23} />{" "}
{t("header.add_new_choice")}

View File

@ -7,7 +7,7 @@ export const getInitialValues = (objectToEdit: Question): any => {
const tags = objectToEdit?.tags?.map((item: any, index: number) => {
return { ...item, key: index };
});
console.log(objectToEdit?.canAnswersBeShuffled);
console.log(objectToEdit);
return {
id: objectToEdit?.id ?? null,
@ -18,7 +18,7 @@ console.log(objectToEdit?.canAnswersBeShuffled);
hint: objectToEdit?.hint ?? "",
isBase: 0,
parent_id: objectToEdit?.parent_id ?? "",
QuestionOptions: objectToEdit?.QuestionOptions ?? [],
answers: objectToEdit?.answers ?? [],
tags: tags ?? [],
};
};
@ -28,10 +28,10 @@ export const getValidationSchema = () => {
return Yup.object().shape({
image: Yup.string().nullable(),
content: Yup.string().required("validation.required"),
QuestionOptions: Yup.array()
answers: Yup.array()
.of(
Yup.object().shape({
answer: Yup.string().required("validation.required"),
content: Yup.string().required("validation.required"),
answer_image: Yup.string().nullable(),
isCorrect: Yup.boolean(),
}),
@ -51,7 +51,7 @@ export const getInitialValuesBase = (objectToEdit: Question): any => {
return {
...item,
canAnswersBeShuffled: objectToEdit?.canAnswersBeShuffled ? 1 : 0,
hint: objectToEdit?.hint ?? "",
hint: item?.hint ?? "",
tags,
};
});
@ -80,10 +80,10 @@ export const getValidationSchemaBase = () => {
Yup.object().shape({
image: Yup.string().nullable(),
content: Yup.string().required("validation.required"),
QuestionOptions: Yup.array()
answers: Yup.array()
.of(
Yup.object().shape({
answer: Yup.string().required("validation.required"),
content: Yup.string().required("validation.required"),
answer_image: Yup.string().nullable(),
isCorrect: Yup.boolean(),
}),

View File

@ -1,9 +1,9 @@
export const BaseURL = "http://192.168.1.111:8000/api/";
export const BaseURL = "http://127.0.0.1:8000/api/";
// export const BaseURL = "http://127.0.0.1:8000/api/";
// export const BaseURL = "http://192.168.1.120:8000/api/";
// export const ImageBaseURL = "http://192.168.1.9:8000/";
export const ImageBaseURL = "http://192.168.1.111:8000/api/";
export const ImageBaseURL = "http://127.0.0.1:8000/api/";
export const HEADER_KEY = "X-Custom-Query-Key";

View File

@ -6,7 +6,7 @@ function QueryProvider({ children }: any) {
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
// staleTime:Infinity
staleTime:60
},
},
});

View File

@ -312,7 +312,7 @@ export interface Question {
image: string | null;
Questions?: any[];
question_options_count?: any;
QuestionOptions: QuestionOption[];
answers: QuestionOption[];
hint?:string;
tags: tags[]; // Assuming tags are strings, adjust as per actual data type
}

View File

@ -15,8 +15,8 @@ export const setLocalStorageBaseQuestions = async (key: string, data: any) => {
}
}
}
if (Array.isArray(data.Questions.QuestionOptions)) {
for (const option of data.Questions.QuestionOptions) {
if (Array.isArray(data.Questions.answers)) {
for (const option of data.Questions.answers) {
if (option.answer_image instanceof File) {
option.answer_image = await convertFileToBase64(option.answer_image);
}
@ -50,8 +50,8 @@ export const getLocalStorageBaseQuestions = (key: string): any | null => {
}
}
}
if (Array.isArray(data.Questions.QuestionOptions)) {
for (const option of data.Questions.QuestionOptions) {
if (Array.isArray(data.Questions.answers)) {
for (const option of data.Questions.answers) {
if (typeof option.answer_image === "string") {
option.answer_image = base64StringToFile(option.answer_image);
}

View File

@ -8,9 +8,9 @@ export const setLocalStorageQuestions = async (key: string, data: any) => {
data.image = await convertFileToBase64(data.image);
}
// Check for QuestionOptions array and convert answer_image if it's a File
if (Array.isArray(data.QuestionOptions)) {
for (const option of data.QuestionOptions) {
// Check for answers array and convert answer_image if it's a File
if (Array.isArray(data.answers)) {
for (const option of data.answers) {
if (option.answer_image instanceof File) {
option.answer_image = await convertFileToBase64(option.answer_image);
}
@ -23,9 +23,9 @@ export const setLocalStorageQuestions = async (key: string, data: any) => {
if (question.image instanceof File) {
question.image = await convertFileToBase64(question.image);
}
// Check for nested QuestionOptions and convert answer_image if it's a File
if (Array.isArray(question.QuestionOptions)) {
for (const option of question.QuestionOptions) {
// Check for nested answers and convert answer_image if it's a File
if (Array.isArray(question.answers)) {
for (const option of question.answers) {
if (option.answer_image instanceof File) {
option.answer_image = await convertFileToBase64(
option.answer_image,
@ -55,9 +55,9 @@ export const getLocalStorageQuestions = (key: string): any | null => {
data.image = base64StringToFile(data.image);
}
// Check for QuestionOptions array and convert answer_image if it's a base64 string
if (Array.isArray(data.QuestionOptions)) {
for (const option of data.QuestionOptions) {
// Check for answers array and convert answer_image if it's a base64 string
if (Array.isArray(data.answers)) {
for (const option of data.answers) {
if (
typeof option.answer_image === "string" &&
option.answer_image.length > 0
@ -73,9 +73,9 @@ export const getLocalStorageQuestions = (key: string): any | null => {
if (typeof question.image === "string" && question.image.length > 0) {
question.image = base64StringToFile(question.image);
}
// Check for nested QuestionOptions and convert answer_image if it's a base64 string
if (Array.isArray(question.QuestionOptions)) {
for (const option of question.QuestionOptions) {
// Check for nested answers and convert answer_image if it's a base64 string
if (Array.isArray(question.answers)) {
for (const option of question.answers) {
if (
typeof option.answer_image === "string" &&
option.answer_image.length > 0