Un-split the flight data by coalition.

This made one callsite cleaner at the expense of the others and the
state management.
This commit is contained in:
Dan Albert
2022-03-04 19:48:42 -08:00
parent 59f734dd07
commit b6457ae434
3 changed files with 26 additions and 39 deletions

View File

@@ -4,14 +4,12 @@ import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import { LatLng } from "leaflet";
interface FlightsState {
blue: { [id: string]: Flight };
red: { [id: string]: Flight };
flights: { [id: string]: Flight };
selected: Flight | null;
}
const initialState: FlightsState = {
blue: {},
red: {},
flights: {},
selected: null,
};
@@ -20,46 +18,33 @@ export const flightsSlice = createSlice({
initialState,
reducers: {
clearFlights: (state) => {
state.blue = {};
state.red = {};
state.flights = {};
},
registerFlight: (state, action: PayloadAction<Flight>) => {
const flight = action.payload;
const coalitionFlights = flight.blue ? state.blue : state.red;
if (flight.id in coalitionFlights) {
if (flight.id in state.flights) {
console.log(`Overriding flight with ID: ${flight.id}`);
}
coalitionFlights[flight.id] = flight;
state.flights[flight.id] = flight;
},
unregisterFlight: (state, action: PayloadAction<string>) => {
const id = action.payload;
if (id in state.blue) {
delete state.blue[id];
} else if (id in state.red) {
delete state.red[id];
} else {
console.log(
`Could not delete flight with ID ${id} because no flight with that ` +
`ID exists`
);
}
delete state.flights[id];
},
updateFlight: (state, action: PayloadAction<Flight>) => {
const flight = action.payload;
const ato = flight.blue ? state.blue : state.red;
ato[flight.id] = flight;
state.flights[flight.id] = flight;
},
deselectFlight: (state) => {
state.selected = null;
},
selectFlight: (state, action: PayloadAction<string>) => {
const id = action.payload;
state.selected = state.blue[id];
state.selected = state.flights[id];
},
updateFlightPosition: (state, action: PayloadAction<[string, LatLng]>) => {
const [id, position] = action.payload;
const ato = id in state.blue ? state.blue : state.red;
ato[id].position = position;
state.flights[id].position = position;
},
},
});