Move verification email sending to user duck

This commit is contained in:
Kimmo Puputti 2017-09-13 12:57:49 +03:00
parent 1a8a1b5fd0
commit d71bf1b0aa
4 changed files with 58 additions and 79 deletions

View file

@ -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)));
};

View file

@ -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),

View file

@ -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,

View file

@ -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)));
};