diff --git a/src/containers/EditListingPage/EditListingPage.js b/src/containers/EditListingPage/EditListingPage.js index eafc7fc0..4d36dfe8 100644 --- a/src/containers/EditListingPage/EditListingPage.js +++ b/src/containers/EditListingPage/EditListingPage.js @@ -5,7 +5,9 @@ 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 { createSlug } from '../../util/urlHelpers'; +import * as propTypes from '../../util/propTypes'; import { requestCreateListing, requestShowListing, @@ -43,7 +45,7 @@ export class EditListingPageComponent extends Component { render() { const { - data, + marketplaceData, intl, onCreateListing, onImageUpload, @@ -51,10 +53,11 @@ export class EditListingPageComponent extends Component { page, params, type, + currentUser, } = this.props; const isNew = type === 'new'; const id = page.submittedListingId || (params && new types.UUID(params.id)); - const listingsById = getListingsById(data, [id]); + const listingsById = getListingsById(marketplaceData, [id]); const currentListing = listingsById.length > 0 ? listingsById[0] : null; const shouldRedirect = page.submittedListingId && currentListing; @@ -84,6 +87,9 @@ export class EditListingPageComponent extends Component { ? intl.formatMessage({ id: 'EditListingPage.titleCreateListing' }) : intl.formatMessage({ id: 'EditListingPage.titleEditListing' }); + // eslint-disable-next-line no-console + console.log('current user:', currentUser); + return ( { onLoadListing(id); }; -EditListingPageComponent.defaultProps = { listing: null, params: null, type: 'edit' }; +EditListingPageComponent.defaultProps = { + listing: null, + params: null, + type: 'edit', + currentUser: null, +}; const { func, object, shape, string } = PropTypes; EditListingPageComponent.propTypes = { - data: object.isRequired, + marketplaceData: object.isRequired, intl: intlShape.isRequired, onCreateListing: func.isRequired, onLoadListing: func.isRequired, @@ -129,11 +140,15 @@ EditListingPageComponent.propTypes = { slug: string, }), type: string, + currentUser: propTypes.user, }; const mapStateToProps = state => { - const { data = {}, EditListingPage } = state; - return { page: EditListingPage, data }; + return { + page: state.EditListingPage, + marketplaceData: state.data || {}, + currentUser: state.Auth.currentUser, + }; }; const mapDispatchToProps = dispatch => { @@ -145,4 +160,12 @@ const mapDispatchToProps = dispatch => { }; }; -export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(EditListingPageComponent)); +const EditListingPage = connect(mapStateToProps, mapDispatchToProps)( + injectIntl(EditListingPageComponent) +); + +EditListingPage.loadData = () => { + return fetchCurrentUser(); +}; + +export default EditListingPage; diff --git a/src/containers/EditListingPage/EditListingPage.test.js b/src/containers/EditListingPage/EditListingPage.test.js index e896200c..cb083f4a 100644 --- a/src/containers/EditListingPage/EditListingPage.test.js +++ b/src/containers/EditListingPage/EditListingPage.test.js @@ -7,7 +7,7 @@ describe('EditListingPageComponent', () => { it('matches snapshot', () => { const tree = renderShallow( v} diff --git a/src/ducks/Auth.duck.js b/src/ducks/Auth.duck.js index 85c841be..5b2dd9f8 100644 --- a/src/ducks/Auth.duck.js +++ b/src/ducks/Auth.duck.js @@ -8,6 +8,10 @@ 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'; @@ -23,8 +27,10 @@ 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, @@ -33,6 +39,13 @@ 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: @@ -60,6 +73,19 @@ 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 }); @@ -136,3 +162,20 @@ export function* watchAuth(sdk) { } } } + +// ================ Thunks ================ // + +export const fetchCurrentUser = () => + (dispatch, getState, sdk) => { + dispatch(usersMeRequest()); + sdk.users + .me() + .then(response => { + dispatch(usersMeSuccess(response.data.data)); + return response; + }) + .catch(e => { + dispatch(usersMeError(e)); + throw e; + }); + }; diff --git a/src/routesConfiguration.js b/src/routesConfiguration.js index e247be7e..26353a6a 100644 --- a/src/routesConfiguration.js +++ b/src/routesConfiguration.js @@ -74,6 +74,7 @@ const routesConfiguration = [ exact: true, name: 'NewListingPage', component: props => , + loadData: (params, search) => EditListingPage.loadData(params, search), }, { auth: true, diff --git a/src/util/propTypes.js b/src/util/propTypes.js index fd1bae9a..0c824b40 100644 --- a/src/util/propTypes.js +++ b/src/util/propTypes.js @@ -66,12 +66,13 @@ export const user = shape({ id: uuid.isRequired, type: value('user').isRequired, attributes: shape({ - email: string.isRequired, + email: string, profile: shape({ firstName: string.isRequired, lastName: string.isRequired, slug: string.isRequired, }).isRequired, + stripeConnected: bool, }), });