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",
|
2021-03-18 23:47:12 +01:00
|
|
|
query: "",
|
|
|
|
|
delayedQuery: "",
|
2021-04-01 15:40:25 +02:00
|
|
|
translation: "",
|
|
|
|
|
isLoading: true
|
2021-03-10 19:27:36 +01:00
|
|
|
}
|
|
|
|
|
|
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,
|
2021-04-01 15:40:25 +02:00
|
|
|
value: any
|
2021-03-10 19:27:36 +01:00
|
|
|
}
|
|
|
|
|
} | {
|
|
|
|
|
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-04-30 23:11:26 +02:00
|
|
|
const { source, target } = replaceBoth("exception", {
|
|
|
|
|
source: state.target,
|
|
|
|
|
target: state.source
|
|
|
|
|
});
|
|
|
|
|
|
2021-03-10 19:27:36 +01:00
|
|
|
switch (action.type) {
|
|
|
|
|
case Actions.SET_FIELD:
|
|
|
|
|
const { key, value } = action.payload;
|
2021-04-30 23:11:26 +02:00
|
|
|
if (key === "source" && value === state.target)
|
|
|
|
|
return { ...state, [key]: value, target: target !== value ? target : "eo" };
|
|
|
|
|
if (key === "target" && value === state.source)
|
|
|
|
|
return { ...state, [key]: value, source };
|
2021-03-10 19:27:36 +01:00
|
|
|
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:
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
source: source !== target
|
|
|
|
|
? source
|
|
|
|
|
: initialState.source,
|
2021-03-18 23:47:12 +01:00
|
|
|
target,
|
|
|
|
|
query: state.translation,
|
|
|
|
|
delayedQuery: state.translation,
|
|
|
|
|
translation: ""
|
2021-03-10 21:31:50 +01:00
|
|
|
};
|
2021-03-10 19:27:36 +01:00
|
|
|
default:
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
}
|