F1Manager-Calc/libs/reducers/configReducer.js

110 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-04-18 10:13:38 +02:00
import {createSlice} from '@reduxjs/toolkit'
2023-04-18 09:23:55 +02:00
const getDefaultSlotConfig = i => ({
id: i,
slotNaming: `car_${i}`,
slotTitle: `Slot ${i}`,
2023-04-18 11:00:08 +02:00
carSetup: [0.5, 0.5, 0.5, 0.5, 0.5],
prevCarSetup: [0.5, 0.5, 0.5, 0.5, 0.5],
feedback: [[], [], [], [], []],
track: "XX",
previousRuns: [],
2023-04-18 09:23:55 +02:00
});
const totalSlots = 4;
2023-04-18 10:13:38 +02:00
const initialSlots = Array.from(Array(totalSlots)).map((x, i) => getDefaultSlotConfig(i+1));
2023-04-18 07:40:11 +02:00
2023-04-18 14:14:23 +02:00
const legacyConfigReader = () => {
2023-04-18 14:20:46 +02:00
if (typeof window === "undefined") return null;
if (localStorage.config) {
try {
let config = JSON.parse(localStorage.config);
const ret = config.slots.map(x => {
let data = {};
try {
data = JSON.parse(localStorage[x.slotNaming]);
} catch {
2023-04-18 14:14:23 +02:00
2023-04-18 14:20:46 +02:00
}
return {
id: x.id,
slotNaming: x.slotNaming,
slotTitle: x.slotTitle,
2023-04-21 18:14:14 +02:00
carSetup: [0.5, 0.5, 0.5, 0.5, 0.5],
prevCarSetup: [0.5, 0.5, 0.5, 0.5, 0.5],
feedback: [[], [], [], [], []],
track: "XX",
previousRuns: [],
2023-04-18 14:20:46 +02:00
...data
};
})
for(const x of ret) {
delete localStorage[x.slotNaming]
2023-04-18 14:14:23 +02:00
}
2023-04-18 14:20:46 +02:00
delete localStorage.config
return ret
} catch {
return null;
2023-04-18 14:14:23 +02:00
}
}
2023-04-18 14:20:46 +02:00
if (localStorage["persist:root"]) {
try {
let config = JSON.parse(localStorage["persist:root"]);
return JSON.parse(config.slots);
} catch {
return null;
}
}
2023-04-18 14:14:23 +02:00
}
2023-04-18 07:40:11 +02:00
export const configSlice = createSlice({
name: 'config',
initialState: {
2023-04-18 14:14:23 +02:00
slots: legacyConfigReader() || initialSlots,
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 16:44:57 +02:00
removeSlot: (state, { payload: { id, slotTitle } }) => {
state.slots = state.slots.filter(
(x, _idx) => x.id !== id
)
},
2023-04-18 11:00:08 +02:00
updateSlot: (state, { payload: { id, payload } }) => {
state.slots = [...state.slots.map(
x => x.id === id ? {...x, ...payload} : x
)]
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,
2023-04-18 11:00:08 +02:00
updateSlot,
2023-04-18 09:23:55 +02:00
removeSlot,
} = configSlice.actions
2023-04-18 07:40:11 +02:00
export default configSlice.reducer