You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lean4game/client/src/state/progress.ts

31 lines
865 B
TypeScript

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 }
return state.progress.code[world][level];
}
}
export const { codeEdited } = progressSlice.actions