linking lists with reducer

This commit is contained in:
Tykayn 2025-09-25 17:43:02 +02:00 committed by tykayn
parent c0761da046
commit e9d273c5a2
7 changed files with 101 additions and 27 deletions

View file

@ -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: {

View file

@ -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;
}