- added functionality to save sighting sessions to black board
This commit is contained in:
@@ -6,30 +6,18 @@ import { toast } from "sonner";
|
|||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { faFloppyDisk, faPause, faPlay, faStop } from "@fortawesome/free-solid-svg-icons";
|
import { faFloppyDisk, faPause, faPlay, faStop } from "@fortawesome/free-solid-svg-icons";
|
||||||
import VehicleSessionItem from "../UI/VehicleSessionItem";
|
import VehicleSessionItem from "../UI/VehicleSessionItem";
|
||||||
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
||||||
|
|
||||||
const SessionCard = () => {
|
const SessionCard = () => {
|
||||||
const { sessionStarted, setSessionStarted, sessionList, setSessionPaused, sessionPaused } = useNPEDContext();
|
const { sessionStarted, setSessionStarted, sessionList, setSessionPaused, sessionPaused, savedSightings } =
|
||||||
|
useNPEDContext();
|
||||||
const handleStartClick = () => {
|
const { mutation } = useCameraBlackboard();
|
||||||
setSessionStarted(!sessionStarted);
|
|
||||||
setSessionPaused(false);
|
|
||||||
toast(`${sessionStarted ? "Vehicle tracking session ended" : "Vehicle tracking session started"}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSaveCick = () => {
|
|
||||||
console.log("clicked");
|
|
||||||
};
|
|
||||||
|
|
||||||
const handlepauseClick = () => {
|
|
||||||
setSessionPaused(!sessionPaused);
|
|
||||||
toast(`${sessionStarted ? "Vehicle tracking session paused" : "Vehicle tracking session resumed"}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
const sightings = [...new Map(sessionList.map((vehicle) => [vehicle.vrm, vehicle]))];
|
const sightings = [...new Map(sessionList.map((vehicle) => [vehicle.vrm, vehicle]))];
|
||||||
|
|
||||||
const dedupedSightings = sightings.map((sighting) => sighting[1]);
|
const dedupedSightings = sightings.map((sighting) => sighting[1]);
|
||||||
|
|
||||||
const vehicles = dedupedSightings.reduce<Record<string, ReducedSightingType[]>>(
|
const vehicles = savedSightings.reduce<Record<string, ReducedSightingType[]>>(
|
||||||
(acc, item) => {
|
(acc, item) => {
|
||||||
const hotlisthit = Object.values(item.metadata?.hotlistMatches ?? {}).includes(true);
|
const hotlisthit = Object.values(item.metadata?.hotlistMatches ?? {}).includes(true);
|
||||||
if (item.metadata?.npedJSON["NPED CATEGORY"] === "A") acc.npedCatA.push(item);
|
if (item.metadata?.npedJSON["NPED CATEGORY"] === "A") acc.npedCatA.push(item);
|
||||||
@@ -39,6 +27,7 @@ const SessionCard = () => {
|
|||||||
if (item.metadata?.npedJSON["TAX STATUS"] === false) acc.notTaxed.push(item);
|
if (item.metadata?.npedJSON["TAX STATUS"] === false) acc.notTaxed.push(item);
|
||||||
if (item.metadata?.npedJSON["MOT STATUS"] === false) acc.notMOT.push(item);
|
if (item.metadata?.npedJSON["MOT STATUS"] === false) acc.notMOT.push(item);
|
||||||
if (hotlisthit) acc.hotlistHit.push(item);
|
if (hotlisthit) acc.hotlistHit.push(item);
|
||||||
|
acc.vehicles.push(item);
|
||||||
return acc;
|
return acc;
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -49,9 +38,31 @@ const SessionCard = () => {
|
|||||||
notTaxed: [],
|
notTaxed: [],
|
||||||
notMOT: [],
|
notMOT: [],
|
||||||
hotlistHit: [],
|
hotlistHit: [],
|
||||||
|
vehicles: [],
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const handleStartClick = () => {
|
||||||
|
setSessionStarted(!sessionStarted);
|
||||||
|
setSessionPaused(false);
|
||||||
|
toast(`${sessionStarted ? "Vehicle tracking session ended" : "Vehicle tracking session started"}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlepauseClick = () => {
|
||||||
|
setSessionPaused(!sessionPaused);
|
||||||
|
toast(`${sessionStarted ? "Vehicle tracking session paused" : "Vehicle tracking session resumed"}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSaveCick = async () => {
|
||||||
|
const result = await mutation.mutateAsync({
|
||||||
|
operation: "INSERT",
|
||||||
|
path: "sessionStats",
|
||||||
|
value: dedupedSightings,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.reason === "OK") toast.success("Session saved");
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="p-4 col-span-3">
|
<Card className="p-4 col-span-3">
|
||||||
<CardHeader title="Session" />
|
<CardHeader title="Session" />
|
||||||
@@ -94,7 +105,7 @@ const SessionCard = () => {
|
|||||||
|
|
||||||
<ul className="text-white space-y-2">
|
<ul className="text-white space-y-2">
|
||||||
<VehicleSessionItem
|
<VehicleSessionItem
|
||||||
sessionNumber={dedupedSightings.length}
|
sessionNumber={vehicles.vehicles.length}
|
||||||
textColour="text-green-400"
|
textColour="text-green-400"
|
||||||
vehicleTag={"Number of Vehicles sightings:"}
|
vehicleTag={"Number of Vehicles sightings:"}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { createContext, useContext, type SetStateAction } from "react";
|
import { createContext, useContext, type SetStateAction } from "react";
|
||||||
import type { NPEDUser, ReducedSightingType } from "../types/types";
|
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../types/types";
|
||||||
|
|
||||||
type UserContextValue = {
|
type UserContextValue = {
|
||||||
user: NPEDUser | null;
|
user: NPEDUser | null;
|
||||||
@@ -10,6 +10,8 @@ type UserContextValue = {
|
|||||||
setSessionStarted: React.Dispatch<SetStateAction<boolean>>;
|
setSessionStarted: React.Dispatch<SetStateAction<boolean>>;
|
||||||
sessionList: ReducedSightingType[];
|
sessionList: ReducedSightingType[];
|
||||||
setSessionList: React.Dispatch<SetStateAction<ReducedSightingType[]>>;
|
setSessionList: React.Dispatch<SetStateAction<ReducedSightingType[]>>;
|
||||||
|
savedSightings: DedupedSightings;
|
||||||
|
setSavedSightings: React.Dispatch<SetStateAction<DedupedSightings>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NPEDUserContext = createContext<UserContextValue | undefined>(undefined);
|
export const NPEDUserContext = createContext<UserContextValue | undefined>(undefined);
|
||||||
|
|||||||
@@ -1,17 +1,34 @@
|
|||||||
import { useState, type ReactNode } from "react";
|
import { useEffect, useReducer, useState, type ReactNode } from "react";
|
||||||
import type { NPEDUser, ReducedSightingType } from "../../types/types";
|
import type { DedupedSightings, NPEDUser, ReducedSightingType } from "../../types/types";
|
||||||
import { NPEDUserContext } from "../NPEDUserContext";
|
import { NPEDUserContext } from "../NPEDUserContext";
|
||||||
|
import { useCameraBlackboard } from "../../hooks/useCameraBlackboard";
|
||||||
|
import { initialState, reducer } from "../reducers/NPEDContextReducer";
|
||||||
|
|
||||||
type NPEDUserProviderType = {
|
type NPEDUserProviderType = {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
|
export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
|
||||||
|
const [state, dispatch] = useReducer(reducer, initialState);
|
||||||
|
const { mutation } = useCameraBlackboard();
|
||||||
const [user, setUser] = useState<NPEDUser | null>(null);
|
const [user, setUser] = useState<NPEDUser | null>(null);
|
||||||
const [sessionStarted, setSessionStarted] = useState(false);
|
const [sessionStarted, setSessionStarted] = useState(false);
|
||||||
const [sessionList, setSessionList] = useState<ReducedSightingType[]>([]);
|
const [sessionList, setSessionList] = useState<ReducedSightingType[]>([]);
|
||||||
const [sessionPaused, setSessionPaused] = useState(false);
|
const [sessionPaused, setSessionPaused] = useState(false);
|
||||||
|
const [savedSightings, setSavedSightings] = useState<DedupedSightings | []>([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchData = async () => {
|
||||||
|
const result = await mutation.mutateAsync({
|
||||||
|
operation: "VIEW",
|
||||||
|
path: "sessionStats",
|
||||||
|
});
|
||||||
|
if (!result.result) return;
|
||||||
|
setSavedSightings(result?.result);
|
||||||
|
};
|
||||||
|
fetchData();
|
||||||
|
}, []);
|
||||||
|
console.log(savedSightings);
|
||||||
return (
|
return (
|
||||||
<NPEDUserContext.Provider
|
<NPEDUserContext.Provider
|
||||||
value={{
|
value={{
|
||||||
@@ -23,6 +40,8 @@ export const NPEDUserProvider = ({ children }: NPEDUserProviderType) => {
|
|||||||
setSessionList,
|
setSessionList,
|
||||||
sessionPaused,
|
sessionPaused,
|
||||||
setSessionPaused,
|
setSessionPaused,
|
||||||
|
savedSightings,
|
||||||
|
setSavedSightings,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
20
src/context/reducers/NPEDContextReducer.ts
Normal file
20
src/context/reducers/NPEDContextReducer.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import type { NPEDACTION, NPEDSTATE } from "../../types/types";
|
||||||
|
|
||||||
|
export const initialState = {
|
||||||
|
sessionStarted: false,
|
||||||
|
sessionList: [],
|
||||||
|
sessionPaused: false,
|
||||||
|
savedSightings: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
export function reducer(state: NPEDSTATE, action: NPEDACTION) {
|
||||||
|
switch (action.type) {
|
||||||
|
case "SESSION":
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
sessionStarted: action.payload,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return { ...state };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -379,3 +379,17 @@ export type QueuedHit = {
|
|||||||
sighting: SightingType;
|
sighting: SightingType;
|
||||||
kind: HitKind;
|
kind: HitKind;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type DedupedSightings = ReducedSightingType[];
|
||||||
|
|
||||||
|
export type NPEDSTATE = {
|
||||||
|
sessionStarted: boolean;
|
||||||
|
sessionList: ReducedSightingType[];
|
||||||
|
sessionPaused: boolean;
|
||||||
|
savedSightings: DedupedSightings;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type NPEDACTION = {
|
||||||
|
type: string;
|
||||||
|
payload: any;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user