-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
150 lines (111 loc) · 3.73 KB
/
app.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
require('dotenv').config()
const Prismic = require('prismic-javascript')
const PrismicDOM = require('prismic-dom')
const filter = require('lodash/filter')
const find = require('lodash/find')
const map = require('lodash/map')
const parser = require('ua-parser-js')
const Onboarding = require('./onboarding')
const app = require('./config')
const get = (results, request) => {
const analytics = process.env.GOOGLE_ANALYTICS
const ua = parser(request.headers['user-agent'])
const functionals = find(results, { type: 'functionals' })
const meta = find(results, { type: 'meta' })
const navigation = find(results, { type: 'navigation' })
const sharing = find(results, { type: 'sharing' })
const social = find(results, { type: 'social' })
const isDesktop = ua.device.type === undefined
const isPhone = ua.device.type === 'mobile'
const isTablet = ua.device.type === 'tablet'
const projectsList = filter(results, { type: 'project' })
const { data: { list: projectsOrder } } = find(results, { type: 'ordering' })
const projects = map(projectsOrder, ({ project : { uid } }) => {
return find(projectsList, { uid })
})
return {
analytics,
functionals,
isDesktop,
isPhone,
isTablet,
meta,
navigation,
projects,
sharing,
social
}
}
app.listen(app.get('port'), () => {
Onboarding.trigger()
})
app.use((request, response, next) => {
const accessToken = process.env.PRISMIC_ACCESS_TOKEN
const endpoint = process.env.PRISMIC_ENDPOINT
response.locals.PrismicDOM = PrismicDOM
Prismic.api(endpoint, {
accessToken,
request
}).then(api => {
request.prismic = { api }
next()
}).catch(error => {
next(error.message)
})
})
app.get('/', (request, response) => {
request.prismic.api.query('', { pageSize : 100 }).then(({ results }) => {
const home = find(results, { type: 'home' })
const standard = get(results, request)
response.render('pages/home', { home, ...standard })
})
})
app.get('/index', (request, response) => {
request.prismic.api.query('', { pageSize : 100 }).then(({ results }) => {
const index = find(results, { type: 'index' })
const standard = get(results, request)
if (standard.isPhone) {
response.redirect('/')
} else {
response.render('pages/index', { index, ...standard })
}
})
})
app.get('/about', (request, response) => {
request.prismic.api.query('', { pageSize : 100 }).then(({ results }) => {
const about = find(results, { type: 'about' })
const standard = get(results, request)
response.render('pages/about', { about, ...standard })
})
})
app.get('/essays', (request, response) => {
request.prismic.api.query('', { pageSize : 100 }).then(({ results }) => {
const about = find(results, { type: 'about' })
const essays = find(results, { type: 'essays' })
const standard = get(results, request)
response.render('pages/essays', { about, essays, ...standard })
})
})
app.get('/case/:id', (request, response) => {
request.prismic.api.query('', { pageSize : 100 }).then(({ results }) => {
const standard = get(results, request)
const { projects } = standard
const cases = find(results, { type: 'projects' })
const project = find(projects, { uid: request.params.id })
const projectIndex = projects.indexOf(project)
const related = projects[projectIndex + 1] ? projects[projectIndex + 1] : projects[0]
response.render('pages/case', { ...standard, cases, project, projectIndex, related })
})
})
app.use((request, response) => {
response.status(404)
if (request.accepts('html')) {
response.redirect('/')
return
}
if (request.accepts('json')) {
response.send({ error: 'Not Found' })
return
}
response.type('txt').send('Not Found')
})