2025-09-16 11:07:35 +01:00
|
|
|
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;
|
|
|
|
|
}
|
2025-09-16 11:07:35 +01:00
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
allAlerts: [...state.allAlerts, action.payload],
|
2025-09-16 14:20:38 +01:00
|
|
|
alertList: [...state.allAlerts, action.payload],
|
2025-09-16 11:07:35 +01:00
|
|
|
};
|
|
|
|
|
}
|
2025-09-16 14:20:38 +01:00
|
|
|
return state;
|
2025-09-16 11:07:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 16:02:14 +01:00
|
|
|
case "REMOVE": {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
alertList: state.alertList.filter(
|
|
|
|
|
(alertItem) => alertItem.vrm !== action.payload.vrm
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 12:28:14 +01:00
|
|
|
case "DELETE": {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
alertList: action.payload,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 11:07:35 +01:00
|
|
|
default:
|
|
|
|
|
return { ...state };
|
|
|
|
|
}
|
|
|
|
|
}
|