settings.js 2.4 KB

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