Skip to content

Commit

Permalink
fix: empty state leads to array instead of empty object
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaeldehta committed Mar 13, 2022
1 parent 486a777 commit 95800d7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/redux/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Answer} from "../types";

const element = document.getElementById("exercise-container");


const initialAnswerState: Answer = element?.dataset.answer? JSON.parse(element.dataset.answer) : {
consequence: "",
statements: {
Expand All @@ -12,6 +13,10 @@ const initialAnswerState: Answer = element?.dataset.answer? JSON.parse(element.d
predicateLogic: false
};

if(Array.isArray(initialAnswerState.statements.entries as unknown)) {
initialAnswerState.statements.entries = {};
}

const answerSlice = createSlice({
name: "answer",
initialState: initialAnswerState,
Expand All @@ -26,10 +31,15 @@ const answerSlice = createSlice({
const newId = nanoid()
state.statements.ids.push(newId);
state.statements.entries[newId] = ""
},
removeStatement(state, action: PayloadAction<string>) {
const index = state.statements.ids.indexOf(action.payload);
state.statements.ids.splice(index, 1);
delete state.statements.entries[action.payload];
}
}
})

export const {setConsequence, setStatement, addStatement} = answerSlice.actions;
export const {setConsequence, setStatement, addStatement, removeStatement} = answerSlice.actions;

export default answerSlice.reducer;
4 changes: 4 additions & 0 deletions src/redux/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const createResponseReducer = async () => {
lines: {}
};

if(Array.isArray(initialState.lines as unknown)) {
initialState.lines = {};
}

const {type} = element.dataset;

const {default: builderCallback}: {default: (builder: ActionReducerMapBuilder<Response>) => ActionReducerMapBuilder<Response>} = await import(`./${type}`);
Expand Down

0 comments on commit 95800d7

Please sign in to comment.