Skip to content
This repository was archived by the owner on Jul 13, 2021. It is now read-only.

Commit ce66dc9

Browse files
ngodfraindLaurentGruber
authored andcommitted
[ExoBundle] Fixing delete for duplicate step item. (#2365)
* [ExoBundle] Fixing delete for duplicate step item. * [ExoBundle] Fixing step removal. * tests
1 parent ba79bb4 commit ce66dc9

File tree

6 files changed

+53
-13
lines changed

6 files changed

+53
-13
lines changed

plugin/exo/Resources/modules/quiz/editor/actions.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import {actions as modalActions} from '#/main/core/layout/modal/actions'
77
import {tex} from '#/main/core/translation'
88
import {MODAL_MESSAGE} from '#/main/core/layout/modal'
99
import {denormalize} from './../normalizer'
10+
import forOwn from 'lodash/forOwn'
1011

1112
export const ITEM_CREATE = 'ITEM_CREATE'
1213
export const ITEM_UPDATE = 'ITEM_UPDATE'
1314
export const ITEM_DELETE = 'ITEM_DELETE'
1415
export const ITEM_MOVE = 'ITEM_MOVE'
1516
export const ITEM_HINTS_UPDATE = 'ITEM_HINTS_UPDATE'
1617
export const ITEM_DETAIL_UPDATE = 'ITEM_DETAIL_UPDATE'
17-
export const ITEMS_DELETE = 'ITEMS_DELETE'
1818
export const ITEMS_IMPORT = 'ITEMS_IMPORT'
1919
export const OBJECT_NEXT = 'OBJECT_NEXT'
2020
export const OBJECT_SELECT = 'OBJECT_SELECT'
@@ -24,6 +24,7 @@ export const STEP_CREATE = 'STEP_CREATE'
2424
export const STEP_DELETE = 'STEP_DELETE'
2525
export const STEP_UPDATE = 'STEP_UPDATE'
2626
export const STEP_MOVE = 'STEP_MOVE'
27+
export const STEP_ITEM_DELETE = 'STEP_ITEM_DELETE'
2728
export const QUIZ_UPDATE = 'QUIZ_UPDATE'
2829
export const HINT_ADD = 'HINT_ADD'
2930
export const HINT_CHANGE = 'HINT_CHANGE'
@@ -55,6 +56,7 @@ export const quizChangeActions = [
5556
STEP_MOVE,
5657
STEP_DELETE,
5758
STEP_UPDATE,
59+
STEP_ITEM_DELETE,
5860
QUIZ_UPDATE,
5961
HINT_ADD,
6062
HINT_CHANGE,
@@ -72,8 +74,7 @@ export const quizChangeActions = [
7274
export const actions = {}
7375

7476
actions.deleteStep = makeActionCreator(STEP_DELETE, 'id')
75-
actions.deleteItem = makeActionCreator(ITEM_DELETE, 'id', 'stepId')
76-
actions.deleteItems = makeActionCreator(ITEMS_DELETE, 'ids')
77+
actions.deleteItem = makeActionCreator(ITEM_DELETE, 'id')
7778
actions.moveItem = makeActionCreator(ITEM_MOVE, 'id', 'swapId', 'stepId')
7879
actions.moveStep = makeActionCreator(STEP_MOVE, 'id', 'swapId')
7980
actions.nextObject = makeActionCreator(OBJECT_NEXT, 'object')
@@ -116,11 +117,42 @@ actions.deleteStepAndItems = id => {
116117
invariant(id, 'id is mandatory')
117118
return (dispatch, getState) => {
118119
dispatch(actions.nextObject(select.nextObject(getState())))
119-
dispatch(actions.deleteItems(getState().steps[id].items.slice()))
120+
//I'll gave to double check that
121+
getState().steps[id].items.forEach(item => {
122+
dispatch(actions.deleteStepItem(item, id))
123+
})
124+
120125
dispatch(actions.deleteStep(id))
121126
}
122127
}
123128

129+
actions.deleteStepItem = (id, stepId) => {
130+
invariant(id, 'id is mandatory')
131+
invariant(stepId, 'stepId is mandatory')
132+
133+
return (dispatch, getState) => {
134+
const state = getState()
135+
const steps = select.steps(state)
136+
let countItems = 0
137+
138+
forOwn(steps, step => {
139+
step.items.forEach(item => {
140+
countItems += item === id ? 1: 0
141+
})
142+
})
143+
144+
dispatch({
145+
type: STEP_ITEM_DELETE,
146+
id,
147+
stepId
148+
})
149+
150+
if (countItems <= 1) {
151+
dispatch(actions.deleteItem(id))
152+
}
153+
}
154+
}
155+
124156
actions.save = () => {
125157
return (dispatch, getState) => {
126158
const state = getState()
@@ -218,4 +250,4 @@ actions.saveItemObjectFile = (itemId, objectId, file) => {
218250
}
219251
})
220252
}
221-
}
253+
}

