F1Manager-Calc/libs/reducers/configReducer.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-04-18 07:40:11 +02:00
import { createSlice } from '@reduxjs/toolkit'
2023-04-18 09:23:55 +02:00
import {useSession} from "next-auth/react";
const getDefaultSlotConfig = i => ({
id: i,
slotNaming: `car_${i}`,
slotTitle: `Slot ${i}`,
});
const totalSlots = 4;
2023-04-18 07:40:11 +02:00
export const configSlice = createSlice({
name: 'config',
initialState: {
2023-04-18 09:23:55 +02:00
slots: Array.from(Array(totalSlots)).map((x, i) => getDefaultSlotConfig(i+1)),
2023-04-18 07:40:11 +02:00
},
reducers: {
2023-04-18 09:23:55 +02:00
setSlots: (state, { payload }) => {
state.slots = payload
},
addSlot: (state) => {
let nextAvailableSlotId = 1;
for(const slot of state.slots) {
if (nextAvailableSlotId === slot.id) {
nextAvailableSlotId++;
}
}
state.slots = [...state.slots, getDefaultSlotConfig(nextAvailableSlotId)]
2023-04-18 07:40:11 +02:00
},
2023-04-18 09:23:55 +02:00
renameSlot: (state, { payload: { id, slotTitle } }) => {
state.slots = state.slots.map(
(x, _idx) =>
x.id === id ? {...x, slotTitle} : x
)
2023-04-18 07:40:11 +02:00
},
2023-04-18 09:23:55 +02:00
removeSlot: (state, { payload: { id } }) => {
state.slots = state.slots.filter((x) => x.id !== id )
2023-04-18 07:40:11 +02:00
},
},
})
// Action creators are generated for each case reducer function
2023-04-18 09:23:55 +02:00
export const {
setSlots,
renameSlot,
addSlot,
removeSlot,
} = configSlice.actions
2023-04-18 07:40:11 +02:00
export default configSlice.reducer