Skip to content

Commit

Permalink
refactor: changeAll reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
marcustyphoon committed Oct 7, 2024
1 parent 2154110 commit 748302d
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 37 deletions.
4 changes: 3 additions & 1 deletion src/state/slices/boss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export const bossSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.boss };
if (action.payload?.form?.boss) {
return { ...state, ...action.payload?.form?.boss };
}
});
},
});
Expand Down
5 changes: 2 additions & 3 deletions src/state/slices/buffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ export const buffsSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
const removeOldBuffs = (data: Buffs | BuffAmounts) => {
if (!data) return data;
const removeOldBuffs = (data?: Buffs | BuffAmounts) => {
if (!data) return {};
const validEntries = Object.entries(data).filter(([key]) => buffModifiersById[key]);
return Object.fromEntries(validEntries);
};

const { buffs, amounts } = action.payload?.form?.buffs ?? {};
const validData = { buffs: removeOldBuffs(buffs), amounts: removeOldBuffs(amounts) };

console.log({ buffs, amounts, validData });
return { ...state, ...validData };
});

Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/controlsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ export const controlSlice = createSlice({
initialState,
reducers: {
changeAll: (state, action) => {
return { ...state, ...action.payload?.control };
if (action.payload?.control) {
return { ...state, ...action.payload.control };
}
},
changeProfession: (state, action: PayloadAction<ProfessionName>) => {
if (state.profession !== action.payload) {
Expand Down
10 changes: 6 additions & 4 deletions src/state/slices/distribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ export const distributionSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
const newState = clone(action.payload?.form?.distribution);
newState.values2.Power2 ??= 0;
newState.textBoxes.Power2 ??= '0';
if (action.payload?.form?.distribution) {
const newState = clone(action.payload.form.distribution);
newState.values2.Power2 ??= 0;
newState.textBoxes.Power2 ??= '0';

return { ...state, ...newState };
return { ...state, ...newState };
}
});

builder.addCase(setBuildTemplate, (state, action) => {
Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/extraModifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ export const extraModifiersSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.extraModifiers };
if (action.payload?.form?.extraModifiers) {
return { ...state, ...action.payload.form.extraModifiers };
}
});
},
});
Expand Down
38 changes: 20 additions & 18 deletions src/state/slices/extras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,27 @@ export const extrasSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
// best effort conversion of old data from before multiple selection
if (action.payload?.form?.extras?.Runes) {
const convertedExtras = mapValues(action.payload.form.extras, (data) => {
if (data === '') {
return {};
}
if (typeof data === 'string') {
return { [data]: {} };
}
if (Array.isArray(data)) {
return Object.fromEntries(data.map((key) => [key, {}]));
}
return data;
});

return { ...state, extras: convertedExtras };
if (action.payload?.form?.extras) {
// best effort conversion of old data from before multiple selection
if (action.payload.form.extras.Runes) {
const convertedExtras = mapValues(action.payload.form.extras, (data) => {
if (data === '') {
return {};
}
if (typeof data === 'string') {
return { [data]: {} };
}
if (Array.isArray(data)) {
return Object.fromEntries(data.map((key) => [key, {}]));
}
return data;
});

return { ...state, extras: convertedExtras };
}

return { ...state, ...action.payload.form.extras };
}

return { ...state, ...action.payload?.form?.extras };
});

builder.addCase(setBuildTemplate, (state, action) => {
Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/forcedSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export const forcedSlotsSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.forcedSlots };
if (action.payload?.form?.forcedSlots) {
return { ...state, ...action.payload.form.forcedSlots };
}
});
},
});
Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/infusions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ export const infusionsSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.infusions };
if (action.payload?.form?.infusions) {
return { ...state, ...action.payload.form.infusions };
}
});

builder.addCase(changeGameMode, (state, action) => {
Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/priorities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export const prioritiesSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.priorities };
if (action.payload?.form?.priorities) {
return { ...state, ...action.payload?.form?.priorities };
}
});

builder.addCase(setBuildTemplate, (state, action) => {
Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const skillsSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.skills };
if (action.payload?.form?.skills) {
return { ...state, ...action.payload.form.skills };
}
});

builder.addCase(changeProfession, (state, _action) => ({
Expand Down
4 changes: 3 additions & 1 deletion src/state/slices/traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export const traitsSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
return { ...state, ...action.payload?.form?.traits };
if (action.payload?.form?.traits) {
return { ...state, ...action.payload.form.traits };
}
});

builder.addCase(changeProfession, (state, _action) => ({
Expand Down
10 changes: 6 additions & 4 deletions src/state/slices/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export const userSettingsSlice = createSlice({

extraReducers: (builder) => {
builder.addCase(changeAll, (state, action) => {
if (action.payload?.userSettings?.gameMode) {
setQueryParm({ key: PARAMS.GAMEMODE, value: action.payload.userSettings.gameMode });
}
if (action.payload?.userSettings) {
if (action.payload.userSettings.gameMode) {
setQueryParm({ key: PARAMS.GAMEMODE, value: action.payload.userSettings.gameMode });
}

return { ...state, ...action.payload?.userSettings };
return { ...state, ...action.payload.userSettings };
}
});
},
});
Expand Down

0 comments on commit 748302d

Please sign in to comment.