From d71bf1b0aaf6134e407bd25790ac9273b011d792 Mon Sep 17 00:00:00 2001 From: Kimmo Puputti Date: Wed, 13 Sep 2017 12:57:49 +0300 Subject: [PATCH] Move verification email sending to user duck --- .../AuthenticationPage.duck.js | 73 ------------------- .../AuthenticationPage/AuthenticationPage.js | 6 +- src/containers/reducers.js | 2 - src/ducks/user.duck.js | 56 ++++++++++++++ 4 files changed, 58 insertions(+), 79 deletions(-) delete mode 100644 src/containers/AuthenticationPage/AuthenticationPage.duck.js diff --git a/src/containers/AuthenticationPage/AuthenticationPage.duck.js b/src/containers/AuthenticationPage/AuthenticationPage.duck.js deleted file mode 100644 index 90ce8dbc..00000000 --- a/src/containers/AuthenticationPage/AuthenticationPage.duck.js +++ /dev/null @@ -1,73 +0,0 @@ -// ================ Action types ================ // - -export const SEND_VERIFICATION_EMAIL_REQUEST = 'app/AuthenticationPage/SEND_VERIFICATION_EMAIL_REQUEST'; -export const SEND_VERIFICATION_EMAIL_SUCCESS = 'app/AuthenticationPage/SEND_VERIFICATION_EMAIL_SUCCESS'; -export const SEND_VERIFICATION_EMAIL_ERROR = 'app/AuthenticationPage/SEND_VERIFICATION_EMAIL_ERROR'; - -// ================ Reducer ================ // - -const initialState = { - sendVerificationEmailInProgress: false, - sendVerificationEmailError: null, -}; - -export default function authenticationPageReducer(state = initialState, action = {}) { - const { type, payload } = action; - switch (type) { - case SEND_VERIFICATION_EMAIL_REQUEST: - return { - ...state, - sendVerificationEmailInProgress: true, - sendVerificationEmailError: null, - }; - case SEND_VERIFICATION_EMAIL_SUCCESS: - return { - ...state, - sendVerificationEmailInProgress: false, - }; - case SEND_VERIFICATION_EMAIL_ERROR: - return { - ...state, - sendVerificationEmailInProgress: false, - sendVerificationEmailError: payload, - }; - default: - return state; - } -} - -// ================ Selectors ================ // - -export const verificationSendingInProgress = state => { - return state.AuthenticationPage.sendVerificationEmailInProgress; -}; - -// ================ Action creators ================ // - -export const sendVerificationEmailRequest = () => ({ - type: SEND_VERIFICATION_EMAIL_REQUEST, -}); - -export const sendVerificationEmailSuccess = () => ({ - type: SEND_VERIFICATION_EMAIL_SUCCESS, -}); - -export const sendVerificationEmailError = e => ({ - type: SEND_VERIFICATION_EMAIL_ERROR, - error: true, - payload: e, -}); - -// ================ Thunks ================ // - -export const sendVerificationEmail = () => - (dispatch, getState, sdk) => { - if (verificationSendingInProgress(getState())) { - return Promise.reject(new Error('Verification email sending already in progress')); - } - dispatch(sendVerificationEmailRequest()); - return sdk.currentUser - .sendVerificationEmail() - .then(() => dispatch(sendVerificationEmailSuccess())) - .catch(e => dispatch(sendVerificationEmailError(e))); - }; diff --git a/src/containers/AuthenticationPage/AuthenticationPage.js b/src/containers/AuthenticationPage/AuthenticationPage.js index 6f18e4aa..58663fca 100644 --- a/src/containers/AuthenticationPage/AuthenticationPage.js +++ b/src/containers/AuthenticationPage/AuthenticationPage.js @@ -18,7 +18,7 @@ import { import { LoginForm, SignupForm } from '../../containers'; import { login, logout, authenticationInProgress, signup } from '../../ducks/Auth.duck'; import { manageDisableScrolling, isScrollingDisabled } from '../../ducks/UI.duck'; -import { sendVerificationEmail } from './AuthenticationPage.duck'; +import { sendVerificationEmail } from '../../ducks/user.duck'; import css from './AuthenticationPage.css'; @@ -279,11 +279,9 @@ const mapStateToProps = state => { currentUser, currentUserHasListings, currentUserNotificationCount: notificationCount, - } = state.user; - const { sendVerificationEmailInProgress, sendVerificationEmailError, - } = state.AuthenticationPage; + } = state.user; return { authInfoError, authInProgress: authenticationInProgress(state), diff --git a/src/containers/reducers.js b/src/containers/reducers.js index 770a96d1..40fe13fa 100644 --- a/src/containers/reducers.js +++ b/src/containers/reducers.js @@ -3,7 +3,6 @@ * We are following Ducks module proposition: * https://github.com/erikras/ducks-modular-redux */ -import AuthenticationPage from './AuthenticationPage/AuthenticationPage.duck'; import CheckoutPage from './CheckoutPage/CheckoutPage.duck'; import EditListingPage from './EditListingPage/EditListingPage.duck'; import InboxPage from './InboxPage/InboxPage.duck'; @@ -16,7 +15,6 @@ import SalePage from './SalePage/SalePage.duck'; import SearchPage from './SearchPage/SearchPage.duck'; export { - AuthenticationPage, CheckoutPage, EditListingPage, InboxPage, diff --git a/src/ducks/user.duck.js b/src/ducks/user.duck.js index dc7eb3ca..a5a9b4c2 100644 --- a/src/ducks/user.duck.js +++ b/src/ducks/user.duck.js @@ -21,6 +21,10 @@ export const FETCH_CURRENT_USER_NOTIFICATIONS_REQUEST = 'app/user/FETCH_CURRENT_ export const FETCH_CURRENT_USER_NOTIFICATIONS_SUCCESS = 'app/user/FETCH_CURRENT_USER_NOTIFICATIONS_SUCCESS'; export const FETCH_CURRENT_USER_NOTIFICATIONS_ERROR = 'app/user/FETCH_CURRENT_USER_NOTIFICATIONS_ERROR'; +export const SEND_VERIFICATION_EMAIL_REQUEST = 'app/user/SEND_VERIFICATION_EMAIL_REQUEST'; +export const SEND_VERIFICATION_EMAIL_SUCCESS = 'app/user/SEND_VERIFICATION_EMAIL_SUCCESS'; +export const SEND_VERIFICATION_EMAIL_ERROR = 'app/user/SEND_VERIFICATION_EMAIL_ERROR'; + // ================ Reducer ================ // const initialState = { @@ -32,6 +36,8 @@ const initialState = { currentUserHasListingsError: null, currentUserNotificationCount: 0, currentUserNotificationCountError: null, + sendVerificationEmailInProgress: false, + sendVerificationEmailError: null, }; export default function reducer(state = initialState, action = {}) { @@ -82,11 +88,35 @@ export default function reducer(state = initialState, action = {}) { console.error(payload); return { ...state, createStripeAccountError: payload, createStripeAccountInProgress: false }; + case SEND_VERIFICATION_EMAIL_REQUEST: + return { + ...state, + sendVerificationEmailInProgress: true, + sendVerificationEmailError: null, + }; + case SEND_VERIFICATION_EMAIL_SUCCESS: + return { + ...state, + sendVerificationEmailInProgress: false, + }; + case SEND_VERIFICATION_EMAIL_ERROR: + return { + ...state, + sendVerificationEmailInProgress: false, + sendVerificationEmailError: payload, + }; + default: return state; } } +// ================ Selectors ================ // + +export const verificationSendingInProgress = state => { + return state.user.sendVerificationEmailInProgress; +}; + // ================ Action creators ================ // export const currentUserShowRequest = () => ({ type: CURRENT_USER_SHOW_REQUEST }); @@ -147,6 +177,20 @@ const fetchCurrentUserNotificationsError = e => ({ payload: e, }); +export const sendVerificationEmailRequest = () => ({ + type: SEND_VERIFICATION_EMAIL_REQUEST, +}); + +export const sendVerificationEmailSuccess = () => ({ + type: SEND_VERIFICATION_EMAIL_SUCCESS, +}); + +export const sendVerificationEmailError = e => ({ + type: SEND_VERIFICATION_EMAIL_ERROR, + error: true, + payload: e, +}); + // ================ Thunks ================ // export const fetchCurrentUserHasListings = () => @@ -253,3 +297,15 @@ export const createStripeAccount = payoutDetails => }) .then(() => dispatch(fetchCurrentUser())); }; + +export const sendVerificationEmail = () => + (dispatch, getState, sdk) => { + if (verificationSendingInProgress(getState())) { + return Promise.reject(new Error('Verification email sending already in progress')); + } + dispatch(sendVerificationEmailRequest()); + return sdk.currentUser + .sendVerificationEmail() + .then(() => dispatch(sendVerificationEmailSuccess())) + .catch(e => dispatch(sendVerificationEmailError(e))); + };