Move users and stripe thunks to a new duck

- Create user.duck
 - Move fetchCurrentUser form Auth to user
 - Move createStripeAccount to user
 - Call fetchCurrentUser to update user data after creating Stripe account
This commit is contained in:
Kimmo Puputti 2017-04-05 16:36:56 +03:00
parent c7d67a64d4
commit 279944d002
5 changed files with 99 additions and 47 deletions

View file

@ -1,4 +1,5 @@
import { showListingsSuccess as globalShowListingsSuccess } from '../../ducks/sdk.duck';
import { createStripeAccount } from '../../ducks/user.duck';
const requestAction = actionType => params => ({ type: actionType, payload: { params } });
@ -147,7 +148,7 @@ export function requestCreateListing(data) {
const shouldCreateStripeAccount = bankAccountToken && country;
const accountCreated = shouldCreateStripeAccount
? sdk.users.createStripeAccount({ bankAccountToken, country })
? dispatch(createStripeAccount(bankAccountToken, country))
: Promise.resolve('already created');
return accountCreated

View file

@ -5,7 +5,7 @@ import { types } from '../../util/sdkLoader';
import { NamedRedirect, PageLayout } from '../../components';
import { EditListingForm } from '../../containers';
import { getListingsById } from '../../ducks/sdk.duck';
import { fetchCurrentUser } from '../../ducks/Auth.duck';
import { fetchCurrentUser } from '../../ducks/user.duck';
import { createSlug } from '../../util/urlHelpers';
import * as propTypes from '../../util/propTypes';
import {
@ -149,7 +149,7 @@ const mapStateToProps = state => {
return {
page: state.EditListingPage,
marketplaceData: state.data || {},
currentUser: state.Auth.currentUser,
currentUser: state.user.currentUser,
};
};

View file

@ -8,10 +8,6 @@ const authenticated = authInfo => authInfo.grantType === 'refresh_token';
// ================ Action types ================ //
export const USERS_ME_REQUEST = 'app/Auth/USERS_ME_REQUEST';
export const USERS_ME_SUCCESS = 'app/Auth/USERS_ME_SUCCESS';
export const USERS_ME_ERROR = 'app/Auth/USERS_ME_ERROR';
export const AUTH_INFO_REQUEST = 'app/Auth/AUTH_INFO_REQUEST';
export const AUTH_INFO_SUCCESS = 'app/Auth/AUTH_INFO_SUCCESS';
export const AUTH_INFO_ERROR = 'app/Auth/AUTH_INFO_ERROR';
@ -27,10 +23,8 @@ export const LOGOUT_ERROR = 'app/Auth/LOGOUT_ERROR';
// ================ Reducer ================ //
const initialState = {
currentUser: null,
authInfoLoaded: false,
isAuthenticated: false,
currentUserError: null,
authInfoError: null,
loginError: null,
logoutError: null,
@ -39,13 +33,6 @@ const initialState = {
export default function reducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case USERS_ME_REQUEST:
return { ...state, currentUserError: null };
case USERS_ME_SUCCESS:
return { ...state, currentUser: payload };
case USERS_ME_ERROR:
return { ...state, currentUserError: payload };
case AUTH_INFO_REQUEST:
return { ...state, authInfoError: null };
case AUTH_INFO_SUCCESS:
@ -73,19 +60,6 @@ export default function reducer(state = initialState, action = {}) {
// ================ Action creators ================ //
export const usersMeRequest = () => ({ type: USERS_ME_REQUEST });
export const usersMeSuccess = user => ({
type: USERS_ME_SUCCESS,
payload: user,
});
export const usersMeError = e => ({
type: USERS_ME_ERROR,
payload: e,
error: true,
});
export const authInfo = () => ({ type: AUTH_INFO_REQUEST });
export const authInfoSuccess = info => ({ type: AUTH_INFO_SUCCESS, payload: info });
export const authInfoError = error => ({ type: AUTH_INFO_ERROR, payload: error, error: true });
@ -162,20 +136,3 @@ export function* watchAuth(sdk) {
}
}
}
// ================ Thunks ================ //
export const fetchCurrentUser = () =>
(dispatch, getState, sdk) => {
dispatch(usersMeRequest());
return sdk.users
.me()
.then(response => {
dispatch(usersMeSuccess(response.data.data));
return response;
})
.catch(e => {
dispatch(usersMeError(e));
throw e;
});
};

View file

@ -9,5 +9,6 @@ import Auth from './Auth.duck';
import FlashNotification from './FlashNotification.duck';
import LocationFilter from './LocationFilter.duck';
import sdkReducer from './sdk.duck';
import user from './user.duck';
export { form, Auth, FlashNotification, LocationFilter, sdkReducer as data };
export { form, Auth, FlashNotification, LocationFilter, sdkReducer as data, user };

93
src/ducks/user.duck.js Normal file
View file

@ -0,0 +1,93 @@
// ================ Action types ================ //
export const USERS_ME_REQUEST = 'app/user/USERS_ME_REQUEST';
export const USERS_ME_SUCCESS = 'app/user/USERS_ME_SUCCESS';
export const USERS_ME_ERROR = 'app/user/USERS_ME_ERROR';
export const STRIPE_ACCOUNT_CREATE_REQUEST = 'app/user/STRIPE_ACCOUNT_CREATE_REQUEST';
export const STRIPE_ACCOUNT_CREATE_SUCCESS = 'app/user/STRIPE_ACCOUNT_CREATE_SUCCESS';
export const STRIPE_ACCOUNT_CREATE_ERROR = 'app/user/STRIPE_ACCOUNT_CREATE_ERROR';
// ================ Reducer ================ //
const initialState = {
currentUser: null,
usersMeError: null,
};
export default function reducer(state = initialState, action = {}) {
const { type, payload } = action;
switch (type) {
case USERS_ME_REQUEST:
return { ...state, usersMeError: null };
case USERS_ME_SUCCESS:
return { ...state, currentUser: payload };
case USERS_ME_ERROR:
// eslint-disable-next-line no-console
console.error(payload);
return { ...state, usersMeError: payload };
default:
return state;
}
}
// ================ Action creators ================ //
export const usersMeRequest = () => ({ type: USERS_ME_REQUEST });
export const usersMeSuccess = user => ({
type: USERS_ME_SUCCESS,
payload: user,
});
export const usersMeError = e => ({
type: USERS_ME_ERROR,
payload: e,
error: true,
});
export const stripeAccountCreateRequest = () => ({ type: STRIPE_ACCOUNT_CREATE_REQUEST });
export const stripeAccountCreateSuccess = response => ({
type: STRIPE_ACCOUNT_CREATE_SUCCESS,
payload: response,
});
export const stripeAccountCreateError = e => ({
type: STRIPE_ACCOUNT_CREATE_ERROR,
payload: e,
error: true,
});
// ================ Thunks ================ //
export const fetchCurrentUser = () =>
(dispatch, getState, sdk) => {
dispatch(usersMeRequest());
return sdk.users
.me()
.then(response => {
dispatch(usersMeSuccess(response.data.data));
return response;
})
.catch(e => {
dispatch(usersMeError(e));
throw e;
});
};
export const createStripeAccount = (bankAccountToken, country) =>
(dispatch, getState, sdk) => {
dispatch(stripeAccountCreateRequest());
return sdk.users.createStripeAccount({ bankAccountToken, country })
.then(response => {
dispatch(stripeAccountCreateSuccess(response));
return response;
})
.catch(e => {
dispatch(stripeAccountCreateError(e));
throw e;
})
.then(() => dispatch(fetchCurrentUser()));
};