feat: split settings slice to staging (ui) and production (in effect)

This commit is contained in:
鲁树人 committed 2023-06-10 13:10:21 +01:00
1 parent 865dcae931
commit f46f36415d
5 files changed
+151 -117

No files matched your search

+85 -12
View File
@@ -1,28 +1,92 @@
import { createSlice } from '@reduxjs/toolkit';
import type { PayloadAction } from '@reduxjs/toolkit';
import type { RootState } from '~/store';
import { nanoid } from 'nanoid';
import { objectify } from 'radash';
export interface QMCSettings {
keys: Record<string, string>; // { [fileName]: ekey }
export interface StagingSettings {
qmc2: {
keys: { id: string; name: string; key: string }[];
};
}
export interface ProductionSettings {
qmc2: {
keys: Record<string, string>; // { [fileName]: ekey }
};
}
export interface SettingsState {
qmc2: QMCSettings;
staging: StagingSettings;
production: ProductionSettings;
}
const initialState: SettingsState = {
qmc2: { keys: {} },
staging: {
qmc2: {
keys: [],
},
},
production: {
qmc2: {
keys: {},
},
},
};
const stagingToProduction = (staging: StagingSettings): ProductionSettings => ({
qmc2: {
keys: objectify(
staging.qmc2.keys,
(item) => item.name,
(item) => item.key
),
},
});
const productionToStaging = (production: ProductionSettings): StagingSettings => ({
qmc2: {
keys: Object.entries(production.qmc2.keys).map(([name, key]) => ({ id: nanoid(), name, key })),
},
});
export const settingsSlice = createSlice({
name: 'settings',
initialState,
reducers: {
updateSettings: (_state, { payload }: PayloadAction<SettingsState>) => {
return payload;
setProductionChanges: (_state, { payload }: PayloadAction<ProductionSettings>) => {
return {
production: payload,
staging: productionToStaging(payload),
};
},
updateQMC2Keys: (state, { payload }: PayloadAction<QMCSettings['keys']>) => {
state.qmc2.keys = payload;
qmc2AddKey(state) {
state.staging.qmc2.keys.push({ id: nanoid(), name: '', key: '' });
},
qmc2DeleteKey(state, { payload: { id } }: PayloadAction<{ id: string }>) {
const qmc2 = state.staging.qmc2;
qmc2.keys = qmc2.keys.filter((item) => item.id !== id);
},
qmc2UpdateKey(
state,
{ payload: { id, field, value } }: PayloadAction<{ id: string; field: 'name' | 'key'; value: string }>
) {
const keyItem = state.staging.qmc2.keys.find((item) => item.id === id);
if (keyItem) {
keyItem[field] = value;
}
},
qmc2ClearKeys(state) {
state.staging.qmc2.keys = [];
},
discardStagingChanges: (state) => {
state.staging = productionToStaging(state.production);
},
commitStagingChange: (state) => {
const production = stagingToProduction(state.staging);
return {
// Sync back to staging
staging: productionToStaging(production),
production,
};
},
resetConfig: () => {
return initialState;
@@ -30,8 +94,17 @@ export const settingsSlice = createSlice({
},
});
export const { updateSettings, resetConfig, updateQMC2Keys } = settingsSlice.actions;
export const {
setProductionChanges,
resetConfig,
export const selectQM2CSettings = (state: RootState) => state.settings.qmc2;
qmc2AddKey,
qmc2UpdateKey,
qmc2DeleteKey,
qmc2ClearKeys,
commitStagingChange,
discardStagingChanges,
} = settingsSlice.actions;
export default settingsSlice.reducer;