Explorar el Código

Splitted actions logic

Oleh T hace 7 años
padre
commit
81a48f0224
Se han modificado 4 ficheros con 284 adiciones y 141 borrados
  1. 86 0
      client/actions/auth.js
  2. 60 141
      client/actions/index.js
  3. 67 0
      client/actions/settings.js
  4. 71 0
      client/actions/url.js

+ 86 - 0
client/actions/auth.js

@@ -0,0 +1,86 @@
+import Router from 'next/router';
+import axios from 'axios';
+import cookie from 'js-cookie';
+import decodeJwt from 'jwt-decode';
+import {
+  SET_DOMAIN,
+  SHOW_PAGE_LOADING,
+  HIDE_PAGE_LOADING,
+  AUTH_USER,
+  UNAUTH_USER,
+  SENT_VERIFICATION,
+  AUTH_ERROR,
+  LOGIN_LOADING,
+  SIGNUP_LOADING,
+  AUTH_RENEW,
+} from './actionTypes';
+
+const setDomain = payload => ({ type: SET_DOMAIN, payload });
+
+export const showPageLoading = () => ({ type: SHOW_PAGE_LOADING });
+export const hidePageLoading = () => ({ type: HIDE_PAGE_LOADING });
+export const authUser = payload => ({ type: AUTH_USER, payload });
+export const unauthUser = () => ({ type: UNAUTH_USER });
+export const sentVerification = payload => ({
+  type: SENT_VERIFICATION,
+  payload,
+});
+export const showAuthError = payload => ({ type: AUTH_ERROR, payload });
+export const showLoginLoading = () => ({ type: LOGIN_LOADING });
+export const showSignupLoading = () => ({ type: SIGNUP_LOADING });
+export const authRenew = () => ({ type: AUTH_RENEW });
+
+export const signupUser = payload => async dispatch => {
+  dispatch(showSignupLoading());
+  try {
+    const { data: { email } } = await axios.post('/api/auth/signup', payload);
+    dispatch(sentVerification(email));
+  } catch ({ response }) {
+    dispatch(showAuthError(response.data.error));
+  }
+};
+
+export const loginUser = payload => async dispatch => {
+  dispatch(showLoginLoading());
+  try {
+    const { data: { token } } = await axios.post('/api/auth/login', payload);
+    cookie.set('token', token, { expires: 7 });
+    dispatch(authRenew());
+    dispatch(authUser(decodeJwt(token).sub));
+    dispatch(setDomain(decodeJwt(token).domain));
+    dispatch(showPageLoading());
+    Router.push('/');
+  } catch ({ response }) {
+    dispatch(showAuthError(response.data.error));
+  }
+};
+
+export const logoutUser = () => dispatch => {
+  dispatch(showPageLoading());
+  cookie.remove('token');
+  dispatch(unauthUser());
+  Router.push('/login');
+};
+
+export const renewAuthUser = () => async (dispatch, getState) => {
+  if (getState().auth.renew) {
+    return;
+  }
+
+  const options = {
+    method: 'POST',
+    headers: { Authorization: cookie.get('token') },
+    url: '/api/auth/renew',
+  };
+
+  try {
+    const { data: { token } } = await axios(options);
+    cookie.set('token', token, { expires: 7 });
+    dispatch(authRenew());
+    dispatch(authUser(decodeJwt(token).sub));
+    dispatch(setDomain(decodeJwt(token).domain));
+  } catch (error) {
+    cookie.remove('token');
+    dispatch(unauthUser());
+  }
+};

+ 60 - 141
client/actions/index.js

