Skip to content

Commit

Permalink
Merge pull request #12099 from KshitijThareja/12012
Browse files Browse the repository at this point in the history
Move handleReplacement logic from ReplaceQuestions.vue to useQuizCreation.js
  • Loading branch information
nucleogenesis authored Apr 23, 2024
2 parents afe5a61 + ecf1ee5 commit 85d7288
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
21 changes: 21 additions & 0 deletions kolibri/plugins/coach/assets/src/composables/useQuizCreation.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export default function useQuizCreation() {
/** @type {ref<Number>} A counter for use in naming new sections */
const _sectionLabelCounter = ref(1);

/** @type {ref<Array>} A list of all Question objects selected for replacement */
const replacements = ref([]);

// ------------------
// Section Management
// ------------------
Expand Down Expand Up @@ -163,6 +166,16 @@ export default function useQuizCreation() {
});
}

function handleReplacement() {
const questionsNotSelectedToBeReplaced = activeQuestions.value.filter(
question => !selectedActiveQuestions.value.includes(question.id)
);
updateSection({
section_id: activeSection.value.section_id,
questions: [...questionsNotSelectedToBeReplaced, ...replacements.value],
});
}

/**
* @description Selects random questions from the active section's `resource_pool` - no side
* effects
Expand Down Expand Up @@ -481,6 +494,7 @@ export default function useQuizCreation() {

provide('saveQuiz', saveQuiz);
provide('updateSection', updateSection);
provide('handleReplacement', handleReplacement);
provide('replaceSelectedQuestions', replaceSelectedQuestions);
provide('addSection', addSection);
provide('removeSection', removeSection);
Expand All @@ -491,6 +505,7 @@ export default function useQuizCreation() {
provide('removeQuestionFromSelection', removeQuestionFromSelection);
provide('clearSelectedQuestions', clearSelectedQuestions);
provide('channels', channels);
provide('replacements', replacements);
provide('quiz', quiz);
provide('allSections', allSections);
provide('activeSection', activeSection);
Expand All @@ -511,6 +526,7 @@ export default function useQuizCreation() {
// Methods
saveQuiz,
updateSection,
handleReplacement,
replaceSelectedQuestions,
addSection,
removeSection,
Expand All @@ -523,6 +539,7 @@ export default function useQuizCreation() {

// Computed
channels,
replacements,
quiz,
allSections,
activeSection,
Expand Down Expand Up @@ -552,6 +569,7 @@ export default function useQuizCreation() {
export function injectQuizCreation() {
const saveQuiz = inject('saveQuiz');
const updateSection = inject('updateSection');
const handleReplacement = inject('handleReplacement');
const replaceSelectedQuestions = inject('replaceSelectedQuestions');
const addSection = inject('addSection');
const removeSection = inject('removeSection');
Expand All @@ -562,6 +580,7 @@ export function injectQuizCreation() {
const removeQuestionFromSelection = inject('removeQuestionFromSelection');
const clearSelectedQuestions = inject('clearSelectedQuestions');
const channels = inject('channels');
const replacements = inject('replacements');
const quiz = inject('quiz');
const allSections = inject('allSections');
const activeSection = inject('activeSection');
Expand All @@ -584,6 +603,7 @@ export function injectQuizCreation() {
deleteActiveSelectedQuestions,
selectAllQuestions,
updateSection,
handleReplacement,
replaceSelectedQuestions,
addSection,
removeSection,
Expand All @@ -599,6 +619,7 @@ export function injectQuizCreation() {
allQuestionsSelected,
selectAllIsIndeterminate,
channels,
replacements,
quiz,
allSections,
activeSection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
:cancelText="coreString('cancelAction')"
:title="replaceQuestions$()"
@cancel="showReplacementConfirmation = false"
@submit="handleReplacement"
@submit="submitReplacement"
>
<div> {{ replaceQuestionsExplaination$() }} </div>
<div style="font-weight: bold;">
Expand Down Expand Up @@ -188,7 +188,6 @@
} = enhancedQuizManagementStrings;
const {
// Computed
activeQuestions,
activeSection,
selectedActiveQuestions,
activeResourceMap,
Expand All @@ -199,6 +198,8 @@
replaceSelectedQuestions,
toggleQuestionInSelection,
updateSection,
handleReplacement,
replacements,
} = injectQuizCreation();
const showCloseConfirmation = ref(false);
Expand All @@ -208,17 +209,9 @@
context.emit('closePanel');
}
const replacements = ref([]);
function handleReplacement() {
function submitReplacement() {
handleReplacement();
const count = replacements.value.length;
const questionsNotSelectedToBeReplaced = activeQuestions.value.filter(
question => !selectedActiveQuestions.value.includes(question.id)
);
updateSection({
section_id: activeSection.value.section_id,
questions: [...questionsNotSelectedToBeReplaced, ...replacements.value],
});
router.replace({
name: PageNames.EXAM_CREATION_ROOT,
query: {
Expand Down Expand Up @@ -263,8 +256,6 @@
return {
toggleInReplacements,
handleReplacement,
replacements,
activeSection,
selectAllReplacementQuestions,
selectedActiveQuestions,
Expand All @@ -283,6 +274,8 @@
isItemExpanded,
toggleQuestionInSelection,
updateSection,
submitReplacement,
replacements,
replaceQuestions$,
deleteSectionLabel$,
replaceAction$,
Expand Down

0 comments on commit 85d7288

Please sign in to comment.