This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathapp.js
198 lines (161 loc) · 4.49 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
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
/* eslint-disable no-unused-vars */
require('dotenv').config();
const fetch = require('node-fetch');
const logger = require('morgan');
const path = require('path');
const express = require('express');
const errorHandler = require('errorhandler');
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const app = express();
const port = process.env.PORT || 8004;
const Prismic = require('@prismicio/client');
const PrismicH = require('@prismicio/helpers');
const { application } = require('express');
const UAParser = require('ua-parser-js');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(errorHandler());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
// Initialize the prismic.io api
const initApi = (req) => {
return Prismic.createClient(process.env.PRISMIC_ENDPOINT, {
accessToken: process.env.PRISMIC_ACCESS_TOKEN,
req,
fetch,
});
};
// Link Resolver
const HandleLinkResolver = (doc) => {
if (doc.type === 'product') {
return `/detail/${doc.slug}`;
}
if (doc.type === 'collections') {
return '/collections';
}
if (doc.type === 'about') {
return `/about`;
}
// Default to homepage
return '/';
};
// Middleware to inject prismic context
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.PrismicH = PrismicH;
res.locals.Numbers = (index) => {
return index === 0
? 'One'
: index === 1
? 'Two'
: index === 2
? 'Three'
: index === 3
? 'Four'
: '';
};
next();
});
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
app.locals.basedir = app.get('views');
const handleRequest = async (api) => {
const [meta, preloader, navigation, home, about, { results: collections }] =
await Promise.all([
api.getSingle('meta'),
api.getSingle('preloader'),
api.getSingle('navigation'),
api.getSingle('home'),
api.getSingle('about'),
api.query(Prismic.Predicates.at('document.type', 'collection'), {
fetchLinks: 'product.image',
}),
]);
// console.log(about, home, collections);
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.list.forEach((item) => {
assets.push(item.product.data.image.url);
});
});
console.log(assets);
return {
assets,
meta,
home,
collections,
about,
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/:uid', async (req, res) => {
// const api = await initApi(req);
// const defaults = await handleRequest(api);
// const product = await api.getByUID('product', req.params.uid, {
// fetchLinks: 'collection.title',
// });
// if (product) {
// res.render('pages/detail', {
// product,
// ...defaults,
// });
// } else {
// res.status(404).send('Page not found');
// }
// });
app.get('/detail/:uid', async (req, res) => {
const api = await initApi(req);
const defaults = await handleRequest(api);
const product = await api.getByUID('product', req.params.uid, {
fetchLinks: 'collection.title',
});
res.render('pages/detail', {
...defaults,
product,
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
// "start": "concurrently --kill-others \"npm run backend:development\" \"npm run frontend:development\""