save state of code

This commit is contained in:
Alexander Bentkamp
2022-12-14 14:02:57 +01:00
parent 4114cbc304
commit 32b9d028a7
7 changed files with 63 additions and 15 deletions
+30
View File
@@ -0,0 +1,30 @@
import { createSlice } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'
interface ProgressState {
code: {[world: string]: {[level: number]: string}}
}
const initialState = { code: {} } as ProgressState
export const progressSlice = createSlice({
name: 'progress',
initialState,
reducers: {
codeEdited(state, action: PayloadAction<{world: string, level: number, code: string}>) {
if (!state.code[action.payload.world]) {
state.code[action.payload.world] = {}
}
state.code[action.payload.world][action.payload.level] = action.payload.code
},
}
})
export function selectCode(world: string, level: number) {
return (state) => {
if (!state.progress.code[world]) { return undefined }
state.progress.code[world][level];
}
}
export const { codeEdited } = progressSlice.actions