Skip to content
Snippets Groups Projects
Commit 36c2d8dd authored by Loïc Poullain's avatar Loïc Poullain
Browse files

Move "resBrut" to results.casTypes.items

parent dfa8ccf4
Branches
No related tags found
1 merge request!21Re-factoriser le state redux
......@@ -28,6 +28,7 @@
}
},
"rules": {
"no-case-declarations": 0,
"local/no-material-root-imports": 2,
"sort-keys": [2, "asc"],
"array-bracket-newline": ["error", { "multiline": true }],
......
......@@ -48,7 +48,7 @@ Here ate the environment variables that you have to set:
# Affichage du PLF
L'affichage du PLF est contrôlé par l'état redux `reformePLF` et par l'état `resBrut`.
L'affichage du PLF est contrôlé par l'état redux `reformePLF` et par l'état `results.casTypes`.
- Pour afficher les valeurs des paramètres, vous devez définir la variable `REFORME_PLF_DEFAULT_STATE` comme valeur par défaut de l'état dans `reforme-plf.js`.
- Pour afficher les résultats, la réponse de l'API doit inclure une propriété `plf` (voir documentation côté serveur).
......
......@@ -5,7 +5,6 @@ import ImpactComponent from "./impact-component";
const mapStateToProps = ({
casTypes,
display,
resBrut,
token,
totalPop,
}) => {
......@@ -14,7 +13,6 @@ const mapStateToProps = ({
casTypes,
isInformationPanelVisible: display.isInformationPanelVisible,
isUserLogged,
resBrut,
totalPop,
};
};
......
......@@ -3,18 +3,18 @@ import { connect } from "react-redux";
import { removeCasType, showEditCasTypesPopin, simulateCasTypes } from "../../../redux/actions";
import SimpleCard from "./simple-card-component";
const mapStateToProps = ({ casTypes, resBrut, results }, { index }) => {
const isLoading = results.casTypes.isFetching;
const mapStateToProps = ({ casTypes, results }, { index }) => {
const { isFetching, items } = results.casTypes;
const { name } = casTypes[index];
const descCasType = casTypes[index];
const resultats = {
apres: resBrut.apres[index],
avant: resBrut.avant[index],
plf: resBrut.plf ? resBrut.plf[index] : null,
apres: items.apres[index],
avant: items.avant[index],
plf: items.plf ? items.plf[index] : null,
};
return {
descCasType,
isLoading,
isLoading: isFetching,
name,
resultats,
};
......
......@@ -27,7 +27,7 @@ const display = (state = initialState, action) => {
export default display;
// Other possible version:
// Other possible version (in this file or in separate files in a subdirectory "display/"):
// function currentExpandedArticlePanel(state = null, action) {
// switch (action.type) {
......
......@@ -7,7 +7,6 @@ import loadingEtat from "./loading-etat";
import reforme from "./reforme";
import reformeBase from "./reforme-base";
import reformePLF from "./reforme-plf";
import resBrut from "./res-brut";
import results from "./results";
import token from "./token";
import totalPop from "./total-pop";
......@@ -20,7 +19,6 @@ export default combineReducers({
reforme,
reformeBase,
reformePLF,
resBrut,
results,
token,
totalPop,
......
import { cloneDeep } from "lodash";
import { SIMULATE_CAS_TYPES_SUCCESS } from "../actions";
const wprm = [1, 1, 1, 1, 1, 1];
const apres = [0, -1336, -1068, 0, -1723, -820];
const avant = [0, -1336, -1068, 0, -1723, -820];
// cree une map d'objet
// dont la cle est l'index dans un array
const toIndexedObject = arr => arr.reduce((acc, v, i) => ({ ...acc, [i]: v }), {});
const DEFAULT_STATE = {
apres: toIndexedObject(apres),
avant: toIndexedObject(avant),
wprm: toIndexedObject(wprm),
};
const getNextStateKey = (state) => {
const nextKey = Object.keys(state.wprm).length;
return nextKey;
};
const resBrut = (state = DEFAULT_STATE, action) => {
let nextKey = null;
let nextState = null;
switch (action.type) {
case SIMULATE_CAS_TYPES_SUCCESS:
return cloneDeep(action.data);
case "onCreateCasType":
nextKey = getNextStateKey(state);
nextState = {
apres: { ...state.apres, [nextKey]: 0 },
avant: { ...state.avant, [nextKey]: 0 },
plf: { ...state.plf, [nextKey]: 0 },
wprm: { ...state.wprm, [nextKey]: 1 },
};
return nextState;
default:
return state;
}
};
export default resBrut;
import { cloneDeep } from "lodash";
import { SIMULATE_CAS_TYPES_FAILURE, SIMULATE_CAS_TYPES_REQUEST, SIMULATE_CAS_TYPES_SUCCESS } from "../../actions";
const wprm = [1, 1, 1, 1, 1, 1];
const apres = [0, -1336, -1068, 0, -1723, -820];
const avant = [0, -1336, -1068, 0, -1723, -820];
// Create a map of objects which key is the index of the array.
const toIndexedObject = arr => arr.reduce((acc, v, i) => ({ ...acc, [i]: v }), {});
const initialState = {
didInvalidate: false,
isFetching: false,
// items: [],
items: {
apres: toIndexedObject(apres),
avant: toIndexedObject(avant),
wprm: toIndexedObject(wprm),
},
};
function casTypes(state = initialState, action) {
......@@ -17,6 +30,7 @@ function casTypes(state = initialState, action) {
return Object.assign({}, state, {
didInvalidate: false,
isFetching: false,
items: cloneDeep(action.data),
});
case SIMULATE_CAS_TYPES_FAILURE:
// The console.log is temporary.
......@@ -25,6 +39,16 @@ function casTypes(state = initialState, action) {
didInvalidate: true,
isFetching: false,
});
case "onCreateCasType":
const nextKey = Object.keys(state.wprm).length;
return Object.assign({}, state, {
items: {
apres: { ...state.apres, [nextKey]: 0 },
avant: { ...state.avant, [nextKey]: 0 },
plf: state.plf ? { ...state.plf, [nextKey]: 0 } : null,
wprm: { ...state.wprm, [nextKey]: 1 },
},
});
default:
return state;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment