index.js 5.5 KB

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