From cba39df5da739dfb58d80670a75d5e8ea20a122d Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Fri, 4 Mar 2022 00:37:31 -0800 Subject: [PATCH] Handle to front line events in the new UI. --- client/src/api/eventstream.tsx | 23 +++++++++++++++++++ client/src/api/frontLinesSlice.ts | 23 +++++++++++++++---- client/src/api/frontline.ts | 1 + .../frontlineslayer/FrontLinesLayer.tsx | 2 +- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/client/src/api/eventstream.tsx b/client/src/api/eventstream.tsx index 72fa2790..e8f0d9ca 100644 --- a/client/src/api/eventstream.tsx +++ b/client/src/api/eventstream.tsx @@ -4,6 +4,11 @@ import { updateControlPoint } from "./controlPointsSlice"; import { ControlPoint } from "./controlpoint"; import { Flight } from "./flight"; import { deselectFlight, selectFlight } from "./flightsSlice"; +import { + addFrontLine, + deleteFrontLine, + updateFrontLine, +} from "./frontLinesSlice"; import FrontLine from "./frontline"; import Tgo from "./tgo"; import { updateTgo } from "./tgosSlice"; @@ -40,15 +45,33 @@ export const handleStreamedEvents = ( if (events.deselected_flight) { dispatch(deselectFlight()); } + if (events.selected_flight != null) { dispatch(selectFlight(events.selected_flight)); } + + for (const front of events.new_front_lines) { + dispatch(addFrontLine(front)); + } + + for (const id of events.updated_front_lines) { + backend.get(`/front-lines/${id}`).then((response) => { + const front = response.data as FrontLine; + dispatch(updateFrontLine(front)); + }); + } + + for (const id of events.deleted_front_lines) { + dispatch(deleteFrontLine(id)); + } + for (const id of events.updated_tgos) { backend.get(`/tgos/${id}`).then((response) => { const tgo = response.data as Tgo; dispatch(updateTgo(tgo)); }); } + for (const id of events.updated_control_points) { backend.get(`/control-points/${id}`).then((response) => { const cp = response.data as ControlPoint; diff --git a/client/src/api/frontLinesSlice.ts b/client/src/api/frontLinesSlice.ts index a55ec36f..41c4be9a 100644 --- a/client/src/api/frontLinesSlice.ts +++ b/client/src/api/frontLinesSlice.ts @@ -3,11 +3,11 @@ import FrontLine from "./frontline"; import { PayloadAction, createSlice } from "@reduxjs/toolkit"; interface FrontLinesState { - fronts: FrontLine[]; + fronts: { [key: string]: FrontLine }; } const initialState: FrontLinesState = { - fronts: [], + fronts: {}, }; export const frontLinesSlice = createSlice({ @@ -15,12 +15,27 @@ export const frontLinesSlice = createSlice({ initialState, reducers: { setFrontLines: (state, action: PayloadAction) => { - state.fronts = action.payload; + state.fronts = {}; + for (const front of action.payload) { + state.fronts[front.id] = front; + } + }, + addFrontLine: (state, action: PayloadAction) => { + const front = action.payload; + state.fronts[front.id] = front; + }, + updateFrontLine: (state, action: PayloadAction) => { + const front = action.payload; + state.fronts[front.id] = front; + }, + deleteFrontLine: (state, action: PayloadAction) => { + delete state.fronts[action.payload]; }, }, }); -export const { setFrontLines } = frontLinesSlice.actions; +export const { setFrontLines, addFrontLine, updateFrontLine, deleteFrontLine } = + frontLinesSlice.actions; export const selectFrontLines = (state: RootState) => state.frontLines; diff --git a/client/src/api/frontline.ts b/client/src/api/frontline.ts index 2d1ac6f7..5a04c3ed 100644 --- a/client/src/api/frontline.ts +++ b/client/src/api/frontline.ts @@ -1,6 +1,7 @@ import { LatLng } from "leaflet"; export interface FrontLine { + id: string; extents: LatLng[]; } diff --git a/client/src/components/frontlineslayer/FrontLinesLayer.tsx b/client/src/components/frontlineslayer/FrontLinesLayer.tsx index feed0713..999c61ab 100644 --- a/client/src/components/frontlineslayer/FrontLinesLayer.tsx +++ b/client/src/components/frontlineslayer/FrontLinesLayer.tsx @@ -7,7 +7,7 @@ export default function SupplyRoutesLayer() { const fronts = useAppSelector(selectFrontLines).fronts; return ( - {fronts.map((front, idx) => { + {Object.values(fronts).map((front, idx) => { return ; })}