Files
Mav-Mobile-UI/src/context/reducers/AlertReducers.ts

57 lines
1.3 KiB
TypeScript
Raw Normal View History

import type { AlertPayload, AlertState } from "../../types/types";
export const initalState = {
alertList: [],
allAlerts: [],
};
export function reducer(state: AlertState, action: AlertPayload) {
switch (action.type) {
case "ADD": {
2025-09-16 14:20:38 +01:00
if (action.payload && "vrm" in action.payload) {
const alreadyExists = state.allAlerts.some(
(alertItem) => alertItem.vrm === action.payload.vrm
);
if (alreadyExists) {
return state;
}
return {
...state,
allAlerts: [...state.allAlerts, action.payload],
2025-09-16 14:20:38 +01:00
alertList: [...state.allAlerts, action.payload],
};
}
2025-09-16 14:20:38 +01:00
return state;
}
case "SEARCH": {
if (action.payload && typeof action.payload === "string") {
const searchTerm = action.payload.toLowerCase();
return {
...state,
alertList: state.alertList.filter((alertItem) =>
alertItem.vrm.toLowerCase().includes(searchTerm)
),
};
} else {
return {
...state,
alertList: state.allAlerts,
};
}
}
case "REMOVE": {
return {
...state,
alertList: state.alertList.filter(
(alertItem) => alertItem.vrm !== action.payload.vrm
),
};
}
default:
return { ...state };
}
}