PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / assets / src / apps / js / admin / react / question / store / reducer.js

reducer.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.1, at assets/src/apps/js/admin/react/question/store/reducer.js

83 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import {combineReducers} from '@wordpress/data';
2
3 const {omit, flow, isArray, remove, get, set} = lodash;
4 const STORE_DATA = {
5 question: false
6 };
7
8 /**
9 * Remove an option from question answers.
10 *
11 * @param {object} state
12 * @param {int} optionId
13 * @param {int} id
14 */
15 const removeOptionById = function removeOptionById(state, optionId, id) {
16 const {question: {blankOptions}} = state;
17
18 remove(blankOptions, function (a) {
19 return a.question_answer_id == optionId;
20 });
21
22 return blankOptions;
23 };
24
25 const setData = function setData(state, data, key) {
26 if (!key) {
27 return {
28 ...state,
29 ...data
30 }
31 }
32
33 const oldData = get(state, key);
34 set(state, key, {...oldData, ...data});
35
36 return {...state};
37 };
38
39 const updateOptionById = function updateOptionById(state, option, optionId, id) {
40 const {question: {blankOptions}} = state;
41
42 return blankOptions.map((opt) => {
43 return opt.question_answer_id == optionId ? {
44 ...opt,
45 ...option
46 } : opt;
47 });
48 };
49
50 export const storeData = (state = STORE_DATA, action) => {
51 switch (action.type) {
52 case 'SET_DATA':
53 return setData(state, action.data, action.key);
54 case 'ADD_OPTION':
55 return {
56 ...state,
57 question: {
58 ...state.question,
59 blankOptions: [...state.question.blankOptions, action.option]
60 }
61 }
62 case 'REMOVE_OPTION':
63 return {
64 ...state,
65 question: {
66 ...state.question,
67 blankOptions: [...removeOptionById(state, action.optionId, action.id)]
68 }
69 }
70 case 'UPDATE_OPTION':
71 return {
72 ...state,
73 question: {
74 ...state.question,
75 blankOptions: [...updateOptionById(state, action.option, action.optionId, action.id)]
76 }
77 }
78 }
79 return state;
80 };
81
82 export default storeData;
83