State refactored using reducer

This commit is contained in:
David
2021-03-10 19:27:36 +01:00
parent 61f83278bc
commit 8f33cb92f2
3 changed files with 57 additions and 12 deletions

View File

@@ -121,6 +121,7 @@
},
"mappings": {
"target": {
"zh": "zh-CN",
"zh_HANT": "zh-TW"
}
}

37
utils/reducer.ts Normal file
View File

@@ -0,0 +1,37 @@
export enum Actions {
"SET_FIELD",
"SET_ALL"
}
export const initialState = {
source: "auto",
target: "en",
query: ""
}
type Action = {
type: Actions.SET_FIELD,
payload: {
key: string,
value: string
}
} | {
type: Actions.SET_ALL,
payload: {
state: {
[key: string]: string
}
}
}
export default function reducer(state: typeof initialState, action: Action) {
switch (action.type) {
case Actions.SET_FIELD:
const { key, value } = action.payload;
return { ...state, [key]: value };
case Actions.SET_ALL:
return { ...state, ...action.payload.state };
default:
return state;
}
}