Skip to content

Commit

Permalink
Cyberleague: [premieroctet#152] Add action previous_question
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastien-Wappizy committed Sep 24, 2024
1 parent efd8022 commit efbf8b4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
16 changes: 15 additions & 1 deletion backend/web/server/plugins/cyberleague/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const nextQuestion = async ({ value }, user) => {

const answerIndex = lodash.findIndex(score.answers, (a)=> idEqual(a._id, value))
if (answerIndex +1 == score.answers.length) {
throw new NotFoundError(`Question ${value} is the last of the quiz`)
throw new NotFoundError(`Answer ${value} is the last of the quiz`)
}

return score.answers[answerIndex +1]
Expand All @@ -42,6 +42,20 @@ const finishSurvey = ({ value }, user) => {
//TODO rename action to finish_survey
addAction('smartdiet_finish_survey', finishSurvey)

//value : _id of the answered question
const previousQuestion = async ({ value }, user) => {
const score = await Score.findOne({answers: value}).populate('answers')

const answerIndex = lodash.findIndex(score.answers, (a)=> idEqual(a._id, value))
if (answerIndex == 0) {
throw new NotFoundError(`Answer ${value} is the first of the quiz`)
}

return score.answers[answerIndex -1]
}
//TODO rename action to next_question
addAction('previous_question', previousQuestion)

const isActionAllowed = async ({action, dataId, user, ...rest}) => {
if (lodash.includes([,'smartdiet_next_question','smartdiet_finish_survey','previous_question'])) {

Expand Down
14 changes: 14 additions & 0 deletions studio-test/src/dependencies/utils/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,20 @@ return Promise.allSettled(imagePromises)
}))
},

previous_question: ({value}) => {
let url = `${API_ROOT}/action`
const body = {
action: 'previous_question',
value: value._id,
}
return axios.post(url, body)
.then(res => {
var searchParams = new URLSearchParams(window.location.search);
searchParams.set('id', res.data._id)
window.location.search=searchParams.toString()
})
},

smartdiet_next_question: ({value}) => {
let url = `${API_ROOT}/action`
const body = {
Expand Down
14 changes: 14 additions & 0 deletions studio/src/dependencies/utils/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,20 @@ return Promise.allSettled(imagePromises)
}))
},

previous_question: ({value}) => {
let url = `${API_ROOT}/action`
const body = {
action: 'previous_question',
value: value._id,
}
return axios.post(url, body)
.then(res => {
var searchParams = new URLSearchParams(window.location.search);
searchParams.set('id', res.data._id)
window.location.search=searchParams.toString()
})
},

smartdiet_next_question: ({value}) => {
let url = `${API_ROOT}/action`
const body = {
Expand Down
6 changes: 6 additions & 0 deletions studio/src/utils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,12 @@ export const ACTIONS: IActions = {
next: ['openPage'],
},

previous_question: {
label: 'Previous question',
options: {},
next: ['openPage'],
},

smartdiet_next_question: {
label: 'Next question',
options: {},
Expand Down

0 comments on commit efbf8b4

Please sign in to comment.