/
EditListingWizard.js
333 lines (302 loc) · 10.1 KB
/
EditListingWizard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import React, { Component } from 'react';
import { array, bool, func, number, object, oneOf, shape, string } from 'prop-types';
import { compose } from 'redux';
import { FormattedMessage, injectIntl, intlShape } from 'react-intl';
import classNames from 'classnames';
import { withViewport } from '../../util/contextHelpers';
import {
LISTING_PAGE_PARAM_TYPE_DRAFT,
LISTING_PAGE_PARAM_TYPE_NEW,
LISTING_PAGE_PARAM_TYPES,
} from '../../util/urlHelpers';
import { ensureListing } from '../../util/data';
import { PayoutDetailsForm } from '../../forms';
import { Modal, NamedRedirect, Tabs } from '../../components';
import EditListingWizardTab, {
DESCRIPTION,
FEATURES,
POLICY,
LOCATION,
PRICING,
PHOTOS,
} from './EditListingWizardTab';
import css from './EditListingWizard.css';
// TODO: PHOTOS panel needs to be the last one since it currently contains PayoutDetailsForm modal
// All the other panels can be reordered.
export const TABS = [DESCRIPTION, FEATURES, POLICY, LOCATION, PRICING, PHOTOS];
// Tabs are horizontal in small screens
const MAX_HORIZONTAL_NAV_SCREEN_WIDTH = 1023;
const tabLabel = (intl, tab) => {
let key = null;
if (tab === DESCRIPTION) {
key = 'EditListingWizard.tabLabelDescription';
} else if (tab === FEATURES) {
key = 'EditListingWizard.tabLabelFeatures';
} else if (tab === POLICY) {
key = 'EditListingWizard.tabLabelPolicy';
} else if (tab === LOCATION) {
key = 'EditListingWizard.tabLabelLocation';
} else if (tab === PRICING) {
key = 'EditListingWizard.tabLabelPricing';
} else if (tab === PHOTOS) {
key = 'EditListingWizard.tabLabelPhotos';
}
return intl.formatMessage({ id: key });
};
/**
* Check if a wizard tab is completed.
*
* @param tab wizard's tab
* @param listing is contains some specific data if tab is completed
*
* @return true if tab / step is completed.
*/
const tabCompleted = (tab, listing) => {
const { description, geolocation, price, title, publicData } = listing.attributes;
const images = listing.images;
switch (tab) {
case DESCRIPTION:
return !!(description && title);
case FEATURES:
return !!(publicData && publicData.amenities);
case POLICY:
return !!(publicData && typeof publicData.rules !== 'undefined');
case LOCATION:
return !!(geolocation && publicData && publicData.location && publicData.location.address);
case PRICING:
return !!price;
case PHOTOS:
return images && images.length > 0;
default:
return false;
}
};
/**
* Check which wizard tabs are active and which are not yet available. Tab is active if previous
* tab is completed. In edit mode all tabs are active.
*
* @param isNew flag if a new listing is being created or an old one being edited
* @param listing data to be checked
*
* @return object containing activity / editability of different tabs of this wizard
*/
const tabsActive = (isNew, listing) => {
return TABS.reduce((acc, tab) => {
const previousTabIndex = TABS.findIndex(t => t === tab) - 1;
const isActive =
previousTabIndex >= 0 ? !isNew || tabCompleted(TABS[previousTabIndex], listing) : true;
return { ...acc, [tab]: isActive };
}, {});
};
const scrollToTab = (tabPrefix, tabId) => {
const el = document.querySelector(`#${tabPrefix}_${tabId}`);
if (el) {
el.scrollIntoView({
block: 'start',
behavior: 'smooth',
});
}
};
// Create a new or edit listing through EditListingWizard
class EditListingWizard extends Component {
constructor(props) {
super(props);
// Having this info in state would trigger unnecessary rerendering
this.hasScrolledToTab = false;
this.state = {
draftId: null,
showPayoutDetails: false,
};
this.handleCreateFlowTabScrolling = this.handleCreateFlowTabScrolling.bind(this);
this.handlePublishListing = this.handlePublishListing.bind(this);
this.handlePayoutModalClose = this.handlePayoutModalClose.bind(this);
this.handlePayoutSubmit = this.handlePayoutSubmit.bind(this);
}
handleCreateFlowTabScrolling(shouldScroll) {
this.hasScrolledToTab = shouldScroll;
}
handlePublishListing(id) {
const { onPublishListingDraft, currentUser } = this.props;
const stripeConnected =
currentUser && currentUser.attributes && currentUser.attributes.stripeConnected;
if (stripeConnected) {
onPublishListingDraft(id);
} else {
this.setState({
draftId: id,
showPayoutDetails: true,
});
}
}
handlePayoutModalClose() {
this.setState({ showPayoutDetails: false });
}
handlePayoutSubmit(values) {
const { fname: firstName, lname: lastName, ...rest } = values;
this.props
.onPayoutDetailsSubmit({ firstName, lastName, ...rest })
.then(() => {
this.setState({ showPayoutDetails: false });
this.props.onManageDisableScrolling('EditListingWizard.payoutModal', false);
this.props.onPublishListingDraft(this.state.draftId);
})
.catch(() => {
// do nothing
});
}
render() {
const {
id,
className,
rootClassName,
params,
listing,
viewport,
intl,
errors,
fetchInProgress,
onManageDisableScrolling,
onPayoutDetailsFormChange,
...rest
} = this.props;
const selectedTab = params.tab;
const isNewListingFlow = [LISTING_PAGE_PARAM_TYPE_NEW, LISTING_PAGE_PARAM_TYPE_DRAFT].includes(
params.type
);
const rootClasses = rootClassName || css.root;
const classes = classNames(rootClasses, className);
const currentListing = ensureListing(listing);
const tabsStatus = tabsActive(isNewListingFlow, currentListing);
// If selectedTab is not active, redirect to the beginning of wizard
if (!tabsStatus[selectedTab]) {
const currentTabIndex = TABS.indexOf(selectedTab);
const nearestActiveTab = TABS.slice(0, currentTabIndex)
.reverse()
.find(t => tabsStatus[t]);
return <NamedRedirect name="EditListingPage" params={{ ...params, tab: nearestActiveTab }} />;
}
const { width } = viewport;
const hasViewport = width > 0;
const hasHorizontalTabLayout = hasViewport && width <= MAX_HORIZONTAL_NAV_SCREEN_WIDTH;
const hasVerticalTabLayout = hasViewport && width > MAX_HORIZONTAL_NAV_SCREEN_WIDTH;
const hasFontsLoaded =
hasViewport && document.documentElement.classList.contains('fontsLoaded');
// Check if scrollToTab call is needed (tab is not visible on mobile)
if (hasVerticalTabLayout) {
this.hasScrolledToTab = true;
} else if (hasHorizontalTabLayout && !this.hasScrolledToTab && hasFontsLoaded) {
const tabPrefix = id;
scrollToTab(tabPrefix, selectedTab);
this.hasScrolledToTab = true;
}
const tabLink = tab => {
return { name: 'EditListingPage', params: { ...params, tab } };
};
return (
<div className={classes}>
<Tabs
rootClassName={css.tabsContainer}
navRootClassName={css.nav}
tabRootClassName={css.tab}
>
{TABS.map(tab => {
return (
<EditListingWizardTab
{...rest}
key={tab}
tabId={`${id}_${tab}`}
tabLabel={tabLabel(intl, tab)}
tabLinkProps={tabLink(tab)}
selected={selectedTab === tab}
disabled={isNewListingFlow && !tabsStatus[tab]}
tab={tab}
intl={intl}
params={params}
listing={listing}
marketplaceTabs={TABS}
errors={errors}
handleCreateFlowTabScrolling={this.handleCreateFlowTabScrolling}
handlePublishListing={this.handlePublishListing}
fetchInProgress={fetchInProgress}
/>
);
})}
</Tabs>
<Modal
id="EditListingWizard.payoutModal"
isOpen={this.state.showPayoutDetails}
onClose={this.handlePayoutModalClose}
onManageDisableScrolling={onManageDisableScrolling}
>
<div className={css.modalHeaderWrapper}>
<h1 className={css.modalTitle}>
<FormattedMessage id="EditListingPhotosPanel.payoutModalTitleOneMoreThing" />
<br />
<FormattedMessage id="EditListingPhotosPanel.payoutModalTitlePayoutPreferences" />
</h1>
<p className={css.modalMessage}>
<FormattedMessage id="EditListingPhotosPanel.payoutModalInfo" />
</p>
</div>
<PayoutDetailsForm
className={css.payoutDetails}
inProgress={fetchInProgress}
createStripeAccountError={errors ? errors.createStripeAccountError : null}
onChange={onPayoutDetailsFormChange}
onSubmit={this.handlePayoutSubmit}
/>
</Modal>
</div>
);
}
}
EditListingWizard.defaultProps = {
className: null,
rootClassName: null,
listing: null,
};
EditListingWizard.propTypes = {
id: string.isRequired,
className: string,
rootClassName: string,
params: shape({
id: string.isRequired,
slug: string.isRequired,
type: oneOf(LISTING_PAGE_PARAM_TYPES).isRequired,
tab: oneOf(TABS).isRequired,
}).isRequired,
// We cannot use propTypes.listing since the listing might be a draft.
listing: shape({
attributes: shape({
publicData: object,
description: string,
geolocation: object,
pricing: object,
title: string,
}),
images: array,
}),
errors: shape({
createListingDraftError: object,
updateListingError: object,
publishListingError: object,
showListingsError: object,
uploadImageError: object,
createStripeAccountError: object,
}).isRequired,
fetchInProgress: bool.isRequired,
onPayoutDetailsFormChange: func.isRequired,
onPayoutDetailsSubmit: func.isRequired,
onManageDisableScrolling: func.isRequired,
// from withViewport
viewport: shape({
width: number.isRequired,
height: number.isRequired,
}).isRequired,
// from injectIntl
intl: intlShape.isRequired,
};
export default compose(
withViewport,
injectIntl
)(EditListingWizard);