Skip to content

Commit

Permalink
Change minimum score for dispatch, fixes #144
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuserik committed Sep 19, 2018
1 parent 1adfb43 commit b9e060d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/components/EditScreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {connect} from 'react-redux';
import React, {Component} from 'react';
import Immutable from 'immutable';

import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import DeleteIcon from '@material-ui/icons/Delete';
Expand All @@ -15,7 +16,8 @@ import {adminCurrentScreen, getScreen} from '../redux/selectors';
import {
editScreen,
updateScreenElement,
addQuestionAnswer
addQuestionAnswer,
updateDispatch
} from '../redux/actions';
import quizElements from './quizElements';
import style from './style';
Expand Down Expand Up @@ -43,15 +45,24 @@ function editUI({
</Grid>
));
}
function editCondition(condition, {classes, doEditScreen}) {
function editCondition(condition, {classes, doEditScreen, doUpdateDispatch}) {
const minScore = condition.getIn(['condition', 'atLeast', 'score']);
return (
<Grid item xs={12} key={condition.getIn(['action', 'screen'])}>
<Paper>
<TextField
label="Mindste antal point"
value={minScore}
onChange={e => console.log('TODO: change minScore')}
onChange={e =>
doUpdateDispatch(o =>
o.set(
'condition',
Immutable.fromJS({
atLeast: {score: Math.max(0, e.target.value)}
})
)
)
}
type="number"
className={classes.margin}
/>
Expand Down Expand Up @@ -82,13 +93,24 @@ function editCondition(condition, {classes, doEditScreen}) {
</Grid>
);
}
function editDispatch({currentScreen, classes, doEditScreen}) {
function editDispatch({
currentScreen,
classes,
doEditScreen,
doUpdateDispatch
}) {
return (
<Grid container spacing={16}>
{currentScreen
.get('dispatch')
.butLast()
.map(o => editCondition(o, {classes, doEditScreen}))}
.map((o, pos) =>
editCondition(o, {
classes,
doEditScreen,
doUpdateDispatch: doUpdateDispatch(currentScreen.get('_id'), pos)
})
)}
<Grid item xs={12}>
<Button aria-label="Add" mini onClick={addQuestionAnswer}>
<AddIcon /> Mulig slutning
Expand Down Expand Up @@ -146,6 +168,8 @@ export function mapDispatchToProps(dispatch) {
doEditScreen: screen => dispatch(editScreen({screen})),
doUpdateScreenElement: (screen, pos) => updateFn =>
dispatch(updateScreenElement({screen, pos, updateFn})),
doUpdateDispatch: (screen, pos) => updateFn =>
dispatch(updateDispatch({screen, pos, updateFn})),
doAddQuestionAnswer: (screen, pos) =>
dispatch(addQuestionAnswer([screen, pos]))
};
Expand Down
6 changes: 6 additions & 0 deletions src/redux/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {getUser, storage, findOrCreateType} from './openplatform';

export const updateDispatch = o => {
return {
type: 'UPDATE_DISPATCH',
...o
};
};
export const addQuestionAnswer = path => {
return {
type: 'ADD_QUESTION_ANSWER',
Expand Down
15 changes: 15 additions & 0 deletions src/redux/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,26 @@ function pageAction(state, action) {
return state;
}
}
function sortDispatchesByAtLeastScore(state, screen) {
return state.updateIn(['quiz', 'screens', screen, 'dispatch'], o =>
o.sort(
(a, b) =>
b.getIn(['condition', 'atLeast', 'score'], -1) -
a.getIn(['condition', 'atLeast', 'score'], -1)
)
);
}

export function root(state = initialState, action) {
switch (action.type) {
case '@@INIT':
return state;
case 'UPDATE_DISPATCH':
state = state.updateIn(
['quiz', 'screens', action.screen, 'dispatch', action.pos],
action.updateFn
);
return sortDispatchesByAtLeastScore(state, action.screen);
case 'UPDATE_SCREEN_ELEMENT':
state = state.updateIn(
['quiz', 'screens', action.screen, 'ui', action.pos],
Expand Down

0 comments on commit b9e060d

Please sign in to comment.