settings.js 2.5 KB

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