@@ -1,142 +1,61 @@
-import Router from 'next/router';
-import axios from 'axios';
-import cookie from 'js-cookie';
-import decodeJwt from 'jwt-decode';
-import * as types from './actionTypes';
-
-/* Homepage input actions */
-const addUrl = payload => ({ type: types.ADD_URL, payload });
-const listUrls = payload => ({ type: types.LIST_URLS, payload });
-const updateUrlList = payload => ({ type: types.UPDATE_URL_LIST, payload });
-const deleteUrl = payload => ({ type: types.DELETE_URL, payload });
-export const showShortenerLoading = () => ({ type: types.SHORTENER_LOADING });
-const showTableLoading = () => ({ type: types.TABLE_LOADING });
-export const setShortenerFormError = payload => ({ type: types.SHORTENER_ERROR, payload });
-
-export const createShortUrl = params => dispatch =>
-  axios
-    .post('/api/url/submit', params, { headers: { Authorization: cookie.get('token') } })
-    .then(({ data }) => dispatch(addUrl(data)))
-    .catch(({ response }) => dispatch(setShortenerFormError(response.data.error)));
-
-export const getUrlsList = params => (dispatch, getState) => {
-  if (params) dispatch(updateUrlList(params));
-  dispatch(showTableLoading());
-  const { url } = getState();
-  const query = Object.keys(url).reduce(
-    (string, item) => (typeof url[item] !== 'object' ? `${string + item}=${url[item]}&` : string),
-    '?'
-  );
-  return axios
-    .get(`/api/url/geturls${query}`, { headers: { Authorization: cookie.get('token') } })
-    .then(({ data }) => dispatch(listUrls(data)));
-};
-
-export const deleteShortUrl = params => dispatch => {
-  dispatch(showTableLoading());
-  return axios
-    .post('/api/url/deleteurl', params, { headers: { Authorization: cookie.get('token') } })
-    .then(() => dispatch(deleteUrl(params.id)))
-    .catch(({ response }) => dispatch(setShortenerFormError(response.data.error)));
-};
-/* Page loading actions */
-export const showPageLoading = () => ({ type: types.SHOW_PAGE_LOADING });
-export const hidePageLoading = () => ({ type: types.HIDE_PAGE_LOADING });
-
-/* Settings actions */
-export const setDomain = payload => ({ type: types.SET_DOMAIN, payload });
-export const setApiKey = payload => ({ type: types.SET_APIKEY, payload });
-const deleteDomain = () => ({ type: types.DELETE_DOMAIN });
-const setDomainError = payload => ({ type: types.DOMAIN_ERROR, payload });
-const showDomainLoading = () => ({ type: types.DOMAIN_LOADING });
-const showApiLoading = () => ({ type: types.API_LOADING });
-export const showDomainInput = () => ({ type: types.SHOW_DOMAIN_INPUT });
-
-export const getUserSettings = () => dispatch =>
-  axios
-    .get('/api/auth/usersettings', { headers: { Authorization: cookie.get('token') } })
-    .then(({ data }) => {
-      dispatch(setDomain(data.customDomain));
-      dispatch(setApiKey(data.apikey));
-    });
-
-export const setCustomDomain = params => dispatch => {
-  dispatch(showDomainLoading());
-  return axios
-    .post('/api/url/customdomain', params, { headers: { Authorization: cookie.get('token') } })
-    .then(({ data }) => dispatch(setDomain(data.customDomain)))
-    .catch(({ response }) => dispatch(setDomainError(response.data.error)));
-};
-
-export const deleteCustomDomain = () => dispatch =>
-  axios
-    .delete('/api/url/customdomain', { headers: { Authorization: cookie.get('token') } })
-    .then(() => dispatch(deleteDomain()))
-    .catch(({ response }) => dispatch(setDomainError(response.data.error)));
-
-export const generateApiKey = () => dispatch => {
-  dispatch(showApiLoading());
-  return axios
-    .post('/api/auth/generateapikey', null, { headers: { Authorization: cookie.get('token') } })
-    .then(({ data }) => dispatch(setApiKey(data.apikey)));
-};
-
-/* Login & signup actions */
-export const authUser = payload => ({ type: types.AUTH_USER, payload });
-export const unauthUser = () => ({ type: types.UNAUTH_USER });
-export const sentVerification = payload => ({ type: types.SENT_VERIFICATION, payload });
-export const showAuthError = payload => ({ type: types.AUTH_ERROR, payload });
-export const showLoginLoading = () => ({ type: types.LOGIN_LOADING });
-export const showSignupLoading = () => ({ type: types.SIGNUP_LOADING });
-export const authRenew = () => ({ type: types.AUTH_RENEW });
-
-export const signupUser = body => dispatch => {
-  dispatch(showSignupLoading());
-  return axios
-    .post('/api/auth/signup', body)
-    .then(res => {
-      const { email } = res.data;
-      dispatch(sentVerification(email));
-    })
-    .catch(err => dispatch(showAuthError(err.response.data.error)));
-};
-
-export const loginUser = body => dispatch => {
-  dispatch(showLoginLoading());
-  return axios
-    .post('/api/auth/login', body)
-    .then(res => {
-      const { token } = res.data;
-      cookie.set('token', token, { expires: 7 });
-      dispatch(authRenew());
-      dispatch(authUser(decodeJwt(token).sub));
-      dispatch(setDomain(decodeJwt(token).domain));
-      dispatch(showPageLoading());
-      Router.push('/');
-    })
-    .catch(err => dispatch(showAuthError(err.response.data.error)));
-};
-
-export const logoutUser = () => dispatch => {
-  dispatch(showPageLoading());
-  cookie.remove('token');
-  dispatch(unauthUser());
-  return Router.push('/login');
-};
-
-export const renewAuthUser = () => (dispatch, getState) => {
-  if (getState().auth.renew) return null;
-  return axios
-    .post('/api/auth/renew', null, { headers: { Authorization: cookie.get('token') } })
-    .then(res => {
-      const { token } = res.data;
-      cookie.set('token', token, { expires: 7 });
-      dispatch(authRenew());
-      dispatch(authUser(decodeJwt(token).sub));
-      dispatch(setDomain(decodeJwt(token).domain));
-    })
-    .catch(() => {
-      cookie.remove('token');
-      dispatch(unauthUser());
-    });
+import {
+  createShortUrl,
+  getUrlsList,
+  deleteShortUrl,
+  showShortenerLoading,
+  setShortenerFormError,
+} from './url';
+
+import {
+  setDomain,
+  setApiKey,
+  showDomainInput,
+  getUserSettings,
+  setCustomDomain,
+  deleteCustomDomain,
+  generateApiKey,
+} from './settings';
+
+import {
+  showPageLoading,
+  hidePageLoading,
+  authUser,
+  unauthUser,
+  sentVerification,
+  showAuthError,
+  showLoginLoading,
+  showSignupLoading,
+  authRenew,
+  signupUser,
+  loginUser,
+  logoutUser,
+  renewAuthUser,
+} from './auth';
+
+export {
+  createShortUrl,
+  getUrlsList,
+  deleteShortUrl,
+  showShortenerLoading,
+  setShortenerFormError,
+  setDomain,
+  setApiKey,
+  showDomainInput,
+  getUserSettings,
+  setCustomDomain,
+  deleteCustomDomain,
+  generateApiKey,
+  showPageLoading,
+  hidePageLoading,
+  authUser,
+  unauthUser,
+  sentVerification,
+  showAuthError,
+  showLoginLoading,
+  showSignupLoading,
+  authRenew,
+  signupUser,
+  loginUser,
+  logoutUser,
+  renewAuthUser,
 };

+ 67 - 0
client/actions/settings.js

@@ -0,0 +1,67 @@
+import axios from 'axios';
+import cookie from 'js-cookie';
+import {
+  DELETE_DOMAIN,
+  DOMAIN_ERROR,
+  DOMAIN_LOADING,
+  API_LOADING,
+  SET_DOMAIN,
+  SET_APIKEY,
+  SHOW_DOMAIN_INPUT,
+} from './actionTypes';
+
+const deleteDomain = () => ({ type: DELETE_DOMAIN });
+const setDomainError = payload => ({ type: DOMAIN_ERROR, payload });
+const showDomainLoading = () => ({ type: DOMAIN_LOADING });
+const showApiLoading = () => ({ type: API_LOADING });
+
+export const setDomain = payload => ({ type: SET_DOMAIN, payload });
+export const setApiKey = payload => ({ type: SET_APIKEY, payload });
+export const showDomainInput = () => ({ type: SHOW_DOMAIN_INPUT });
+
+export const getUserSettings = () => async dispatch => {
+  try {
+    const { data } = await axios.get('/api/auth/usersettings', {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(setDomain(data.customDomain));
+    dispatch(setApiKey(data.apikey));
+  } catch (error) {
+    //
+  }
+};
+
+export const setCustomDomain = params => async dispatch => {
+  dispatch(showDomainLoading());
+  try {
+    const { data } = await axios.post('/api/url/customdomain', params, {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(setDomain(data.customDomain));
+  } catch ({ response }) {
+    dispatch(setDomainError(response.data.error));
+  }
+};
+
+export const deleteCustomDomain = () => async dispatch => {
+  try {
+    await axios.delete('/api/url/customdomain', {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(deleteDomain());
+  } catch ({ response }) {
+    dispatch(setDomainError(response.data.error));
+  }
+};
+
+export const generateApiKey = () => async dispatch => {
+  dispatch(showApiLoading());
+  try {
+    const { data } = await axios.post('/api/auth/generateapikey', null, {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(setApiKey(data.apikey));
+  } catch (error) {
+    //
+  }
+};

+ 71 - 0
client/actions/url.js

@@ -0,0 +1,71 @@
+import axios from 'axios';
+import cookie from 'js-cookie';
+import {
+  ADD_URL,
+  LIST_URLS,
+  UPDATE_URL_LIST,
+  DELETE_URL,
+  SHORTENER_LOADING,
+  TABLE_LOADING,
+  SHORTENER_ERROR,
+} from './actionTypes';
+
+const addUrl = payload => ({ type: ADD_URL, payload });
+const listUrls = payload => ({ type: LIST_URLS, payload });
+const updateUrlList = payload => ({ type: UPDATE_URL_LIST, payload });
+const deleteUrl = payload => ({ type: DELETE_URL, payload });
+const showTableLoading = () => ({ type: TABLE_LOADING });
+
+export const setShortenerFormError = payload => ({
+  type: SHORTENER_ERROR,
+  payload,
+});
+
+export const showShortenerLoading = () => ({ type: SHORTENER_LOADING });
+
+export const createShortUrl = params => async dispatch => {
+  try {
+    const { data } = await axios.post('/api/url/submit', params, {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(addUrl(data));
+  } catch ({ response }) {
+    dispatch(setShortenerFormError(response.data.error));
+  }
+};
+
+export const getUrlsList = params => async (dispatch, getState) => {
+  if (params) {
+    dispatch(updateUrlList(params));
+  }
+
+  dispatch(showTableLoading());
+
+  const { url } = getState();
+  const { list, ...queryParams } = url;
+  const query = Object.keys(queryParams).reduce(
+    (string, item) => `${string + item}=${queryParams[item]}&`,
+    '?'
+  );
+
+  try {
+    const { data } = await axios.get(`/api/url/geturls${query}`, {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(listUrls(data));
+  } catch (error) {
+    //
+  }
+};
+
+export const deleteShortUrl = params => async dispatch => {
+  dispatch(showTableLoading());
+  try {
+    await axios.post('/api/url/deleteurl', params, {
+      headers: { Authorization: cookie.get('token') },
+    });
+    dispatch(deleteUrl(params.id));
+  } catch ({ response }) {
+    dispatch(setShortenerFormError(response.data.error));
+  }
+};