Skip to content

Commit

Permalink
APPEALS-37558 restructured the levers to store the backend lever valu… (
Browse files Browse the repository at this point in the history
#20447)

* APPEALS-37719 added a migration to change the column names to be more specific, and changed everywhere in the code that had references to the old names.

* made changes to the seed data so that the migration would run

* amycommits/APPEALS-37719

* APPEALS-37720 changed the variables in the reducer

* APPEALS-37692 started to convert DockettimeGoal

* APPEALS-37692 progress made in refactoring the update lever

* APPEALS-37692 progress made in refactoring the update lever

* APPEALS-37692 converted Docket time goals to update the lever and use a selector. There is still some code cleanup that needs to be done.

* APPEALS-37558 restructured the levers to store the backend lever value and keep tabs of the actual value change.
  • Loading branch information
amybids authored Jan 8, 2024
1 parent 8f20cbe commit 1dcb34f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 22 deletions.
6 changes: 3 additions & 3 deletions client/app/caseDistribution/components/DocketTimeGoals.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const DocketTimeGoals = (props) => {
setTimeGoalLever(currentTimeLevers);
}, [currentTimeLevers]);

const updateLever = (leverItem, leverType, usesToggle = false, toggleValue = false) => (event) => {
dispatch(updateLeverState(leverType, leverItem, event, false, usesToggle, toggleValue));
};
const updateLever = (leverItem, leverType, toggleValue = false) => (event) => {
dispatch(updateLeverState(leverType, leverItem, event, null, toggleValue))
}

const toggleLever = (index) => () => {
const levers = docketDistributionLevers.map((lever, i) => {
Expand Down
1 change: 1 addition & 0 deletions client/app/caseDistribution/components/LeverSaveButton.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { ACTIONS } from 'app/caseDistribution/reducers/levers/leversActionTypes';
import { haveLeversChanged } from '../reducers/levers/leversSelector';
import ApiUtil from '../../util/ApiUtil';
import Modal from 'app/components/Modal';
import Button from 'app/components/Button';
Expand Down
5 changes: 2 additions & 3 deletions client/app/caseDistribution/reducers/levers/leversActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ export const loadLevers = (levers) =>
});
};

export const updateLeverState = (leverGroup, leverItem, value, usesOption = false, usesToggle = false, toggleValue = false) =>
export const updateLeverState = (leverGroup, leverItem, value, optionValue = null, toggleValue = false) =>
(dispatch) => {
dispatch({
type: ACTIONS.UPDATE_LEVER,
payload: {
leverGroup,
leverItem,
value,
usesOption,
usesToggle,
optionValue,
toggleValue
}
})
Expand Down
82 changes: 66 additions & 16 deletions client/app/caseDistribution/reducers/levers/leversReducer.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,96 @@
import {ACTIONS } from '../levers/leversActionTypes';
import { update } from '../../../util/ReducerUtil';
import { Constant } from '../../constants'

// saveChangesActivated, editedLevers, formattedHistory, changesOccurred should be deleted. Refactor where it is used before deletion
export const initialState = {
saveChangesActivated: false,
editedLevers: [],
levers: {},
backendLevers: [],
formattedHistory: {},
historyList: {},
historyList: [],
changesOccurred: false,
showSuccessBanner: false,
};

const leversReducer = (state = initialState, action = {}) => {
switch (action.type) {

case ACTIONS.INITIAL_LOAD:
return update(state, {
levers: {
$set: action.payload.levers
},
backendLevers: {
$set: action.payload.levers
}
});
case ACTIONS.INITIAL_LOAD:
// all of the logic to append currentValue and backendValue needs to be moved to LOAD_LEVERS
// remove all calls to INITIAL_LOAD and use LOAD_LEVERS instead
const leverGroups = Object.keys(action.payload.levers);

const leversWithValues = () => {
return leverGroups.reduce((updatedLevers, leverGroup) => {
updatedLevers[leverGroup] = action.payload.levers[leverGroup].map(lever => {
let value = null;
const group = lever.lever_group
if (group === Constant.AFFINITY) {
value = lever.options.find(option => option.item === lever.value).value;
} else if (group === Constant.DOCKET_DISTRIBUTION_PRIOR) {
value = `${lever.is_toggle_active}-${lever.value}`;
}
else {
value = lever.value;
}

// Add backendValue and currentValue attributes
return {
...lever,
backendValue: value,
currentValue: value,
};
});

return updatedLevers;
}, {});
};

const updatedLeversWithValues = leversWithValues();

return update(state, {
levers: {
$set: updatedLeversWithValues,
},
backendLevers: {
$set: updatedLeversWithValues,
},
});

case ACTIONS.LOAD_LEVERS:
// const leverGroups = Object.keys(action.payload.levers)
// const levers = leverGroups.forEach(leverGroup => leverGroup.forEach(lever => {
// let value = null;
// switch(lever.lever_group) {
// case Constant.AFFINITY:
// value = lever.options[lever.value].value
// return
// default:
// value = lever.value
// return
// }
// lever.backendValue = value;
// lever.currentValue = value;
// }))
return update(state, {
levers: {
$set: action.payload.levers
}
});

case ACTIONS.UPDATE_LEVER:
const { leverGroup, leverItem, value, usesOption, usesToggle, toggleValue } = action.payload;
const { leverGroup, leverItem, value, optionValue, toggleValue } = action.payload;
const updateLeverValue = (lever) => {
if (usesOption) {
return { ...lever, option: [{ value }] }; // Update the option value
} else if (usesToggle) {
return { ...lever, value, is_toggle_active: toggleValue }; // Update both value and is_toggle_active
if (leverGroup === Constant.AFFINITY) {
const selectedOption = lever.options.find(option => option.item ===value)
selectedOption.value = optionValue
return { ...lever, currentValue: optionValue, value };
} else if (leverGroup === Constant.DOCKET_DISTRIBUTION_PRIOR) {
return { ...lever, value, currentValue: `${toggleValue}-${value}`, is_toggle_active: toggleValue };
} else {
return { ...lever, value }; // Update only the value
return { ...lever, value, currentValue: value };
}
};
const updatedLever = state.levers[leverGroup].map((lever) =>
Expand Down
16 changes: 16 additions & 0 deletions client/app/caseDistribution/reducers/levers/leversSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ const getLeversByGroupConstant = (state, leverSet, groupName) => {
return getLevers(state, leverSet)[groupName] || []
};

const countChangedLevers = state => {
const flattenLevers = Object.values(state.levers).flat();
const flattenBackendLevers = Object.values(state.backendLevers).flat();
const changedLevers = flattenLevers.filter((lever, index) => {
const backendValue = flattenBackendLevers[index].backendValue;
const currentValue = lever.currentValue;
// Check if backendValue and currentValue are different
return backendValue !== currentValue;
});
return changedLevers.length;
};

export const getLeversByGroup = createSelector(
[getLeversByGroupConstant],
(leversByGroup) => {
return leversByGroup
}
);

export const haveLeversChanged = createSelector(
[countChangedLevers],
count => count > 0
);

0 comments on commit 1dcb34f

Please sign in to comment.