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

47 lines
1.1 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": {
const alreadyExists = state.allAlerts.some(
(alertItem) => alertItem.vrm === action.payload.vrm
);
if (alreadyExists) {
return { ...state };
} else {
return {
...state,
alertList: [...state.allAlerts, action.payload],
allAlerts: [...state.allAlerts, action.payload],
};
}
}
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 {
console.log(state);
return {
...state,
alertList: state.allAlerts,
};
}
}
default:
return { ...state };
}
}