aboutsummaryrefslogtreecommitdiff
path: root/src/settings/actions/setting.js
blob: 92c9f8a06c14d9b62af38029ed556a767327f823 (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
import actions from 'settings/actions';
import messages from 'shared/messages';
import DefaultSettings from 'shared/settings/default';
import * as settingsStorage from 'shared/settings/storage';
import * as settingsValues from 'shared/settings/values';

const load = () => {
  return settingsStorage.loadRaw().then((settings) => {
    return set(settings);
  });
};

const save = (settings) => {
  return settingsStorage.save(settings).then(() => {
    return browser.runtime.sendMessage({
      type: messages.SETTINGS_RELOAD
    });
  }).then(() => {
    return set(settings);
  });
};

const set = (settings) => {
  let value = JSON.parse(DefaultSettings.json);
  if (settings.source === 'json') {
    value = settingsValues.valueFromJson(settings.json);
  } else if (settings.source === 'form') {
    value = settingsValues.valueFromForm(settings.form);
  }

  return {
    type: actions.SETTING_SET_SETTINGS,
    source: settings.source,
    json: settings.json,
    form: settings.form,
    value,
  };
};

export { load, save };