save state of code
parent
4114cbc304
commit
32b9d028a7
@ -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
|
@ -1,19 +1,21 @@
|
||||
import { configureStore } from '@reduxjs/toolkit';
|
||||
import { connection } from './connection'
|
||||
import { connection } from '../connection'
|
||||
import thunkMiddleware from 'redux-thunk'
|
||||
import { gameApi } from './game/api'
|
||||
import { apiSlice } from './api'
|
||||
import { progressSlice } from './progress'
|
||||
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
[gameApi.reducerPath]: gameApi.reducer,
|
||||
[apiSlice.reducerPath]: apiSlice.reducer,
|
||||
[progressSlice.name]: progressSlice.reducer,
|
||||
},
|
||||
middleware: getDefaultMiddleware =>
|
||||
getDefaultMiddleware({
|
||||
thunk: {
|
||||
extraArgument: { connection }
|
||||
}
|
||||
}).concat(gameApi.middleware),
|
||||
}).concat(apiSlice.middleware),
|
||||
});
|
||||
|
||||
// Infer the `RootState` and `AppDispatch` types from the store itself
|
Loading…
Reference in New Issue