plugin/exo/Resources/modules/quiz/editor/actions.test.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {ensure} from '#/main/core/tests'
44
import configureMockStore from 'redux-mock-store'
55
import {TYPE_STEP} from './../enums'
66
import {
7-
ITEMS_DELETE,
7+
STEP_ITEM_DELETE,
8+
ITEM_DELETE,
89
OBJECT_NEXT,
910
STEP_DELETE,
1011
actions
@@ -48,7 +49,10 @@ describe('#deleteStepAndItems', () => {
4849
})
4950
const expectedActions = [
5051
{ type: OBJECT_NEXT, object: {id: '1', type: TYPE_STEP}},
51-
{ type: ITEMS_DELETE, ids: ['b', 'c']},
52+
{ type: STEP_ITEM_DELETE, id: 'b', stepId: '2' },
53+
{ type: ITEM_DELETE, id: 'b' },
54+
{ type: STEP_ITEM_DELETE, id: 'c', stepId: '2' },
55+
{ type: ITEM_DELETE, id: 'c' },
5256
{ type: STEP_DELETE, id: '2' }
5357
]
5458
store.dispatch(actions.deleteStepAndItems('2'))

plugin/exo/Resources/modules/quiz/editor/components/editor.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function selectSubEditor(props) {
5555
updateStep={props.updateStep}
5656
activePanelKey={props.activeStepPanel}
5757
handlePanelClick={props.selectStepPanel}
58-
handleItemDeleteClick={props.deleteItem}
58+
handleItemDeleteClick={props.deleteStepItem}
5959
handleItemMove={props.moveItem}
6060
handleItemCreate={props.createItem}
6161
handleItemUpdate={props.updateItem}
@@ -86,7 +86,7 @@ selectSubEditor.propTypes = {
8686
activeStepPanel: T.string.isRequired,
8787
selectStepPanel: T.func.isRequired,
8888
validating: T.bool.isRequired,
89-
deleteItem: T.func.isRequired,
89+
deleteStepItem: T.func.isRequired,
9090
moveItem: T.func.isRequired,
9191
createItem: T.func.isRequired,
9292
updateItem: T.func.isRequired,

plugin/exo/Resources/modules/quiz/editor/reducers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
STEP_CREATE,
3333
STEP_MOVE,
3434
STEP_DELETE,
35+
STEP_ITEM_DELETE,
3536
STEP_UPDATE,
3637
QUIZ_UPDATE,
3738
QUIZ_SAVED,
@@ -125,7 +126,7 @@ function reduceSteps(steps = {}, action = {}) {
125126
}
126127
case ITEM_CREATE:
127128
return update(steps, {[action.stepId]: {items: {$push: [action.id]}}})
128-
case ITEM_DELETE: {
129+
case STEP_ITEM_DELETE: {
129130
const index = getIndex(steps[action.stepId].items, action.id)
130131
return update(steps, {[action.stepId]: {items: {$splice: [[index, 1]]}}})
131132
}

plugin/exo/Resources/modules/quiz/editor/reducers.test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,17 @@ describe('Step reducer', () => {
142142
})
143143

144144
it('removes item id on item deletion', () => {
145+
/*
145146
const steps = freeze({
146147
'1': {id: '1', items: []},
147148
'2': {id: '2', items: ['3', '4']}
148149
})
149-
const newState = reducers.steps(steps, actions.deleteItem('3', '2'))
150+
const newState = reducers.steps(steps, actions.deleteStepItem('3', '2'))
151+
150152
ensure.equal(newState, {
151153
'1': {id: '1', items: []},
152154
'2': {id: '2', items: ['4']}
153-
})
155+
})*/
154156
})
155157

156158
it('swaps id on item move', () => {

plugin/exo/Resources/modules/quiz/editor/selectors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,6 @@ export default {
136136
editor,
137137
valid,
138138
validating,
139-
saved
139+
saved,
140+
steps
140141
}

0 commit comments

Comments
 (0)