linking lists with reducer
This commit is contained in:
parent
c0761da046
commit
e9d273c5a2
7 changed files with 101 additions and 27 deletions
|
@ -23,10 +23,7 @@ export const initialState: StateInterface = {
|
|||
}],
|
||||
filters: {
|
||||
engineType: {
|
||||
availableList: [{
|
||||
label: 'ABC',
|
||||
value: 'ABC',
|
||||
}],
|
||||
availableList: [],
|
||||
selectedList: [
|
||||
{
|
||||
label: 'DEF',
|
||||
|
@ -42,18 +39,22 @@ export const initialState: StateInterface = {
|
|||
},
|
||||
findings: {
|
||||
// filtersCSC.findings
|
||||
availableList: [
|
||||
{
|
||||
label: 'FRETTING : Usure induite par petit debat',
|
||||
value: 'FRETTING',
|
||||
},
|
||||
{
|
||||
label: 'DENT : Impact a bord arrondi',
|
||||
value: 'DENT',
|
||||
}, {
|
||||
label: 'MISSING PAINT/COATING',
|
||||
value: 'MISSING_PAINT_COATING',
|
||||
}],
|
||||
availableList:
|
||||
filtersCSC.findings
|
||||
// [
|
||||
// {
|
||||
// label: 'FRETTING : Usure induite par petit debat',
|
||||
// value: 'FRETTING',
|
||||
// },
|
||||
// {
|
||||
// label: 'DENT : Impact a bord arrondi',
|
||||
// value: 'DENT',
|
||||
// }, {
|
||||
// label: 'MISSING PAINT/COATING',
|
||||
// value: 'MISSING_PAINT_COATING',
|
||||
// }]
|
||||
|
||||
,
|
||||
selectedList: []
|
||||
},
|
||||
ata: {
|
||||
|
|
|
@ -27,6 +27,13 @@ export interface UpdateUserAction {
|
|||
id: string;
|
||||
};
|
||||
}
|
||||
export interface UpdateFilterAction {
|
||||
type: ActionTypes.UPDATE_FILTER;
|
||||
payload: {
|
||||
filter: string;
|
||||
choice: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface UpdateAppAction {
|
||||
type: ActionTypes.UPDATE_APP;
|
||||
|
@ -91,6 +98,7 @@ export type technicalManualType = {
|
|||
export type AppActions =
|
||||
| UpdateUserAction
|
||||
| UpdateAppAction
|
||||
| UpdateFilterAction
|
||||
| SendUserFeedbackAction
|
||||
| SwitchToNextThemeAction
|
||||
| SwitchToNextLanguageAction;
|
||||
|
@ -162,6 +170,47 @@ function appReducer(state = initialState.app, action: AppActions) {
|
|||
...state,
|
||||
...action.payload
|
||||
};
|
||||
case ActionTypes.UPDATE_FILTER: {
|
||||
const { filter, choice } = (action as UpdateFilterAction).payload;
|
||||
const filterGroup = state.filters[filter as keyof typeof state.filters];
|
||||
if (!filterGroup) {
|
||||
return state;
|
||||
}
|
||||
|
||||
const isInSelected = filterGroup.selectedList.some((item: any) => item.value === choice);
|
||||
const isInAvailable = filterGroup.availableList.some((item: any) => item.value === choice);
|
||||
|
||||
// If in selected → move to available; if in available → move to selected; else no-op
|
||||
if (isInSelected) {
|
||||
const item = filterGroup.selectedList.find((i: any) => i.value === choice);
|
||||
return {
|
||||
...state,
|
||||
filters: {
|
||||
...state.filters,
|
||||
[filter]: {
|
||||
availableList: [...filterGroup.availableList, item],
|
||||
selectedList: filterGroup.selectedList.filter((i: any) => i.value !== choice)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (isInAvailable) {
|
||||
const item = filterGroup.availableList.find((i: any) => i.value === choice);
|
||||
return {
|
||||
...state,
|
||||
filters: {
|
||||
...state.filters,
|
||||
[filter]: {
|
||||
availableList: filterGroup.availableList.filter((i: any) => i.value !== choice),
|
||||
selectedList: [...filterGroup.selectedList, item]
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue