-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathartQuizRoutes.tsx
More file actions
126 lines (116 loc) · 3.33 KB
/
artQuizRoutes.tsx
File metadata and controls
126 lines (116 loc) · 3.33 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
import loadable from "@loadable/component"
import type { ArtsyResponse } from "Server/middleware/artsyExpress"
import type { RouteProps } from "System/Router/Route"
import { RedirectException } from "found"
import { graphql } from "react-relay"
const LOGIN_COPY = "Log in to take the art quiz."
const REDIRECT_URL = `/login?redirectTo=/art-quiz©=${LOGIN_COPY}`
const artQuizServerSideRedirect = ({ res }: { res: ArtsyResponse }) => {
if (!res.locals.sd.CURRENT_USER) {
res.redirect(REDIRECT_URL)
}
}
const ArtQuizApp = loadable(
() => import(/* webpackChunkName: "artQuizBundle" */ "./ArtQuizApp"),
{ resolveComponent: component => component.ArtQuizApp },
)
const ArtQuizWelcome = loadable(
() =>
import(/* webpackChunkName: "artQuizBundle" */ "./Routes/ArtQuizWelcome"),
{ resolveComponent: component => component.ArtQuizWelcome },
)
const ArtQuizArtworks = loadable(
() =>
import(/* webpackChunkName: "artQuizBundle" */ "./Routes/ArtQuizArtworks"),
{ resolveComponent: component => component.ArtQuizArtworksFragmentContainer },
)
const ArtQuizResults = loadable(
() =>
import(/* webpackChunkName: "artQuizBundle" */ "./Routes/ArtQuizResults"),
{ resolveComponent: component => component.ArtQuizResultsFragmentContainer },
)
export const artQuizRoutes: RouteProps[] = [
{
path: "/art-quiz",
onServerSideRender: ({ res }) => {
artQuizServerSideRedirect({ res })
res.redirect("/art-quiz/welcome")
},
getComponent: () => ArtQuizApp,
onPreloadJS: () => {
ArtQuizWelcome.preload()
ArtQuizArtworks.preload()
ArtQuizResults.preload()
},
children: [
{
path: "welcome",
getComponent: () => ArtQuizWelcome,
layout: "NavOnly",
onServerSideRender: artQuizServerSideRedirect,
query: graphql`
query artQuizRoutes_WelcomeQuery {
me {
quiz {
completedAt
}
}
}
`,
render: ({ Component, props }) => {
if (!(Component && props)) {
return
}
const { me } = props as any
if (!me?.quiz) {
return
}
const { completedAt } = me.quiz
if (completedAt) {
throw new RedirectException("/art-quiz/results")
}
return <Component {...props} />
},
},
{
path: "artworks",
getComponent: () => ArtQuizArtworks,
layout: "NavOnly",
onPreloadJS: () => {
ArtQuizArtworks.preload()
},
onServerSideRender: artQuizServerSideRedirect,
query: graphql`
query artQuizRoutes_ArtworksQuery {
me {
...ArtQuizArtworks_me
}
artworksConnection(first: 1) {
edges {
node {
isSaved
}
}
}
}
`,
},
{
path: "results",
getComponent: () => ArtQuizResults,
layout: "NavOnly",
onPreloadJS: () => {
ArtQuizResults.preload()
},
onServerSideRender: artQuizServerSideRedirect,
query: graphql`
query artQuizRoutes_ArtQuizResultsQuery {
me {
...ArtQuizResults_me
}
}
`,
},
],
},
]