-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathArtworkRelatedArtists.tsx
More file actions
256 lines (233 loc) · 6.63 KB
/
ArtworkRelatedArtists.tsx
File metadata and controls
256 lines (233 loc) · 6.63 KB
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
import { ContextModule } from "@artsy/cohesion"
import * as DeprecatedSchema from "@artsy/cohesion/dist/DeprecatedSchema"
import {
Box,
Button,
Column,
Flex,
GridColumns,
Skeleton,
SkeletonText,
Spacer,
Text,
} from "@artsy/palette"
import { hideGrid } from "Apps/Artwork/Components/OtherWorks"
import { EntityHeaderArtistFragmentContainer } from "Components/EntityHeaders/EntityHeaderArtist"
import { useSystemContext } from "System/Hooks/useSystemContext"
import { SystemQueryRenderer } from "System/Relay/SystemQueryRenderer"
import { extractNodes } from "Utils/extractNodes"
import createLogger from "Utils/logger"
import type { ArtworkRelatedArtistsQuery } from "__generated__/ArtworkRelatedArtistsQuery.graphql"
import type { ArtworkRelatedArtists_artwork$data } from "__generated__/ArtworkRelatedArtists_artwork.graphql"
import { useState } from "react"
import type * as React from "react"
import {
type RelayPaginationProp,
createPaginationContainer,
graphql,
} from "react-relay"
import track, { useTracking } from "react-tracking"
const logger = createLogger("ArtworkRelatedArtists.tsx")
export interface ArtworkRelatedArtistsProps {
artwork: ArtworkRelatedArtists_artwork$data
relay: RelayPaginationProp
}
const PAGE_SIZE = 6
export const ArtworkRelatedArtists: React.FC<
React.PropsWithChildren<ArtworkRelatedArtistsProps>
> = track()(props => {
const { trackEvent } = useTracking()
const [fetchingNextPage, setFetchingNextPage] = useState(false)
const {
artwork: { artist },
relay,
} = props
if (hideGrid(artist?.related?.artistsConnection)) {
return null
}
const fetchData = () => {
if (!relay.hasMore() || relay.isLoading()) {
return
}
setFetchingNextPage(true)
relay.loadMore(PAGE_SIZE, error => {
if (error) {
logger.error(error)
}
setFetchingNextPage(false)
})
}
const artists = extractNodes(artist?.related?.artistsConnection)
return (
<Box data-test={ContextModule.relatedArtistsRail}>
<Text variant="lg-display">Related artists</Text>
<Spacer y={4} />
<GridColumns>
{artists.map((node, index) => {
return (
<Column key={index} span={[12, 6, 4, 4]}>
<EntityHeaderArtistFragmentContainer
artist={node}
onClick={() => {
trackEvent({
context_module:
DeprecatedSchema.ContextModule.RelatedArtists,
type: DeprecatedSchema.Type.ArtistCard,
action_type: DeprecatedSchema.ActionType.Click,
})
}}
/>
</Column>
)
})}
</GridColumns>
<Spacer y={4} />
{relay.hasMore() && (
<ShowMoreButton onClick={fetchData} loading={fetchingNextPage} />
)}
</Box>
)
})
const ShowMoreButton: React.FC<
React.PropsWithChildren<{ onClick: () => void; loading: boolean }>
> = ({ onClick, loading }) => {
return (
<Flex justifyContent="center">
<Button onClick={onClick} loading={loading}>
Show More
</Button>
</Flex>
)
}
export const ArtworkRelatedArtistsPaginationContainer =
createPaginationContainer(
ArtworkRelatedArtists,
{
artwork: graphql`
fragment ArtworkRelatedArtists_artwork on Artwork
@argumentDefinitions(
count: { type: "Int", defaultValue: 6 }
cursor: { type: "String", defaultValue: "" }
) {
slug
artist(shallow: true) {
href
related {
artistsConnection(kind: MAIN, first: $count, after: $cursor)
@connection(key: "ArtworkRelatedArtists_artistsConnection") {
pageInfo {
hasNextPage
}
edges {
node {
...EntityHeaderArtist_artist
}
}
}
}
}
}
`,
},
{
direction: "forward",
getConnectionFromProps(props) {
return props.artwork.artist?.related?.artistsConnection
},
getFragmentVariables(prevVars, count) {
return {
...prevVars,
count,
}
},
getVariables(props, { count, cursor }, fragmentVariables) {
return {
...fragmentVariables,
count,
cursor,
artworkID: props.artwork.slug,
}
},
query: graphql`
query ArtworkRelatedArtistsPaginationQuery(
$count: Int!
$cursor: String
$artworkID: String!
) {
artwork(id: $artworkID) {
...ArtworkRelatedArtists_artwork
@arguments(count: $count, cursor: $cursor)
}
}
`,
},
)
const PLACEHOLDER = (
<Skeleton>
<Text variant="lg-display">Related artists</Text>
<Spacer y={4} />
<GridColumns>
{[...new Array(4)].map((node, index) => {
return (
<Column key={index} span={[12, 6, 3, 3]}>
<Flex flexDirection="row">
<Flex
width="100%"
alignItems="center"
justifyContent="space-between"
>
<Box>
<SkeletonText variant="sm">Georges Braque</SkeletonText>
<SkeletonText variant="xs">French 1900-2000</SkeletonText>
</Box>
<Button variant="secondaryBlack" size="small">
Follow
</Button>
</Flex>
</Flex>
</Column>
)
})}
</GridColumns>
</Skeleton>
)
export const ArtworkRelatedArtistsQueryRenderer: React.FC<
React.PropsWithChildren<{
slug: string
}>
> = ({ slug }) => {
const { relayEnvironment } = useSystemContext()
return (
<Box data-test="ArtworkRelatedArtistsQueryRenderer">
<SystemQueryRenderer<ArtworkRelatedArtistsQuery>
lazyLoad
environment={relayEnvironment}
variables={{ slug }}
placeholder={PLACEHOLDER}
query={graphql`
query ArtworkRelatedArtistsQuery($slug: String!) {
artwork(id: $slug) {
...ArtworkRelatedArtists_artwork
}
}
`}
render={({ error, props }) => {
if (error) {
console.error(error)
return null
}
if (!props) {
return PLACEHOLDER
}
if (props.artwork) {
return (
<ArtworkRelatedArtistsPaginationContainer
artwork={props.artwork}
/>
)
}
}}
/>
</Box>
)
}