2017-11-09 17:10:30 -08:00
|
|
|
import {
|
|
|
|
createAction,
|
|
|
|
createTypes,
|
|
|
|
handleActions
|
|
|
|
} from 'berkeleys-redux-utils';
|
2017-08-08 15:31:26 -04:00
|
|
|
import noop from 'lodash/noop';
|
|
|
|
|
|
|
|
import ns from '../ns.json';
|
|
|
|
|
|
|
|
export const types = createTypes([
|
|
|
|
'nextQuestion',
|
|
|
|
'selectChoice',
|
|
|
|
'incrementCorrect',
|
|
|
|
'resetQuiz',
|
|
|
|
'resetChoice'
|
|
|
|
], ns);
|
|
|
|
|
|
|
|
export const nextQuestion = createAction(
|
|
|
|
types.nextQuestion,
|
|
|
|
noop
|
|
|
|
);
|
|
|
|
|
|
|
|
export const selectChoice = createAction(
|
|
|
|
types.selectChoice,
|
|
|
|
(selectedChoice) => ({ selectedChoice })
|
|
|
|
);
|
|
|
|
|
|
|
|
export const incrementCorrect = createAction(
|
|
|
|
types.incrementCorrect,
|
|
|
|
noop
|
|
|
|
);
|
|
|
|
|
|
|
|
export const resetQuiz = createAction(
|
|
|
|
types.resetQuiz,
|
|
|
|
noop
|
|
|
|
);
|
|
|
|
|
|
|
|
export const resetChoice = createAction(
|
|
|
|
types.resetChoice,
|
|
|
|
noop
|
|
|
|
);
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
currentIndex: 0,
|
|
|
|
selectedChoice: null,
|
|
|
|
correct: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getNS = state => state[ns];
|
|
|
|
export const currentIndexSelector = state => getNS(state).currentIndex;
|
|
|
|
export const selectedChoiceSelector = state => getNS(state).selectedChoice;
|
|
|
|
export const correctSelector = state => getNS(state).correct;
|
|
|
|
|
2017-11-09 17:10:30 -08:00
|
|
|
export default handleActions(
|
|
|
|
() => ({
|
2017-08-08 15:31:26 -04:00
|
|
|
[types.nextQuestion]: state => ({
|
|
|
|
...state,
|
|
|
|
currentIndex: state.currentIndex + 1
|
|
|
|
}),
|
|
|
|
[types.selectChoice]: (state, {payload}) => ({
|
|
|
|
...state,
|
|
|
|
selectedChoice: payload.selectedChoice
|
|
|
|
}),
|
|
|
|
[types.incrementCorrect]: state => ({
|
|
|
|
...state,
|
|
|
|
correct: state.correct + 1
|
|
|
|
}),
|
|
|
|
[types.resetQuiz]: state => ({
|
|
|
|
...state,
|
|
|
|
currentIndex: 0,
|
|
|
|
correct: 0,
|
|
|
|
selectedChoice: null
|
|
|
|
}),
|
|
|
|
[types.resetChoice]: state => ({
|
|
|
|
...state,
|
|
|
|
selectedChoice: null
|
|
|
|
})
|
2017-11-09 17:10:30 -08:00
|
|
|
}),
|
|
|
|
initialState,
|
|
|
|
ns
|
|
|
|
);
|