2025-09-23 16:02:14 +01:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2025-09-16 11:07:35 +01:00
|
|
|
import { useAlertHitContext } from "../../context/AlertHitContext";
|
2025-09-24 12:28:14 +01:00
|
|
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
|
|
|
|
import type { CameraBlackBoardOptions, SightingType } from "../../types/types";
|
2025-09-16 11:07:35 +01:00
|
|
|
import Card from "../UI/Card";
|
|
|
|
|
import CardHeader from "../UI/CardHeader";
|
|
|
|
|
import AlertItem from "./AlertItem";
|
2025-09-23 16:02:14 +01:00
|
|
|
import { faTrash } from "@fortawesome/free-solid-svg-icons";
|
2025-09-16 11:07:35 +01:00
|
|
|
|
|
|
|
|
const HistoryList = () => {
|
2025-09-24 12:28:14 +01:00
|
|
|
const { state, dispatch, isLoading, error } = useAlertHitContext();
|
|
|
|
|
const { mutation } = useCameraBlackboard();
|
2025-09-16 14:20:38 +01:00
|
|
|
|
2025-10-13 11:48:10 +01:00
|
|
|
const handleDeleteClick = async (deletedItem: SightingType) => {
|
|
|
|
|
const res = await mutation.mutateAsync({
|
|
|
|
|
operation: "VIEW",
|
|
|
|
|
path: "alertHistory",
|
|
|
|
|
});
|
|
|
|
|
const oldArray = res?.result;
|
|
|
|
|
const updatedArray = oldArray?.filter(
|
|
|
|
|
(item: SightingType) => item?.ref !== deletedItem?.ref
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
mutation.mutate({
|
|
|
|
|
operation: "INSERT",
|
|
|
|
|
path: "alertHistory",
|
|
|
|
|
value: updatedArray,
|
|
|
|
|
});
|
|
|
|
|
dispatch({ type: "REMOVE", payload: deletedItem });
|
2025-09-24 12:28:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleClearListClick = (listName: CameraBlackBoardOptions) => {
|
|
|
|
|
dispatch({ type: "DELETE", payload: [] });
|
|
|
|
|
mutation.mutate({
|
|
|
|
|
operation: "DELETE",
|
|
|
|
|
path: listName.path,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-16 11:07:35 +01:00
|
|
|
return (
|
2025-09-26 13:58:14 +01:00
|
|
|
<Card className="h-100 p-4">
|
2025-09-16 11:07:35 +01:00
|
|
|
<CardHeader title="Alert History" />
|
2025-09-24 12:28:14 +01:00
|
|
|
<button
|
|
|
|
|
className="bg-red-600 text-white px-4 py-2 rounded hover:bg-red-700 transition md:w-[10%] mb-2"
|
|
|
|
|
onClick={() => handleClearListClick({ path: "alertHistory" })}
|
|
|
|
|
>
|
|
|
|
|
Clear List
|
|
|
|
|
</button>
|
2025-09-26 13:58:14 +01:00
|
|
|
{isLoading && <p className="px-2">Loading...</p>}
|
|
|
|
|
{error && <p className="text-red-500 px-2">Error: {error.message}</p>}
|
|
|
|
|
<div className="flex flex-col gap-1 px-2">
|
2025-09-16 14:20:38 +01:00
|
|
|
{state?.alertList?.length > 0 ? (
|
2025-09-16 11:07:35 +01:00
|
|
|
state?.alertList?.map((alertItem, index) => (
|
2025-09-23 16:02:14 +01:00
|
|
|
<div key={index} className="flex flex-row space-x-2">
|
|
|
|
|
<AlertItem item={alertItem} />
|
2025-10-13 11:48:10 +01:00
|
|
|
<button onClick={() => handleDeleteClick(alertItem)}>
|
2025-09-23 16:02:14 +01:00
|
|
|
<div className="p-4">
|
|
|
|
|
<FontAwesomeIcon icon={faTrash} size="2x" />
|
|
|
|
|
</div>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-09-16 11:07:35 +01:00
|
|
|
))
|
|
|
|
|
) : (
|
2025-09-16 14:20:38 +01:00
|
|
|
<p>No Alert results</p>
|
2025-09-16 11:07:35 +01:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default HistoryList;
|