mirror of
https://github.com/dcs-liberation/dcs_liberation.git
synced 2025-11-10 14:22:26 +00:00
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import { RootState } from "../app/store";
|
|
import { gameLoaded, gameUnloaded } from "./actions";
|
|
import { Tgo } from "./liberationApi";
|
|
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
|
|
|
|
interface TgosState {
|
|
tgos: { [key: string]: Tgo };
|
|
}
|
|
|
|
const initialState: TgosState = {
|
|
tgos: {},
|
|
};
|
|
|
|
export const tgosSlice = createSlice({
|
|
name: "tgos",
|
|
initialState,
|
|
reducers: {
|
|
updateTgo: (state, action: PayloadAction<Tgo>) => {
|
|
const tgo = action.payload;
|
|
state.tgos[tgo.id] = tgo;
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(gameLoaded, (state, action) => {
|
|
state.tgos = action.payload.tgos.reduce(
|
|
(acc: { [key: string]: Tgo }, curr) => {
|
|
acc[curr.id] = curr;
|
|
return acc;
|
|
},
|
|
{}
|
|
);
|
|
});
|
|
builder.addCase(gameUnloaded, (state) => {
|
|
state.tgos = {};
|
|
});
|
|
},
|
|
});
|
|
|
|
export const { updateTgo } = tgosSlice.actions;
|
|
|
|
export const selectTgos = (state: RootState) => state.tgos;
|
|
|
|
export default tgosSlice.reducer;
|