mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-31 02:26:50 +10:00
Fetch current user info on EditListingPage
This commit is contained in:
parent
b73b48abb0
commit
840b53c133
5 changed files with 77 additions and 9 deletions
|
|
@ -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 (
|
||||
<PageLayout title={title}>
|
||||
<EditListingForm
|
||||
|
|
@ -112,12 +118,17 @@ EditListingPageComponent.loadData = (id, onLoadListing) => {
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ describe('EditListingPageComponent', () => {
|
|||
it('matches snapshot', () => {
|
||||
const tree = renderShallow(
|
||||
<EditListingPageComponent
|
||||
data={{ entities: {} }}
|
||||
marketplaceData={{ entities: {} }}
|
||||
images={[]}
|
||||
intl={fakeIntl}
|
||||
onCreateListing={v => v}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -74,6 +74,7 @@ const routesConfiguration = [
|
|||
exact: true,
|
||||
name: 'NewListingPage',
|
||||
component: props => <EditListingPage {...props} type={'new'} />,
|
||||
loadData: (params, search) => EditListingPage.loadData(params, search),
|
||||
},
|
||||
{
|
||||
auth: true,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}),
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue