Language switch implemented
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
export enum Actions {
|
||||
"SET_FIELD",
|
||||
"SET_ALL"
|
||||
}
|
||||
import { replaceBoth } from "./language";
|
||||
|
||||
export const initialState = {
|
||||
source: "auto",
|
||||
@@ -9,6 +6,14 @@ export const initialState = {
|
||||
query: ""
|
||||
}
|
||||
|
||||
type State = typeof initialState;
|
||||
|
||||
export enum Actions {
|
||||
SET_FIELD,
|
||||
SET_ALL,
|
||||
SWITCH_LANGS
|
||||
}
|
||||
|
||||
type Action = {
|
||||
type: Actions.SET_FIELD,
|
||||
payload: {
|
||||
@@ -18,19 +23,31 @@ type Action = {
|
||||
} | {
|
||||
type: Actions.SET_ALL,
|
||||
payload: {
|
||||
state: {
|
||||
[key: string]: string
|
||||
}
|
||||
state: State
|
||||
}
|
||||
} | {
|
||||
type: Actions.SWITCH_LANGS
|
||||
}
|
||||
|
||||
export default function reducer(state: typeof initialState, action: Action) {
|
||||
export default function reducer(state: State, 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 };
|
||||
case Actions.SWITCH_LANGS:
|
||||
const { source, target } = replaceBoth("exception", {
|
||||
source: state.target,
|
||||
target: state.source
|
||||
});
|
||||
return {
|
||||
...state,
|
||||
source: source !== target
|
||||
? source
|
||||
: initialState.source,
|
||||
target
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user