Files
LingvAI/utils/reducer.ts

55 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-03-10 21:31:50 +01:00
import { replaceBoth } from "./language";
2021-03-10 19:27:36 +01:00
export const initialState = {
source: "auto",
target: "en",
query: ""
}
2021-03-10 21:31:50 +01:00
type State = typeof initialState;
export enum Actions {
SET_FIELD,
SET_ALL,
SWITCH_LANGS
}
2021-03-10 19:27:36 +01:00
type Action = {
type: Actions.SET_FIELD,
payload: {
key: string,
value: string
}
} | {
type: Actions.SET_ALL,
payload: {
2021-03-10 21:31:50 +01:00
state: State
2021-03-10 19:27:36 +01:00
}
2021-03-10 21:31:50 +01:00
} | {
type: Actions.SWITCH_LANGS
2021-03-10 19:27:36 +01:00
}
2021-03-10 21:31:50 +01:00
export default function reducer(state: State, action: Action) {
2021-03-10 19:27:36 +01:00
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 };
2021-03-10 21:31:50 +01:00
case Actions.SWITCH_LANGS:
const { source, target } = replaceBoth("exception", {
source: state.target,
target: state.source
});
return {
...state,
source: source !== target
? source
: initialState.source,
target
};
2021-03-10 19:27:36 +01:00
default:
return state;
}
}