settings.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. BAN_URL,
  12. } from './actionTypes';
  13. const deleteDomain = () => ({ type: DELETE_DOMAIN });
  14. const setDomainError = payload => ({ type: DOMAIN_ERROR, payload });
  15. const showDomainLoading = () => ({ type: DOMAIN_LOADING });
  16. const showApiLoading = () => ({ type: API_LOADING });
  17. const urlBanned = () => ({ type: BAN_URL });
  18. export const setDomain = payload => ({ type: SET_DOMAIN, payload });
  19. export const setApiKey = payload => ({ type: SET_APIKEY, payload });
  20. export const showDomainInput = () => ({ type: SHOW_DOMAIN_INPUT });
  21. export const getUserSettings = () => async dispatch => {
  22. try {
  23. const { data } = await axios.get('/api/auth/usersettings', {
  24. headers: { Authorization: cookie.get('token') },
  25. });
  26. dispatch(setDomain(data.customDomain));
  27. dispatch(setApiKey(data.apikey));
  28. } catch (error) {
  29. //
  30. }
  31. };
  32. export const setCustomDomain = params => async dispatch => {
  33. dispatch(showDomainLoading());
  34. try {
  35. const { data } = await axios.post('/api/url/customdomain', params, {
  36. headers: { Authorization: cookie.get('token') },
  37. });
  38. dispatch(setDomain(data.customDomain));
  39. } catch ({ response }) {
  40. dispatch(setDomainError(response.data.error));
  41. }
  42. };
  43. export const deleteCustomDomain = () => async dispatch => {
  44. try {
  45. await axios.delete('/api/url/customdomain', {
  46. headers: { Authorization: cookie.get('token') },
  47. });
  48. dispatch(deleteDomain());
  49. } catch ({ response }) {
  50. dispatch(setDomainError(response.data.error));
  51. }
  52. };
  53. export const generateApiKey = () => async dispatch => {
  54. dispatch(showApiLoading());
  55. try {
  56. const { data } = await axios.post('/api/auth/generateapikey', null, {
  57. headers: { Authorization: cookie.get('token') },
  58. });
  59. dispatch(setApiKey(data.apikey));
  60. } catch (error) {
  61. //
  62. }
  63. };
  64. export const banUrl = params => async dispatch => {
  65. try {
  66. const { data } = await axios.post('/api/url/admin/ban', params, {
  67. headers: { Authorization: cookie.get('token') },
  68. });
  69. dispatch(urlBanned());
  70. return data.message;
  71. } catch ({ response }) {
  72. return Promise.reject(response.data && response.data.error);
  73. }
  74. };