Farmbot-Web-App/frontend/redux/revert_to_english_middlewar...

37 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import { Middleware, DeepPartial } from "redux";
2018-01-23 14:00:43 -07:00
import { MiddlewareConfig } from "./middlewares";
import { ResourceName, TaggedWebAppConfig } from "farmbot";
2018-10-29 15:02:24 -06:00
import { Actions } from "../constants";
2018-01-23 14:00:43 -07:00
import { revertToEnglish } from "../revert_to_english";
import { SyncResponse } from "../sync/actions";
import { arrayUnwrap } from "../resources/util";
2018-10-22 09:35:44 -06:00
2018-01-23 14:00:43 -07:00
const WEB_APP_CONFIG: ResourceName = "WebAppConfig";
/**
* When the app first loads, everything is localized. Some users do not wish to
* localize the app and would like instead to leave the app in English.
*
* Unfortunately, the setting to do this lives on the API in the `WebAppConfig`
* table and we do not have access to that information until after the app
* bootstraps.
*
* To get around this limitation, we internationalize the app as soon as
* possible and then revert to english as soon as we have a chance to read the
* value of `web_app_config.disable_i18n`.
*/
// tslint:disable-next-line:no-any
2018-01-23 14:00:43 -07:00
const fn: Middleware = () => (dispatch) => (action: any) => {
const x: DeepPartial<SyncResponse<TaggedWebAppConfig>> = action;
2020-01-03 13:04:45 -07:00
if (x?.type === Actions.RESOURCE_READY
&& x.payload?.body
&& x.payload.kind === WEB_APP_CONFIG) {
const conf = arrayUnwrap(x.payload.body);
2020-01-03 13:04:45 -07:00
conf?.body?.disable_i18n && revertToEnglish();
2018-01-23 14:00:43 -07:00
}
return dispatch(action);
};
export const revertToEnglishMiddleware: MiddlewareConfig = { fn, env: "*" };