aboutsummaryrefslogtreecommitdiff
path: root/src/settings/reducers/setting.ts
blob: 31544bbb5bd24daa849462168f20a4d97bd06046 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as actions from "../actions";
import {
  JSONTextSettings,
  FormSettings,
  SettingSource,
} from "../../shared/SettingData";
import { DefaultSetting } from "../../shared/settings/Settings";

export interface State {
  source: SettingSource;
  json?: JSONTextSettings;
  form?: FormSettings;
  error: string;
}

const defaultState: State = {
  source: SettingSource.JSON,
  json: JSONTextSettings.fromText(""),
  form: FormSettings.fromSettings(DefaultSetting),
  error: "",
};

export default function reducer(
  state = defaultState,
  action: actions.SettingAction
): State {
  switch (action.type) {
    case actions.SETTING_SET_SETTINGS:
      return {
        ...state,
        source: action.source,
        json: action.json,
        form: action.form,
        error: "",
      };
    case actions.SETTING_SHOW_ERROR:
      return { ...state, error: action.error, json: action.json };
    case actions.SETTING_SWITCH_TO_FORM:
      return {
        ...state,
        error: "",
        source: SettingSource.Form,
        form: action.form,
      };
    case actions.SETTING_SWITCH_TO_JSON:
      return {
        ...state,
        error: "",
        source: SettingSource.JSON,
        json: action.json,
      };
    default:
      return state;
  }
}