Farmbot-Web-App/frontend/config_storage/actions.ts

53 lines
1.6 KiB
TypeScript
Raw Normal View History

import { GetState } from "../redux/interfaces";
import { edit, save } from "../api/crud";
2018-10-22 09:35:44 -06:00
import {
BooleanConfigKey,
WebAppConfig,
NumberConfigKey,
2020-02-28 09:35:32 -07:00
StringConfigKey,
2018-10-22 09:35:44 -06:00
} from "farmbot/dist/resources/configs/web_app";
import { getWebAppConfig } from "../resources/getters";
/** Inverts boolean config key in WebAppConfig object, stored in the API. */
2018-10-22 09:35:44 -06:00
export function toggleWebAppBool(key: BooleanConfigKey) {
return (dispatch: Function, getState: GetState) => {
const conf = getWebAppConfig(getState().resources.index);
if (conf) {
2018-08-29 18:55:25 -06:00
const val = !(conf.body as WebAppConfig)[key];
dispatch(edit(conf, { [key]: val }));
dispatch(save(conf.uuid));
} else {
throw new Error("Toggled settings before app was loaded.");
}
};
}
2018-01-31 21:27:04 -07:00
2018-02-15 03:51:38 -07:00
type WebAppConfigKey =
2018-10-22 09:35:44 -06:00
BooleanConfigKey
| NumberConfigKey
| StringConfigKey;
2018-02-15 03:51:38 -07:00
type WebAppConfigValue = boolean | number | string | undefined;
export type GetWebAppConfigValue = (k: WebAppConfigKey) => WebAppConfigValue;
2018-01-31 21:27:04 -07:00
export function getWebAppConfigValue(getState: GetState) {
2018-02-15 03:51:38 -07:00
return (key: WebAppConfigKey): WebAppConfigValue => {
2018-01-31 21:27:04 -07:00
const conf = getWebAppConfig(getState().resources.index);
2018-08-29 18:55:25 -06:00
return conf && (conf.body as WebAppConfig)[key];
2018-01-31 21:27:04 -07:00
};
}
2018-02-15 03:51:38 -07:00
export function setWebAppConfigValue(
key: WebAppConfigKey, value: WebAppConfigValue) {
return (dispatch: Function, getState: GetState) => {
const conf = getWebAppConfig(getState().resources.index);
if (conf) {
dispatch(edit(conf, { [key]: value }));
dispatch(save(conf.uuid));
} else {
throw new Error("Changed settings before app was loaded.");
}
};
}