index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import Router from 'next/router';
  2. import axios from 'axios';
  3. import cookie from 'js-cookie';
  4. import decodeJwt from 'jwt-decode';
  5. import * as types from './actionTypes';
  6. /* Homepage input actions */
  7. const addUrl = payload => ({ type: types.ADD_URL, payload });
  8. const listUrls = payload => ({ type: types.LIST_URLS, payload });
  9. const updateUrlList = payload => ({ type: types.UPDATE_URL_LIST, payload });
  10. const deleteUrl = payload => ({ type: types.DELETE_URL, payload });
  11. const showShortenerLoading = () => ({ type: types.SHORTENER_LOADING });
  12. const showTableLoading = () => ({ type: types.TABLE_LOADING });
  13. export const setShortenerFormError = payload => ({ type: types.SHORTENER_ERROR, payload });
  14. export const createShortUrl = params => dispatch => {
  15. dispatch(showShortenerLoading());
  16. return axios
  17. .post('/api/url/submit', params, { headers: { Authorization: cookie.get('token') } })
  18. .then(({ data }) => dispatch(addUrl(data)))
  19. .catch(({ response }) => dispatch(setShortenerFormError(response.data.error)));
  20. };
  21. export const getUrlsList = params => (dispatch, getState) => {
  22. if (params) dispatch(updateUrlList(params));
  23. dispatch(showTableLoading());
  24. return axios
  25. .post('/api/url/geturls', getState().url, { headers: { Authorization: cookie.get('token') } })
  26. .then(({ data }) => dispatch(listUrls(data)));
  27. };
  28. export const deleteShortUrl = params => dispatch => {
  29. dispatch(showTableLoading());
  30. return axios
  31. .post('/api/url/deleteurl', params, { headers: { Authorization: cookie.get('token') } })
  32. .then(() => dispatch(deleteUrl(params.id)))
  33. .catch(({ response }) => dispatch(setShortenerFormError(response.data.error)));
  34. };
  35. /* Page loading actions */
  36. export const showPageLoading = () => ({ type: types.SHOW_PAGE_LOADING });
  37. export const hidePageLoading = () => ({ type: types.HIDE_PAGE_LOADING });
  38. /* Settings actions */
  39. export const setDomain = payload => ({ type: types.SET_DOMAIN, payload });
  40. export const setApiKey = payload => ({ type: types.SET_APIKEY, payload });
  41. const deleteDomain = () => ({ type: types.DELETE_DOMAIN });
  42. const setDomainError = payload => ({ type: types.DOMAIN_ERROR, payload });
  43. const showDomainLoading = () => ({ type: types.DOMAIN_LOADING });
  44. const showApiLoading = () => ({ type: types.API_LOADING });
  45. export const showDomainInput = () => ({ type: types.SHOW_DOMAIN_INPUT });
  46. export const getUserSettings = () => dispatch =>
  47. axios
  48. .post('/api/auth/usersettings', null, { headers: { Authorization: cookie.get('token') } })
  49. .then(({ data }) => {
  50. dispatch(setDomain(data.customDomain));
  51. dispatch(setApiKey(data.apikey));
  52. });
  53. export const setCustomDomain = params => dispatch => {
  54. dispatch(showDomainLoading());
  55. return axios
  56. .post('/api/url/customdomain', params, { headers: { Authorization: cookie.get('token') } })
  57. .then(({ data }) => dispatch(setDomain(data.customDomain)))
  58. .catch(({ response }) => dispatch(setDomainError(response.data.error)));
  59. };
  60. export const deleteCustomDomain = () => dispatch =>
  61. axios
  62. .delete('/api/url/customdomain', { headers: { Authorization: cookie.get('token') } })
  63. .then(() => dispatch(deleteDomain()))
  64. .catch(({ response }) => dispatch(setDomainError(response.data.error)));
  65. export const generateApiKey = () => dispatch => {
  66. dispatch(showApiLoading());
  67. return axios
  68. .post('/api/auth/generateapikey', null, { headers: { Authorization: cookie.get('token') } })
  69. .then(({ data }) => dispatch(setApiKey(data.apikey)));
  70. };
  71. /* Login & signup actions */
  72. export const authUser = payload => ({ type: types.AUTH_USER, payload: decodeJwt(payload).sub });
  73. export const unauthUser = () => ({ type: types.UNAUTH_USER });
  74. export const sentVerification = payload => ({ type: types.SENT_VERIFICATION, payload });
  75. export const showAuthError = payload => ({ type: types.AUTH_ERROR, payload });
  76. export const showLoginLoading = () => ({ type: types.LOGIN_LOADING });
  77. export const showSignupLoading = () => ({ type: types.SIGNUP_LOADING });
  78. export const authRenew = () => ({ type: types.AUTH_RENEW });
  79. export const signupUser = body => dispatch => {
  80. dispatch(showSignupLoading());
  81. return axios
  82. .post('/api/auth/signup', body)
  83. .then(res => {
  84. const { email } = res.data;
  85. dispatch(sentVerification(email));
  86. })
  87. .catch(err => dispatch(showAuthError(err.response.data.error)));
  88. };
  89. export const loginUser = body => dispatch => {
  90. dispatch(showLoginLoading());
  91. return axios
  92. .post('/api/auth/login', body)
  93. .then(res => {
  94. const { token } = res.data;
  95. cookie.set('token', token, { expires: 7 });
  96. dispatch(authRenew());
  97. dispatch(authUser(token));
  98. dispatch(showPageLoading());
  99. Router.push('/');
  100. })
  101. .catch(err => dispatch(showAuthError(err.response.data.error)));
  102. };
  103. export const logoutUser = () => dispatch => {
  104. dispatch(showPageLoading());
  105. cookie.remove('token');
  106. dispatch(unauthUser());
  107. return Router.push('/login');
  108. };
  109. export const renewAuthUser = () => (dispatch, getState) => {
  110. if (getState().auth.renew) return null;
  111. return axios
  112. .post('/api/auth/renew', null, { headers: { Authorization: cookie.get('token') } })
  113. .then(res => {
  114. const { token } = res.data;
  115. cookie.set('token', token, { expires: 7 });
  116. dispatch(authRenew());
  117. dispatch(authUser(token));
  118. })
  119. .catch(() => {
  120. cookie.remove('token');
  121. dispatch(unauthUser());
  122. });
  123. };