settings.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { action, Action, thunk, Thunk } from "easy-peasy";
  2. import axios from "axios";
  3. import { API } from "../consts";
  4. import { getAxiosConfig } from "../utils";
  5. interface Domain {
  6. customDomain: string;
  7. homepage: string;
  8. }
  9. export interface Settings {
  10. domains: Array<Domain>;
  11. apikey: string;
  12. setApiKey: Action<Settings, string>;
  13. generateApiKey: Thunk<Settings>;
  14. addDomain: Action<Settings, Domain>;
  15. removeDomain: Action<Settings>;
  16. saveDomain: Thunk<Settings, Domain>;
  17. deleteDomain: Thunk<Settings>;
  18. }
  19. export const settings: Settings = {
  20. domains: [],
  21. apikey: null,
  22. setApiKey: action((state, payload) => {
  23. state.apikey = payload;
  24. }),
  25. generateApiKey: thunk(async actions => {
  26. const res = await axios.post(API.GENERATE_APIKEY, null, getAxiosConfig());
  27. actions.setApiKey(res.data.apikey);
  28. }),
  29. addDomain: action((state, payload) => {
  30. state.domains.push(payload);
  31. }),
  32. removeDomain: action(state => {
  33. state.domains = [];
  34. }),
  35. saveDomain: thunk(async (actions, payload) => {
  36. const res = await axios.post(API.CUSTOM_DOMAIN, payload, getAxiosConfig());
  37. actions.addDomain({
  38. customDomain: res.data.customDomain,
  39. homepage: res.data.homepage
  40. });
  41. }),
  42. deleteDomain: thunk(async actions => {
  43. await axios.delete(API.CUSTOM_DOMAIN, getAxiosConfig());
  44. actions.removeDomain();
  45. })
  46. };