aboutsummaryrefslogtreecommitdiff
path: root/src/settings/actions
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-07 21:16:47 +0900
committerGitHub <noreply@github.com>2019-05-07 21:16:47 +0900
commit05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch)
tree2c7708ca91ac2b462cc86aa28612e3d3943496f3 /src/settings/actions
parent457d954e08923b4accd28a919c72d0b61db1bb98 (diff)
parent27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff)
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'src/settings/actions')
-rw-r--r--src/settings/actions/index.js7
-rw-r--r--src/settings/actions/index.ts36
-rw-r--r--src/settings/actions/setting.js63
-rw-r--r--src/settings/actions/setting.ts73
4 files changed, 109 insertions, 70 deletions
diff --git a/src/settings/actions/index.js b/src/settings/actions/index.js
deleted file mode 100644
index 016f2a5..0000000
--- a/src/settings/actions/index.js
+++ /dev/null
@@ -1,7 +0,0 @@
-export default {
- // Settings
- SETTING_SET_SETTINGS: 'setting.set.settings',
- SETTING_SHOW_ERROR: 'setting.show.error',
- SETTING_SWITCH_TO_FORM: 'setting.switch.to.form',
- SETTING_SWITCH_TO_JSON: 'setting.switch.to.json',
-};
diff --git a/src/settings/actions/index.ts b/src/settings/actions/index.ts
new file mode 100644
index 0000000..b1e996e
--- /dev/null
+++ b/src/settings/actions/index.ts
@@ -0,0 +1,36 @@
+import {
+ JSONSettings, FormSettings, SettingSource,
+} from '../../shared/SettingData';
+
+// Settings
+export const SETTING_SET_SETTINGS = 'setting.set.settings';
+export const SETTING_SHOW_ERROR = 'setting.show.error';
+export const SETTING_SWITCH_TO_FORM = 'setting.switch.to.form';
+export const SETTING_SWITCH_TO_JSON = 'setting.switch.to.json';
+
+interface SettingSetSettingsAcion {
+ type: typeof SETTING_SET_SETTINGS;
+ source: SettingSource;
+ json?: JSONSettings;
+ form?: FormSettings;
+}
+
+interface SettingShowErrorAction {
+ type: typeof SETTING_SHOW_ERROR;
+ error: string;
+ json: JSONSettings;
+}
+
+interface SettingSwitchToFormAction {
+ type: typeof SETTING_SWITCH_TO_FORM;
+ form: FormSettings,
+}
+
+interface SettingSwitchToJsonAction {
+ type: typeof SETTING_SWITCH_TO_JSON;
+ json: JSONSettings,
+}
+
+export type SettingAction =
+ SettingSetSettingsAcion | SettingShowErrorAction |
+ SettingSwitchToFormAction | SettingSwitchToJsonAction;
diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js
deleted file mode 100644
index db63a45..0000000
--- a/src/settings/actions/setting.js
+++ /dev/null
@@ -1,63 +0,0 @@
-import actions from 'settings/actions';
-import * as validator from 'shared/settings/validator';
-import * as settingsValues from 'shared/settings/values';
-import * as settingsStorage from 'shared/settings/storage';
-import keymaps from '../keymaps';
-
-const load = async() => {
- let settings = await settingsStorage.loadRaw();
- return set(settings);
-};
-
-const save = async(settings) => {
- try {
- if (settings.source === 'json') {
- let value = JSON.parse(settings.json);
- validator.validate(value);
- }
- } catch (e) {
- return {
- type: actions.SETTING_SHOW_ERROR,
- error: e.toString(),
- json: settings.json,
- };
- }
- await settingsStorage.save(settings);
- return set(settings);
-};
-
-const switchToForm = (json) => {
- try {
- validator.validate(JSON.parse(json));
- let form = settingsValues.formFromJson(json, keymaps.allowedOps);
- return {
- type: actions.SETTING_SWITCH_TO_FORM,
- form,
- };
- } catch (e) {
- return {
- type: actions.SETTING_SHOW_ERROR,
- error: e.toString(),
- json,
- };
- }
-};
-
-const switchToJson = (form) => {
- let json = settingsValues.jsonFromForm(form);
- return {
- type: actions.SETTING_SWITCH_TO_JSON,
- json,
- };
-};
-
-const set = (settings) => {
- return {
- type: actions.SETTING_SET_SETTINGS,
- source: settings.source,
- json: settings.json,
- form: settings.form,
- };
-};
-
-export { load, save, set, switchToForm, switchToJson };
diff --git a/src/settings/actions/setting.ts b/src/settings/actions/setting.ts
new file mode 100644
index 0000000..9eb416e
--- /dev/null
+++ b/src/settings/actions/setting.ts
@@ -0,0 +1,73 @@
+import * as actions from './index';
+import * as storages from '../storage';
+import SettingData, {
+ JSONSettings, FormSettings, SettingSource,
+} from '../../shared/SettingData';
+
+const load = async(): Promise<actions.SettingAction> => {
+ let data = await storages.load();
+ return set(data);
+};
+
+const save = async(data: SettingData): Promise<actions.SettingAction> => {
+ try {
+ if (data.getSource() === SettingSource.JSON) {
+ // toSettings exercise validation
+ data.toSettings();
+ }
+ } catch (e) {
+ return {
+ type: actions.SETTING_SHOW_ERROR,
+ error: e.toString(),
+ json: data.getJSON(),
+ };
+ }
+ await storages.save(data);
+ return set(data);
+};
+
+const switchToForm = (json: JSONSettings): actions.SettingAction => {
+ try {
+ // toSettings exercise validation
+ let form = FormSettings.fromSettings(json.toSettings());
+ return {
+ type: actions.SETTING_SWITCH_TO_FORM,
+ form,
+ };
+ } catch (e) {
+ return {
+ type: actions.SETTING_SHOW_ERROR,
+ error: e.toString(),
+ json,
+ };
+ }
+};
+
+const switchToJson = (form: FormSettings): actions.SettingAction => {
+ let json = JSONSettings.fromSettings(form.toSettings());
+ return {
+ type: actions.SETTING_SWITCH_TO_JSON,
+ json,
+ };
+};
+
+const set = (data: SettingData): actions.SettingAction => {
+ let source = data.getSource();
+ switch (source) {
+ case SettingSource.JSON:
+ return {
+ type: actions.SETTING_SET_SETTINGS,
+ source: source,
+ json: data.getJSON(),
+ };
+ case SettingSource.Form:
+ return {
+ type: actions.SETTING_SET_SETTINGS,
+ source: source,
+ form: data.getForm(),
+ };
+ }
+ throw new Error(`unknown source: ${source}`);
+};
+
+export { load, save, set, switchToForm, switchToJson };