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