fix: fetch question details

This commit is contained in:
Jannat Patel
2024-08-01 13:00:37 +05:30
parent b8c3bdc0b4
commit 471e7d9229
6 changed files with 105 additions and 28 deletions

View File

@@ -49,10 +49,11 @@
</Dialog>
</template>
<script setup>
import { Dialog, FormControl, TextEditor } from 'frappe-ui'
import { computed, onMounted, reactive } from 'vue'
import { Dialog, FormControl, TextEditor, createResource } from 'frappe-ui'
import { computed, onMounted, reactive, inject } from 'vue'
const show = defineModel()
const user = inject('$user')
const question = reactive({
question: '',
type: 'Choices',
@@ -60,7 +61,19 @@ const question = reactive({
onMounted(() => {
populateFields()
populateQuestionDetail()
console.log(props.questionName)
if (
props.questionName == 'new' &&
!user.data?.is_moderator &&
!user.data?.is_instructor
) {
router.push({ name: 'Courses' })
}
if (props.courseName !== 'new') {
questionDoc.reload()
}
window.addEventListener('keydown', keyboardShortcut)
})
const props = defineProps({
@@ -68,9 +81,29 @@ const props = defineProps({
type: String,
default: __('Add a Question'),
},
questionDetail: {
type: Object,
default: {},
questionName: {
type: String,
},
})
const questionDoc = createResource({
url: 'frappe.client.get',
makeParams: (values) => {
return {
doctype: 'LMS Question',
name: props.questionName,
}
},
onSuccess(data) {
let counter = 1
Object.keys(data).forEach((key) => {
if (Object.hasOwn(question, key)) question[key] = data[key]
})
while (counter <= 4) {
question[`is_correct_${counter}`] = question[`is_correct_${counter}`]
? true
: false
}
},
})
@@ -85,13 +118,14 @@ const populateFields = () => {
})
}
const populateQuestionDetail = () => {
if (Object.keys(props.questionDetail).length) {
Object.keys(props.questionDetail).forEach((key) => {
if (Object.hasOwn(question, key)) {
question[key] = props.questionDetail[key]
}
})
const keyboardShortcut = (e) => {
if (
e.key === 's' &&
(e.ctrlKey || e.metaKey) &&
!e.target.classList.contains('ProseMirror')
) {
submitQuestion()
e.preventDefault()
}
}

View File

@@ -75,7 +75,6 @@
row-key="name"
:options="{
showTooltip: false,
onRowClick: (row) => emit('openQuestionModal', row.name),
}"
>
<ListHeader
@@ -88,9 +87,16 @@
:row="row"
v-slot="{ idx, column, item }"
v-for="row in quiz.questions"
@click="openQuestionModal(row.question)"
>
<ListRowItem :item="item">
<div class="text-xs">
<div
v-if="column.key == 'question_detail'"
class="text-xs truncate"
>
{{ item }}
</div>
<div v-else class="text-xs">
{{ item }}
</div>
</ListRowItem>
@@ -99,7 +105,7 @@
</ListView>
</div>
</div>
<Question v-model="showQuestionModal" :question="currentQuestion" />
<Question v-model="showQuestionModal" :questionName="currentQuestion" />
</template>
<script setup>
import {
@@ -144,7 +150,7 @@ const quizDetails = createDocumentResource({
doctype: 'LMS Quiz',
name: props.quizID,
auto: true,
cache: ['quiz', props.quiz],
cache: ['quiz', props.quizID],
onSuccess(data) {
Object.keys(data).forEach((key) => {
if (Object.hasOwn(quiz, key)) quiz[key] = data[key]
@@ -162,29 +168,29 @@ const quizDetails = createDocumentResource({
},
})
console.log(quizDetails)
const questionColumns = computed(() => {
return [
{
label: __('ID'),
key: 'question',
width: 1,
width: '25%',
},
{
label: __('Question'),
key: __('question_detail'),
width: 3,
width: '60%',
},
{
label: __('Marks'),
key: 'marks',
width: 0.5,
width: '10%',
},
]
})
const openQuestionModal = (question = {}) => {
const openQuestionModal = (question = null) => {
console.log('called')
console.log(question)
currentQuestion.value = question
showQuestionModal.value = true
}