settings.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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: { apikey, customDomain, homepage, useHttps } } = await axios.get(
  24. '/api/auth/usersettings',
  25. {
  26. headers: { Authorization: cookie.get('token') },
  27. }
  28. );
  29. dispatch(setDomain({ customDomain, homepage, useHttps }));
  30. dispatch(setApiKey(apikey));
  31. } catch (error) {
  32. //
  33. }
  34. };
  35. export const setCustomDomain = params => async dispatch => {
  36. dispatch(showDomainLoading());
  37. try {
  38. const { data: { customDomain, homepage, useHttps } } = await axios.post(
  39. '/api/url/customdomain',
  40. params,
  41. {
  42. headers: { Authorization: cookie.get('token') },
  43. }
  44. );
  45. dispatch(setDomain({ customDomain, homepage, useHttps }));
  46. } catch ({ response }) {
  47. dispatch(setDomainError(response.data.error));
  48. }
  49. };
  50. export const deleteCustomDomain = () => async dispatch => {
  51. try {
  52. await axios.delete('/api/url/customdomain', {
  53. headers: { Authorization: cookie.get('token') },
  54. });
  55. dispatch(deleteDomain());
  56. } catch ({ response }) {
  57. dispatch(setDomainError(response.data.error));
  58. }
  59. };
  60. export const generateApiKey = () => async dispatch => {
  61. dispatch(showApiLoading());
  62. try {
  63. const { data } = await axios.post('/api/auth/generateapikey', null, {
  64. headers: { Authorization: cookie.get('token') },
  65. });
  66. dispatch(setApiKey(data.apikey));
  67. } catch (error) {
  68. //
  69. }
  70. };
  71. export const banUrl = params => async dispatch => {
  72. try {
  73. const { data } = await axios.post('/api/url/admin/ban', params, {
  74. headers: { Authorization: cookie.get('token') },
  75. });
  76. dispatch(urlBanned());
  77. return data.message;
  78. } catch ({ response }) {
  79. return Promise.reject(response.data && response.data.error);
  80. }
  81. };