-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (126 loc) · 3.48 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
require('dotenv').config()
const logger = require('morgan')
const express = require('express')
const errorHandler = require('errorhandler')
const bodyParser = require('body-parser')
const methodOverride = require('method-override')
const app = express()
const path = require('path')
const port = process.env.PORT || 3001
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(methodOverride())
app.use(errorHandler())
app.use(express.static(path.join(__dirname, 'public')))
const Prismic = require('@prismicio/client')
const PrismicDOM = require('prismic-dom')
const UAParser = require('ua-parser-js')
const initApi = (req) => {
return Prismic.getApi(process.env.PRISMIC_ENDPOINT, {
accessToken: process.env.PRISMIC_ACCESS_TOKEN,
req
})
}
const handleLinkResolver = (doc) => {
if (doc.type === 'product') {
return `/detail/${doc.slug}`
}
if (doc.type === 'collections') {
return '/collections'
}
if (doc.type === 'about') {
return '/about'
}
return '/'
}
app.use((req, res, next) => {
const ua = UAParser(req.headers['user-agent'])
res.locals.isDesktop = ua.device.type === undefined
res.locals.isPhone = ua.device.type === 'mobile'
res.locals.isTablet = ua.device.type === 'tablet'
res.locals.Link = handleLinkResolver
res.locals.PrismicDOM = PrismicDOM
res.locals.Numbers = index => {
return index === 0 ? 'One' : index === 1 ? 'Two' : index === 2 ? 'Three' : index === 3 ? 'Four' : ''
}
next()
})
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'pug')
const handleRequest = async (api) => {
const meta = await api.getSingle('meta')
const home = await api.getSingle('home')
const navigation = await api.getSingle('navigation')
const about = await api.getSingle('about')
const preloader = await api.getSingle('preloader')
const { results: collections } = await api.query(
Prismic.Predicates.at('document.type', 'collection'),
{
fetchLinks: 'product.image'
}
)
const assets = []
home.data.gallery.forEach(item => {
assets.push(item.image.url)
})
about.data.gallery.forEach(item => {
assets.push(item.image.url)
})
about.data.body.forEach(section => {
if (section.slice_type === 'gallery') {
section.items.forEach(item => {
assets.push(item.image.url)
})
}
})
collections.forEach(collection => {
collection.data.products.forEach(item => {
assets.push(item.products_product.data.image.url)
})
})
return {
about,
assets,
collections,
home,
meta,
navigation,
preloader
}
}
app.get('/', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
res.render('pages/home', {
...defaults
})
})
app.get('/about', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
res.render('pages/about', {
...defaults
})
})
app.get('/collections', async (req, res) => {
const api = await initApi(req)
const defaults = await handleRequest(api)
res.render('pages/collections', {
...defaults
})
})
app.get('/detail/:id', async (req, res) => {
const api = await initApi(req)
const product = await api.getByUID('product', req.params.id, {
fetchLinks: 'collection.title'
})
const defaults = await handleRequest(api)
res.render('pages/detail', {
...defaults,
product
})
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})