fix
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<div class="exam-item__sub">
|
||||
<span class="exam-item__sub-label">دوره:</span>
|
||||
<span class="exam-item__sub-value">
|
||||
{{ exam.courseTemplate?.title || exam.courseTemplateTitle || '—' }}
|
||||
{{ exam.course?.title || exam.courseTitle || '—' }}
|
||||
</span>
|
||||
<span class="exam-item__dot">|</span>
|
||||
<span class="exam-item__sub-label">جلسه:</span>
|
||||
|
||||
@@ -4,12 +4,16 @@
|
||||
v-for="(question, questionIndex) in questions"
|
||||
:key="question.id"
|
||||
class="exam-question-builder__card"
|
||||
:class="{ 'exam-question-builder__card--readonly': isLocked(question) }"
|
||||
>
|
||||
<div class="exam-question-builder__head">
|
||||
<p class="exam-question-builder__title">سوال شماره {{ questionIndex + 1 }}</p>
|
||||
<p class="exam-question-builder__title">
|
||||
سوال شماره {{ questionIndex + 1 }}
|
||||
<span v-if="isLocked(question)" class="exam-question-builder__lock">(ذخیره شده)</span>
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="disabled"
|
||||
:disabled="disabled || isLocked(question)"
|
||||
class="exam-question-builder__remove"
|
||||
aria-label="حذف سوال"
|
||||
@click="removeQuestion(question.id)"
|
||||
@@ -22,24 +26,24 @@
|
||||
<div class="exam-question-builder__col exam-question-builder__col--main">
|
||||
<label class="exam-question-builder__label">متن سوال</label>
|
||||
<textarea
|
||||
:value="question.title"
|
||||
:disabled="disabled"
|
||||
:value="question.questionText"
|
||||
:disabled="disabled || isLocked(question)"
|
||||
rows="1"
|
||||
placeholder="لطفا سوال خود را وارد کنید"
|
||||
class="exam-question-builder__textarea"
|
||||
@input="updateQuestion(question.id, { title: $event.target.value })"
|
||||
@input="updateQuestion(question.id, { questionText: $event.target.value })"
|
||||
/>
|
||||
</div>
|
||||
<div class="exam-question-builder__col exam-question-builder__col--score">
|
||||
<label class="exam-question-builder__label">بارم نمره</label>
|
||||
<label class="exam-question-builder__label">ترتیب</label>
|
||||
<input
|
||||
:value="question.score"
|
||||
:disabled="disabled"
|
||||
:value="question.position"
|
||||
:disabled="disabled || isLocked(question)"
|
||||
type="text"
|
||||
inputmode="numeric"
|
||||
placeholder="15"
|
||||
placeholder="1"
|
||||
class="exam-question-builder__input"
|
||||
@input="updateQuestion(question.id, { score: $event.target.value })"
|
||||
@input="updateQuestion(question.id, { position: $event.target.value })"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -49,31 +53,31 @@
|
||||
<p class="exam-question-builder__label">گزینهها</p>
|
||||
<div class="exam-question-builder__answers-list">
|
||||
<div
|
||||
v-for="answer in question.answers"
|
||||
:key="answer.id"
|
||||
v-for="option in question.options"
|
||||
:key="option.id"
|
||||
class="exam-question-builder__answer"
|
||||
>
|
||||
<input
|
||||
:value="answer.title"
|
||||
:disabled="disabled"
|
||||
:value="option.optionText"
|
||||
:disabled="disabled || isLocked(question)"
|
||||
type="text"
|
||||
placeholder="متن گزینه"
|
||||
class="exam-question-builder__answer-input"
|
||||
@input="updateOption(question.id, answer.id, $event.target.value)"
|
||||
@input="updateOption(question.id, option.id, $event.target.value)"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="disabled"
|
||||
:disabled="disabled || isLocked(question)"
|
||||
class="exam-question-builder__answer-remove"
|
||||
aria-label="حذف گزینه"
|
||||
@click="removeOption(question.id, answer.id)"
|
||||
@click="removeOption(question.id, option.id)"
|
||||
>
|
||||
<SvgIcon name="close" :size="14" color="#b1b1b1" />
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="disabled"
|
||||
:disabled="disabled || isLocked(question)"
|
||||
class="exam-question-builder__answer-add"
|
||||
aria-label="افزودن گزینه"
|
||||
@click="addOption(question.id)"
|
||||
@@ -85,16 +89,14 @@
|
||||
|
||||
<div class="exam-question-builder__correct">
|
||||
<SelectField
|
||||
:model-value="question.correctAnswerId"
|
||||
:model-value="correctOptionId(question)"
|
||||
:name="`correctAnswer-${question.id}`"
|
||||
:options="correctAnswerOptions(question)"
|
||||
:options="correctOptions(question)"
|
||||
option-label="label"
|
||||
option-value="value"
|
||||
label="گزینه صحیح"
|
||||
:disabled="disabled || question.answers.length === 0"
|
||||
@update:model-value="
|
||||
(value) => updateQuestion(question.id, { correctAnswerId: value || null })
|
||||
"
|
||||
:disabled="disabled || isLocked(question) || question.options.length === 0"
|
||||
@update:model-value="(value) => setCorrectOption(question.id, value)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -128,22 +130,26 @@ const defaultLabels = ['گزینه اول', 'گزینه دوم', 'گزینه س
|
||||
|
||||
const questions = computed(() => (Array.isArray(props.modelValue) ? props.modelValue : []))
|
||||
|
||||
// Backend has no PATCH/DELETE for questions/options — once a question came
|
||||
// from the server (numeric id, no `__local`), the form locks editing it.
|
||||
const isLocked = (question) => question?.__local !== true
|
||||
|
||||
const cloneQuestions = () =>
|
||||
questions.value.map((q) => ({
|
||||
...q,
|
||||
answers: Array.isArray(q.answers) ? q.answers.map((a) => ({ ...a })) : [],
|
||||
options: Array.isArray(q.options) ? q.options.map((o) => ({ ...o })) : [],
|
||||
}))
|
||||
|
||||
const createId = (prefix) => `${prefix}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
||||
|
||||
const createOption = () => ({ id: createId('answer'), title: '' })
|
||||
const createOption = () => ({ id: createId('option'), optionText: '', isCorrect: false })
|
||||
|
||||
const createQuestion = () => ({
|
||||
id: createId('question'),
|
||||
title: '',
|
||||
score: '',
|
||||
correctAnswerId: null,
|
||||
answers: [createOption(), createOption()],
|
||||
questionText: '',
|
||||
position: questions.value.length + 1,
|
||||
options: [createOption(), createOption()],
|
||||
__local: true,
|
||||
})
|
||||
|
||||
const emitQuestions = (next) => emit('update:modelValue', next)
|
||||
@@ -163,39 +169,48 @@ const updateQuestion = (questionId, patch) => {
|
||||
const addOption = (questionId) => {
|
||||
const next = cloneQuestions().map((q) => {
|
||||
if (q.id !== questionId) return q
|
||||
return { ...q, answers: [...q.answers, createOption()] }
|
||||
return { ...q, options: [...q.options, createOption()] }
|
||||
})
|
||||
emitQuestions(next)
|
||||
}
|
||||
|
||||
const removeOption = (questionId, answerId) => {
|
||||
const removeOption = (questionId, optionId) => {
|
||||
const next = cloneQuestions().map((q) => {
|
||||
if (q.id !== questionId) return q
|
||||
const options = q.options.filter((o) => o.id !== optionId)
|
||||
return { ...q, options }
|
||||
})
|
||||
emitQuestions(next)
|
||||
}
|
||||
|
||||
const updateOption = (questionId, optionId, optionText) => {
|
||||
const next = cloneQuestions().map((q) => {
|
||||
if (q.id !== questionId) return q
|
||||
const answers = q.answers.filter((a) => a.id !== answerId)
|
||||
return {
|
||||
...q,
|
||||
answers,
|
||||
correctAnswerId: String(q.correctAnswerId) === String(answerId) ? null : q.correctAnswerId,
|
||||
options: q.options.map((o) => (o.id === optionId ? { ...o, optionText } : o)),
|
||||
}
|
||||
})
|
||||
emitQuestions(next)
|
||||
}
|
||||
|
||||
const updateOption = (questionId, answerId, title) => {
|
||||
const setCorrectOption = (questionId, optionId) => {
|
||||
const next = cloneQuestions().map((q) => {
|
||||
if (q.id !== questionId) return q
|
||||
return {
|
||||
...q,
|
||||
answers: q.answers.map((a) => (a.id === answerId ? { ...a, title } : a)),
|
||||
options: q.options.map((o) => ({ ...o, isCorrect: String(o.id) === String(optionId) })),
|
||||
}
|
||||
})
|
||||
emitQuestions(next)
|
||||
}
|
||||
|
||||
const correctOptionId = (question) => question.options.find((o) => o.isCorrect)?.id ?? null
|
||||
|
||||
const optionLabel = (index) => defaultLabels[index] || `گزینه ${index + 1}`
|
||||
|
||||
const correctAnswerOptions = (question) =>
|
||||
question.answers.map((a, idx) => ({ value: a.id, label: optionLabel(idx) }))
|
||||
const correctOptions = (question) =>
|
||||
question.options.map((o, idx) => ({ value: o.id, label: optionLabel(idx) }))
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
@@ -209,6 +224,10 @@ const correctAnswerOptions = (question) =>
|
||||
border-radius: 1.5rem;
|
||||
padding: 0.875rem;
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 2.5%);
|
||||
|
||||
&--readonly {
|
||||
background: rgba(0, 0, 0, 2%);
|
||||
}
|
||||
}
|
||||
|
||||
&__head {
|
||||
@@ -227,6 +246,12 @@ const correctAnswerOptions = (question) =>
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
&__lock {
|
||||
font-size: 0.7rem;
|
||||
color: #9c9c9c;
|
||||
margin-inline-start: 0.25rem;
|
||||
}
|
||||
|
||||
&__remove {
|
||||
width: 2rem;
|
||||
height: 2rem;
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
</template>
|
||||
</TextField>
|
||||
<SelectField
|
||||
v-model="form.courseTemplateId"
|
||||
name="courseTemplateId"
|
||||
label="دوره الگو"
|
||||
v-model="form.courseId"
|
||||
name="courseId"
|
||||
label="دوره"
|
||||
:options="templateOptions"
|
||||
option-label="title"
|
||||
option-value="id"
|
||||
@@ -30,7 +30,7 @@
|
||||
@click="onReset"
|
||||
>
|
||||
<template #icon>
|
||||
<SvgIcon name="close" :size="20" />
|
||||
<SvgIcon name="close" color="black" :size="20" />
|
||||
</template>
|
||||
</CircleButton>
|
||||
<CircleButton
|
||||
@@ -55,18 +55,18 @@ import TextField from '@/components/form/TextField.vue'
|
||||
import CircleButton from '@/components/CircleButton.vue'
|
||||
import SelectField from '@/components/form/SelectField.vue'
|
||||
import DatePickerField from '@/components/form/DatePickerField.vue'
|
||||
import { useAdminCourseTemplatesListQuery } from '@/services/query/admin-course-templates'
|
||||
import { useAdminCoursesListQuery } from '@/services/query/admin-courses'
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: {
|
||||
type: Object,
|
||||
default: () => ({ title: '', courseTemplateId: '', fromDate: '', toDate: '' }),
|
||||
default: () => ({ title: '', courseId: '', fromDate: '', toDate: '' }),
|
||||
},
|
||||
})
|
||||
|
||||
const emit = defineEmits(['update:modelValue', 'apply', 'reset'])
|
||||
|
||||
const emptyForm = () => ({ title: '', courseTemplateId: '', fromDate: '', toDate: '' })
|
||||
const emptyForm = () => ({ title: '', courseId: '', fromDate: '', toDate: '' })
|
||||
const form = ref({ ...emptyForm(), ...props.modelValue })
|
||||
|
||||
watch(
|
||||
@@ -82,10 +82,7 @@ const todayIso = new Date().toISOString()
|
||||
const templateSearch = ref('')
|
||||
const templateFilters = computed(() => ({ title: templateSearch.value }))
|
||||
const templatePagination = ref({ page: 1, perPage: 30 })
|
||||
const { data: templatesResponse } = useAdminCourseTemplatesListQuery(
|
||||
templateFilters,
|
||||
templatePagination
|
||||
)
|
||||
const { data: templatesResponse } = useAdminCoursesListQuery(templateFilters, templatePagination)
|
||||
const templateOptions = computed(() => templatesResponse.value?.data ?? [])
|
||||
|
||||
const searchTemplates = useDebounce((q) => {
|
||||
|
||||
@@ -102,13 +102,13 @@ const summaryItems = computed(() => {
|
||||
},
|
||||
{
|
||||
title: 'دوره مرتبط',
|
||||
value: e.courseTemplate?.title || e.courseTemplateTitle || '—',
|
||||
value: e.course?.title || e.courseTitle || '—',
|
||||
numeric: false,
|
||||
},
|
||||
{ title: 'وضعیت', value: e.statusLabel || e.faStatus || e.status || '—', numeric: false },
|
||||
{
|
||||
title: 'مدت زمان',
|
||||
value: e.durationMinutes == null ? '—' : `${e.durationMinutes} دقیقه`,
|
||||
title: 'حد نصاب قبولی',
|
||||
value: e.passScore == null ? '—' : `${e.passScore} نمره`,
|
||||
numeric: true,
|
||||
},
|
||||
{
|
||||
@@ -127,25 +127,17 @@ const summaryItems = computed(() => {
|
||||
const displayQuestions = computed(() => {
|
||||
const raw = exam.value?.questions || []
|
||||
return raw.map((question, index) => {
|
||||
const answersSource =
|
||||
(Array.isArray(question.answers) && question.answers) ||
|
||||
(Array.isArray(question.options) && question.options) ||
|
||||
[]
|
||||
const answers = answersSource.map((answer, ai) => {
|
||||
const id = answer?.id ?? `${question?.id || index + 1}-${ai + 1}`
|
||||
const title =
|
||||
(typeof answer === 'string' && answer) ||
|
||||
answer?.title ||
|
||||
answer?.text ||
|
||||
answer?.label ||
|
||||
'—'
|
||||
return { id, title, isCorrect: String(id) === String(question?.correctAnswerId) }
|
||||
})
|
||||
const optionsSource = Array.isArray(question.options) ? question.options : []
|
||||
const answers = optionsSource.map((option, ai) => ({
|
||||
id: option?.id ?? `${question?.id || index + 1}-${ai + 1}`,
|
||||
title: option?.optionText || '—',
|
||||
isCorrect: !!option?.isCorrect,
|
||||
}))
|
||||
return {
|
||||
id: question?.id ?? `question-${index + 1}`,
|
||||
order: index + 1,
|
||||
title: question?.title || question?.question || '—',
|
||||
score: question?.score ?? '',
|
||||
order: question?.position ?? index + 1,
|
||||
title: question?.questionText || '—',
|
||||
score: '',
|
||||
answers,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -31,32 +31,17 @@
|
||||
:error="errors.title"
|
||||
@blur="validateAt('title', form.title)"
|
||||
/>
|
||||
<TextField
|
||||
v-model="form.durationMinutes"
|
||||
name="durationMinutes"
|
||||
label="مدت آزمون"
|
||||
inputmode="numeric"
|
||||
:convert-digits="true"
|
||||
:error="errors.durationMinutes"
|
||||
@blur="validateAt('durationMinutes', form.durationMinutes)"
|
||||
/>
|
||||
<TextField
|
||||
v-model="form.passingScore"
|
||||
name="passingScore"
|
||||
label="حداقل نمره قبولی"
|
||||
label="حد نصاب قبولی"
|
||||
inputmode="numeric"
|
||||
:convert-digits="true"
|
||||
:error="errors.passingScore"
|
||||
@blur="validateAt('passingScore', form.passingScore)"
|
||||
/>
|
||||
<DatePickerField
|
||||
v-model="form.endDate"
|
||||
name="endDate"
|
||||
label="تاریخ اعتبار"
|
||||
:error="errors.endDate"
|
||||
/>
|
||||
<div class="exam-form__toggle-cell">
|
||||
<ToggleSwitch v-model="form.randomize" label="به صورت رندوم باشد" />
|
||||
<ToggleSwitch v-model="form.isActive" label="آزمون فعال باشد" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -116,13 +101,13 @@ import { examSchema } from '@/features/admin/exams/schema'
|
||||
import SelectField from '@/components/form/SelectField.vue'
|
||||
import ToggleSwitch from '@/components/form/ToggleSwitch.vue'
|
||||
import TextareaField from '@/components/form/TextareaField.vue'
|
||||
import DatePickerField from '@/components/form/DatePickerField.vue'
|
||||
import { useAdminSessionsListQuery } from '@/services/query/admin-sessions'
|
||||
import BoxedIconTitleBlock from '@/components/blocks/BoxedIconTitleBlock.vue'
|
||||
import ExamQuestionBuilder from '@/features/admin/exams/components/ExamQuestionBuilder.vue'
|
||||
import {
|
||||
adminExamsKeys,
|
||||
useAddAdminExamMutation,
|
||||
useAddAdminExamQuestionMutation,
|
||||
useAdminExamQuery,
|
||||
useUpdateAdminExamMutation,
|
||||
} from '@/services/query/admin-exams'
|
||||
@@ -137,32 +122,27 @@ const isEditMode = computed(() => !!examId.value)
|
||||
const form = ref({
|
||||
title: '',
|
||||
sessionId: '',
|
||||
endDate: '',
|
||||
durationMinutes: '',
|
||||
passingScore: '',
|
||||
randomize: true,
|
||||
isActive: true,
|
||||
description: '',
|
||||
})
|
||||
|
||||
const createId = (prefix) => `${prefix}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
||||
|
||||
const blankOption = () => ({ id: createId('option'), optionText: '', isCorrect: false })
|
||||
|
||||
const blankQuestion = () => ({
|
||||
id: createId('question'),
|
||||
title: '',
|
||||
score: '',
|
||||
correctAnswerId: null,
|
||||
answers: [
|
||||
{ id: createId('answer'), title: '' },
|
||||
{ id: createId('answer'), title: '' },
|
||||
],
|
||||
questionText: '',
|
||||
position: 1,
|
||||
options: [blankOption(), { ...blankOption(), id: createId('option') }],
|
||||
__local: true,
|
||||
})
|
||||
|
||||
const questions = ref([blankQuestion()])
|
||||
const questionError = ref('')
|
||||
|
||||
const schema = examSchema
|
||||
|
||||
const { validate, validateAt, errors } = useYup(schema)
|
||||
const { validate, validateAt, errors } = useYup(examSchema)
|
||||
|
||||
const sessionSearch = ref('')
|
||||
const sessionFilters = computed(() => ({ title: sessionSearch.value }))
|
||||
@@ -185,20 +165,20 @@ const { data: existingExam } = useAdminExamQuery(examId, {
|
||||
enabled: () => !!examId.value,
|
||||
})
|
||||
|
||||
const normalizeQuestions = (raw = []) => {
|
||||
const normalizeExistingQuestions = (raw = []) => {
|
||||
if (!Array.isArray(raw) || raw.length === 0) return [blankQuestion()]
|
||||
return raw.map((q, qIdx) => {
|
||||
const answersSrc = q.answers || q.options || q.choices || []
|
||||
const answers = (Array.isArray(answersSrc) ? answersSrc : []).map((a, aIdx) => ({
|
||||
id: a?.id || createId(`answer-${qIdx}-${aIdx}`),
|
||||
title: typeof a === 'string' ? a : a?.title || a?.text || a?.label || '',
|
||||
}))
|
||||
const options = Array.isArray(q.options) ? q.options : []
|
||||
return {
|
||||
id: q.id || createId(`question-${qIdx}`),
|
||||
title: q.title || q.question || q.text || '',
|
||||
score: q.score ?? q.barom ?? '',
|
||||
correctAnswerId: q.correctAnswerId || q.correctOptionId || q.correctAnswer?.id || null,
|
||||
answers: answers.length > 0 ? answers : blankQuestion().answers,
|
||||
id: q.id ?? createId(`question-${qIdx}`),
|
||||
questionText: q.questionText || '',
|
||||
position: q.position ?? qIdx + 1,
|
||||
options: options.map((o, oIdx) => ({
|
||||
id: o.id ?? createId(`option-${qIdx}-${oIdx}`),
|
||||
optionText: o.optionText || '',
|
||||
isCorrect: !!o.isCorrect,
|
||||
})),
|
||||
// No `__local` flag — these came from the server, so the builder will lock them.
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -209,62 +189,89 @@ watch(existingExam, (exam) => {
|
||||
form.value = {
|
||||
title: exam.title || '',
|
||||
sessionId: exam.session?.id || exam.sessionId || '',
|
||||
endDate: exam.endDate || '',
|
||||
durationMinutes: exam.durationMinutes ?? '',
|
||||
passingScore: exam.passingScore ?? '',
|
||||
randomize: exam.randomize ?? true,
|
||||
passingScore: exam.passScore ?? '',
|
||||
isActive: exam.isActive ?? true,
|
||||
description: exam.description || '',
|
||||
}
|
||||
questions.value = normalizeQuestions(exam.questions)
|
||||
questions.value = normalizeExistingQuestions(exam.questions)
|
||||
})
|
||||
|
||||
const validateQuestionList = () => {
|
||||
const list = questions.value
|
||||
if (list.some((q) => !String(q.title || '').trim() || !String(q.score || '').trim())) {
|
||||
questionError.value = 'لطفا متن سوال و بارم هر سوال را وارد کنید.'
|
||||
const validateLocalQuestions = () => {
|
||||
const localOnes = questions.value.filter((q) => q.__local === true)
|
||||
if (!isEditMode.value && localOnes.length === 0) {
|
||||
questionError.value = 'حداقل یک سوال اضافه کنید.'
|
||||
return null
|
||||
}
|
||||
if (list.some((q) => q.answers.filter((a) => a.title?.trim()).length < 2)) {
|
||||
questionError.value = 'برای هر سوال حداقل دو گزینه وارد کنید.'
|
||||
return null
|
||||
}
|
||||
if (
|
||||
list.some((q) => {
|
||||
if (!q.correctAnswerId) return true
|
||||
return !q.answers.some((a) => String(a.id) === String(q.correctAnswerId) && a.title?.trim())
|
||||
})
|
||||
) {
|
||||
questionError.value = 'گزینه صحیح هر سوال را از گزینههای موجود انتخاب کنید.'
|
||||
return null
|
||||
for (const q of localOnes) {
|
||||
if (!String(q.questionText || '').trim()) {
|
||||
questionError.value = 'متن همه سوالات را وارد کنید.'
|
||||
return null
|
||||
}
|
||||
const validOptions = q.options.filter((o) => String(o.optionText || '').trim())
|
||||
if (validOptions.length < 2) {
|
||||
questionError.value = 'برای هر سوال حداقل دو گزینه وارد کنید.'
|
||||
return null
|
||||
}
|
||||
if (!validOptions.some((o) => o.isCorrect)) {
|
||||
questionError.value = 'گزینه صحیح هر سوال را انتخاب کنید.'
|
||||
return null
|
||||
}
|
||||
}
|
||||
questionError.value = ''
|
||||
return list.map((q) => ({
|
||||
id: q.id,
|
||||
title: q.title.trim(),
|
||||
score: q.score,
|
||||
correctAnswerId: q.correctAnswerId,
|
||||
answers: q.answers.filter((a) => a.title?.trim()).map((a) => ({ id: a.id, title: a.title })),
|
||||
return localOnes.map((q, idx) => ({
|
||||
questionText: q.questionText.trim(),
|
||||
position: Number(q.position) || idx + 1,
|
||||
options: q.options
|
||||
.filter((o) => String(o.optionText || '').trim())
|
||||
.map((o) => ({
|
||||
optionText: o.optionText.trim(),
|
||||
isCorrect: !!o.isCorrect,
|
||||
})),
|
||||
}))
|
||||
}
|
||||
|
||||
const addMutation = useAddAdminExamMutation()
|
||||
const updateMutation = useUpdateAdminExamMutation()
|
||||
const buildExamPayload = (values) => ({
|
||||
sessionId: values.sessionId,
|
||||
title: values.title,
|
||||
description: values.description,
|
||||
passingScore: Number(values.passingScore) || 0,
|
||||
isActive: values.isActive,
|
||||
})
|
||||
|
||||
const submitting = computed(() => addMutation.isPending.value || updateMutation.isPending.value)
|
||||
const addExamMutation = useAddAdminExamMutation()
|
||||
const updateExamMutation = useUpdateAdminExamMutation()
|
||||
const addQuestionMutation = useAddAdminExamQuestionMutation()
|
||||
|
||||
const submitting = computed(
|
||||
() =>
|
||||
addExamMutation.isPending.value ||
|
||||
updateExamMutation.isPending.value ||
|
||||
addQuestionMutation.isPending.value
|
||||
)
|
||||
|
||||
const postQuestionsSequentially = async (id, list) => {
|
||||
for (const payload of list) {
|
||||
// Sequential so question position ordering is preserved on the backend.
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await addQuestionMutation.mutateAsync({ examId: id, payload })
|
||||
}
|
||||
}
|
||||
|
||||
const onSubmit = async () => {
|
||||
const { isValid, payload } = await validate(form.value)
|
||||
const cleanQuestions = validateQuestionList()
|
||||
if (!isValid || !cleanQuestions) return
|
||||
const finalPayload = {
|
||||
...payload,
|
||||
questions: cleanQuestions,
|
||||
questionsCount: cleanQuestions.length,
|
||||
}
|
||||
const { isValid } = await validate(form.value)
|
||||
const newQuestions = validateLocalQuestions()
|
||||
if (!isValid || !newQuestions) return
|
||||
|
||||
const examPayload = buildExamPayload(form.value)
|
||||
let targetExamId = examId.value
|
||||
if (isEditMode.value) {
|
||||
await updateMutation.mutateAsync({ id: examId.value, payload: finalPayload })
|
||||
await updateExamMutation.mutateAsync({ id: targetExamId, payload: examPayload })
|
||||
} else {
|
||||
await addMutation.mutateAsync(finalPayload)
|
||||
const created = await addExamMutation.mutateAsync(examPayload)
|
||||
targetExamId = created?.data?.id ?? created?.id ?? targetExamId
|
||||
}
|
||||
if (targetExamId && newQuestions.length > 0) {
|
||||
await postQuestionsSequentially(targetExamId, newQuestions)
|
||||
}
|
||||
await queryClient.invalidateQueries({ queryKey: adminExamsKeys.all })
|
||||
router.push({ name: 'admin-exams' })
|
||||
@@ -306,7 +313,7 @@ const onCancel = () => router.push({ name: 'admin-exams' })
|
||||
}
|
||||
|
||||
@media (min-width: 1280px) {
|
||||
grid-template-columns: repeat(6, 1fr);
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ const router = useRouter()
|
||||
const queryClient = useQueryClient()
|
||||
const { openModal, isModal } = useModal()
|
||||
|
||||
const filters = ref({ title: '', courseTemplateId: '', fromDate: '', toDate: '' })
|
||||
const filters = ref({ title: '', courseId: '', fromDate: '', toDate: '' })
|
||||
const { pagination, setPage, reset: resetPagination } = usePagination({ page: 1, perPage: 10 })
|
||||
|
||||
const { data, isLoading } = useAdminExamsListQuery(filters, pagination, {
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { number, object, string } from 'yup'
|
||||
import { boolean, number, object, string } from 'yup'
|
||||
|
||||
export const examSchema = object().shape({
|
||||
title: string().required().min(3),
|
||||
sessionId: string().required(),
|
||||
endDate: string().required(),
|
||||
durationMinutes: number().typeError('مدت زمان باید عدد باشد').required(),
|
||||
passingScore: number().typeError('حد نصاب قبولی باید عدد باشد').required(),
|
||||
description: string().nullable().notRequired(),
|
||||
isActive: boolean().nullable().notRequired(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user