/
ListingLink.js
80 lines (73 loc) · 2.08 KB
/
ListingLink.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
/*
A component so safely link to the ListingPage of the given listing.
When the listing is pending approval, the normal ListingPage won't
work as the listing isn't yet published. This component links to the
correct pending-approval variant URL or to the normal ListingPage
based on the listing state.
*/
import React from 'react';
import { string, oneOfType, node } from 'prop-types';
import { richText } from '../../util/richText';
import { LISTING_STATE_DRAFT, LISTING_STATE_PENDING_APPROVAL, propTypes } from '../../util/types';
import {
LISTING_PAGE_DRAFT_VARIANT,
LISTING_PAGE_PENDING_APPROVAL_VARIANT,
createSlug,
} from '../../util/urlHelpers';
import { NamedLink } from '../../components';
import css from './ListingLink.css';
const MIN_LENGTH_FOR_LONG_WORDS = 16;
const ListingLink = props => {
const { className, listing, children } = props;
const listingLoaded = listing && listing.id;
if (!listingLoaded) {
return null;
}
const id = listing.id.uuid;
const { title, state } = listing.attributes;
const slug = createSlug(title);
const richTitle = (
<span>
{richText(title, {
longWordMinLength: MIN_LENGTH_FOR_LONG_WORDS,
longWordClass: css.longWord,
})}
</span>
);
const isPendingApproval = state === LISTING_STATE_PENDING_APPROVAL;
const isDraft = state === LISTING_STATE_DRAFT;
const variant = isPendingApproval
? LISTING_PAGE_PENDING_APPROVAL_VARIANT
: isDraft
? LISTING_PAGE_DRAFT_VARIANT
: null;
const linkProps = !!variant
? {
name: 'ListingPageVariant',
params: {
id,
slug,
variant,
},
}
: {
name: 'ListingPage',
params: { id, slug },
};
return (
<NamedLink className={className} {...linkProps}>
{children ? children : richTitle || ''}
</NamedLink>
);
};
ListingLink.defaultProps = {
className: null,
listing: null,
children: null,
};
ListingLink.propTypes = {
className: string,
listing: oneOfType([propTypes.listing, propTypes.ownListing]),
children: node,
};
export default ListingLink;