settings.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import axios from 'axios';
  2. import cookie from 'js-cookie';
  3. import {
  4. DELETE_DOMAIN,
  5. DOMAIN_ERROR,
  6. DOMAIN_LOADING,
  7. API_LOADING,
  8. SET_DOMAIN,
  9. SET_APIKEY,
  10. SHOW_DOMAIN_INPUT,
  11. } from './actionTypes';
  12. const deleteDomain = () => ({ type: DELETE_DOMAIN });
  13. const setDomainError = payload => ({ type: DOMAIN_ERROR, payload });
  14. const showDomainLoading = () => ({ type: DOMAIN_LOADING });
  15. const showApiLoading = () => ({ type: API_LOADING });
  16. export const setDomain = payload => ({ type: SET_DOMAIN, payload });
  17. export const setApiKey = payload => ({ type: SET_APIKEY, payload });
  18. export const showDomainInput = () => ({ type: SHOW_DOMAIN_INPUT });
  19. export const getUserSettings = () => async dispatch => {
  20. try {
  21. const { data } = await axios.get('/api/auth/usersettings', {
  22. headers: { Authorization: cookie.get('token') },
  23. });
  24. dispatch(setDomain(data.customDomain));
  25. dispatch(setApiKey(data.apikey));
  26. } catch (error) {
  27. //
  28. }
  29. };
  30. export const setCustomDomain = params => async dispatch => {
  31. dispatch(showDomainLoading());
  32. try {
  33. const { data } = await axios.post('/api/url/customdomain', params, {
  34. headers: { Authorization: cookie.get('token') },
  35. });
  36. dispatch(setDomain(data.customDomain));
  37. } catch ({ response }) {
  38. dispatch(setDomainError(response.data.error));
  39. }
  40. };
  41. export const deleteCustomDomain = () => async dispatch => {
  42. try {
  43. await axios.delete('/api/url/customdomain', {
  44. headers: { Authorization: cookie.get('token') },
  45. });
  46. dispatch(deleteDomain());
  47. } catch ({ response }) {
  48. dispatch(setDomainError(response.data.error));
  49. }
  50. };
  51. export const generateApiKey = () => async dispatch => {
  52. dispatch(showApiLoading());
  53. try {
  54. const { data } = await axios.post('/api/auth/generateapikey', null, {
  55. headers: { Authorization: cookie.get('token') },
  56. });
  57. dispatch(setApiKey(data.apikey));
  58. } catch (error) {
  59. //
  60. }
  61. };