/
ManageListingCard.js
365 lines (341 loc) · 11.5 KB
/
ManageListingCard.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import React from 'react';
import PropTypes from 'prop-types';
import { compose } from 'redux';
import { withRouter } from 'react-router-dom';
import { FormattedMessage, intlShape, injectIntl } from 'react-intl';
import classNames from 'classnames';
import routeConfiguration from '../../routeConfiguration';
import {
LISTING_STATE_PENDING_APPROVAL,
LISTING_STATE_CLOSED,
LISTING_STATE_DRAFT,
propTypes,
} from '../../util/types';
import { formatMoney } from '../../util/currency';
import { ensureOwnListing } from '../../util/data';
import {
LISTING_PAGE_PENDING_APPROVAL_VARIANT,
LISTING_PAGE_DRAFT_VARIANT,
LISTING_PAGE_PARAM_TYPE_DRAFT,
LISTING_PAGE_PARAM_TYPE_EDIT,
createSlug,
} from '../../util/urlHelpers';
import { createResourceLocatorString } from '../../util/routes';
import config from '../../config';
import {
InlineTextButton,
Menu,
MenuLabel,
MenuContent,
MenuItem,
NamedLink,
IconSpinner,
ResponsiveImage,
} from '../../components';
import MenuIcon from './MenuIcon';
import Overlay from './Overlay';
import css from './ManageListingCard.css';
// Menu content needs the same padding
const MENU_CONTENT_OFFSET = -12;
const MAX_LENGTH_FOR_WORDS_IN_TITLE = 7;
const priceData = (price, intl) => {
if (price && price.currency === config.currency) {
const formattedPrice = formatMoney(intl, price);
return { formattedPrice, priceTitle: formattedPrice };
} else if (price) {
return {
formattedPrice: intl.formatMessage(
{ id: 'ManageListingCard.unsupportedPrice' },
{ currency: price.currency }
),
priceTitle: intl.formatMessage(
{ id: 'ManageListingCard.unsupportedPriceTitle' },
{ currency: price.currency }
),
};
}
return {};
};
const createListingURL = (routes, listing) => {
const id = listing.id.uuid;
const slug = createSlug(listing.attributes.title);
const isPendingApproval = listing.attributes.state === LISTING_STATE_PENDING_APPROVAL;
const isDraft = listing.attributes.state === LISTING_STATE_DRAFT;
const variant = isDraft
? LISTING_PAGE_DRAFT_VARIANT
: isPendingApproval
? LISTING_PAGE_PENDING_APPROVAL_VARIANT
: null;
const linkProps =
isPendingApproval || isDraft
? {
name: 'ListingPageVariant',
params: {
id,
slug,
variant,
},
}
: {
name: 'ListingPage',
params: { id, slug },
};
return createResourceLocatorString(linkProps.name, routes, linkProps.params, {});
};
// Cards are not fixed sizes - So, long words in title make flexboxed items to grow too big.
// 1. We split title to an array of words and spaces.
// "foo bar".split(/([^\s]+)/gi) => ["", "foo", " ", "bar", ""]
// 2. Then we break long words by adding a '<span>' with word-break: 'break-all';
const formatTitle = (title, maxLength) => {
const nonWhiteSpaceSequence = /([^\s]+)/gi;
return title.split(nonWhiteSpaceSequence).map((word, index) => {
return word.length > maxLength ? (
<span key={index} style={{ wordBreak: 'break-all' }}>
{word}
</span>
) : (
word
);
});
};
export const ManageListingCardComponent = props => {
const {
className,
rootClassName,
hasClosingError,
hasOpeningError,
history,
intl,
isMenuOpen,
actionsInProgressListingId,
listing,
onCloseListing,
onOpenListing,
onToggleMenu,
renderSizes,
} = props;
const classes = classNames(rootClassName || css.root, className);
const currentListing = ensureOwnListing(listing);
const id = currentListing.id.uuid;
const { title = '', price, state } = currentListing.attributes;
const slug = createSlug(title);
const isPendingApproval = state === LISTING_STATE_PENDING_APPROVAL;
const isClosed = state === LISTING_STATE_CLOSED;
const isDraft = state === LISTING_STATE_DRAFT;
const firstImage =
currentListing.images && currentListing.images.length > 0 ? currentListing.images[0] : null;
const menuItemClasses = classNames(css.menuItem, {
[css.menuItemDisabled]: !!actionsInProgressListingId,
});
const { formattedPrice, priceTitle } = priceData(price, intl);
const hasError = hasOpeningError || hasClosingError;
const thisListingInProgress =
actionsInProgressListingId && actionsInProgressListingId.uuid === id;
const titleClasses = classNames(css.title, {
[css.titlePending]: isPendingApproval,
[css.titleDraft]: isDraft,
});
const editListingLinkType = isDraft
? LISTING_PAGE_PARAM_TYPE_DRAFT
: LISTING_PAGE_PARAM_TYPE_EDIT;
return (
<div className={classes}>
<div
className={css.threeToTwoWrapper}
tabIndex={0}
onClick={event => {
event.preventDefault();
event.stopPropagation();
// ManageListingCard contains links, buttons and elements that are working with routing.
// This card doesn't work if <a> or <button> is used to wrap events that are card 'clicks'.
//
// NOTE: It might be better to absolute-position those buttons over a card-links.
// (So, that they have no parent-child relationship - like '<a>bla<a>blaa</a></a>')
history.push(createListingURL(routeConfiguration(), listing));
}}
>
<div className={css.aspectWrapper}>
<ResponsiveImage
rootClassName={css.rootForImage}
alt={title}
image={firstImage}
variants={['landscape-crop', 'landscape-crop2x']}
sizes={renderSizes}
/>
</div>
<div className={classNames(css.menuOverlayWrapper, { [css.menuOverlayOpen]: isMenuOpen })}>
<div className={classNames(css.menuOverlay)} />
<div className={css.menuOverlayContent}>
<FormattedMessage id="ManageListingCard.viewListing" />
</div>
</div>
<div className={css.menubarWrapper}>
<div className={css.menubarGradient} />
<div className={css.menubar}>
<Menu
className={classNames(css.menu, { [css.cardIsOpen]: !isClosed })}
contentPlacementOffset={MENU_CONTENT_OFFSET}
contentPosition="left"
useArrow={false}
onToggleActive={isOpen => {
const listingOpen = isOpen ? currentListing : null;
onToggleMenu(listingOpen);
}}
isOpen={isMenuOpen}
>
<MenuLabel className={css.menuLabel} isOpenClassName={css.listingMenuIsOpen}>
<div className={css.iconWrapper}>
<MenuIcon className={css.menuIcon} isActive={isMenuOpen} />
</div>
</MenuLabel>
<MenuContent rootClassName={css.menuContent}>
<MenuItem key="close-listing">
<InlineTextButton
className={menuItemClasses}
onClick={event => {
event.preventDefault();
event.stopPropagation();
if (!actionsInProgressListingId) {
onToggleMenu(null);
onCloseListing(currentListing.id);
}
}}
>
<FormattedMessage id="ManageListingCard.closeListing" />
</InlineTextButton>
</MenuItem>
</MenuContent>
</Menu>
</div>
</div>
{isDraft ? (
<React.Fragment>
<div className={classNames({ [css.draftNoImage]: !firstImage })} />
<Overlay
message={intl.formatMessage(
{ id: 'ManageListingCard.draftOverlayText' },
{ listingTitle: title }
)}
>
<NamedLink
className={css.finishListingDraftLink}
name="EditListingPage"
params={{ id, slug, type: LISTING_PAGE_PARAM_TYPE_DRAFT, tab: 'photos' }}
>
<FormattedMessage id="ManageListingCard.finishListingDraft" />
</NamedLink>
</Overlay>
</React.Fragment>
) : null}
{isClosed ? (
<Overlay
message={intl.formatMessage(
{ id: 'ManageListingCard.closedListing' },
{ listingTitle: title }
)}
>
<button
className={css.openListingButton}
disabled={!!actionsInProgressListingId}
onClick={event => {
event.preventDefault();
event.stopPropagation();
if (!actionsInProgressListingId) {
onOpenListing(currentListing.id);
}
}}
>
<FormattedMessage id="ManageListingCard.openListing" />
</button>
</Overlay>
) : null}
{isPendingApproval ? (
<Overlay
message={intl.formatMessage(
{ id: 'ManageListingCard.pendingApproval' },
{ listingTitle: title }
)}
/>
) : null}
{thisListingInProgress ? (
<Overlay>
<IconSpinner />
</Overlay>
) : hasError ? (
<Overlay errorMessage={intl.formatMessage({ id: 'ManageListingCard.actionFailed' })} />
) : null}
</div>
<div className={css.info}>
<div className={css.price}>
{formattedPrice ? (
<React.Fragment>
<div className={css.priceValue} title={priceTitle}>
{formattedPrice}
</div>
<div className={css.perUnit}>
<FormattedMessage id="ManageListingCard.perUnit" />
</div>
</React.Fragment>
) : (
<div className={css.noPrice}>
<FormattedMessage id="ManageListingCard.priceNotSet" />
</div>
)}
</div>
<div className={css.mainInfo}>
<div className={css.titleWrapper}>
<InlineTextButton
className={titleClasses}
onClick={event => {
event.preventDefault();
event.stopPropagation();
history.push(createListingURL(routeConfiguration(), listing));
}}
>
{formatTitle(title, MAX_LENGTH_FOR_WORDS_IN_TITLE)}
</InlineTextButton>
</div>
</div>
<div className={css.manageLinks}>
<NamedLink
className={css.manageLink}
name="EditListingPage"
params={{ id, slug, type: editListingLinkType, tab: 'description' }}
>
<FormattedMessage id="ManageListingCard.editListing" />
</NamedLink>
</div>
</div>
</div>
);
};
ManageListingCardComponent.defaultProps = {
className: null,
rootClassName: null,
actionsInProgressListingId: null,
renderSizes: null,
};
const { bool, func, shape, string } = PropTypes;
ManageListingCardComponent.propTypes = {
className: string,
rootClassName: string,
hasClosingError: bool.isRequired,
hasOpeningError: bool.isRequired,
intl: intlShape.isRequired,
listing: propTypes.ownListing.isRequired,
isMenuOpen: bool.isRequired,
actionsInProgressListingId: shape({ uuid: string.isRequired }),
onCloseListing: func.isRequired,
onOpenListing: func.isRequired,
onToggleMenu: func.isRequired,
// Responsive image sizes hint
renderSizes: string,
// from withRouter
history: shape({
push: func.isRequired,
}).isRequired,
};
export default compose(
withRouter,
injectIntl
)(ManageListingCardComponent);