-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage-generator.js
151 lines (79 loc) · 2.69 KB
/
page-generator.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
import { serialize } from "./z.js";
import { page as eventHomePage } from "./events.js";
import { page as eventPage } from "./event.js";
import fs from "fs";
import { formatPageName } from "./utils/url.js";
import faunadb from "faunadb";
let q = faunadb.query;
const client = new faunadb.Client({ secret: process.env.FAUNADB_SECRET });
const saveFile = ({data, loc}) => {
fs.writeFile(loc, data, "utf-8", (err) => {
if(err) throw err;
console.log("Saved file: " + loc);
});
};
const genEventHomePage = (events) => {
let data = serialize(eventHomePage(events));
saveFile({data, loc: "events/index.html"});
};
const genEventPages = (events) => {
for(let evt of events) {
let data = serialize(eventPage(evt));
saveFile({data, loc: "events/" + formatPageName(evt.title)});
};
};
const dateToday = () => {
const now = new Date();
const day = now.getDate();
const month = now.getMonth();
const year = now.getYear();
return { day, month, year };
}
const getTodaysEvents = (events) => {
const { startDate } = events;
const { day, month, year } = dateToday();
return events.filter(({startDate}) => {
const evtDate = new Date(startDate);
return day === evtDate.getDate() && month === evtDate.getMonth() && year === evtDate.getYear();
});
};
const daysAfterToday = (days) => {
const currentDate = new Date((new Date()).setHours(0,0,0,0));
return currentDate.setDate(currentDate.getDate() + days);
};
const getThisWeeksEvent = (events) => {
const tomorrow = daysAfterToday(1);
const dayAfterThisWeek = daysAfterToday(8);
return events.filter(({startDate}) => {
let date = +new Date(startDate);
return (date > tomorrow) && (date < dayAfterThisWeek);
});
};
const getUpcomingEvents = (events) => {
const dayAfterThisWeek = daysAfterToday(8);
const dayAfterThisMonth = daysAfterToday(31);
return events.filter(({startDate}) => {
let date = +new Date(startDate);
console.log(startDate, date, dayAfterThisMonth);
return date > dayAfterThisWeek && date < dayAfterThisMonth;
});
};
const marshallData = events => {
const today = getTodaysEvents(events);
const thisWeek = getThisWeeksEvent(events);
const upcoming = getUpcomingEvents(events);
return {today, thisWeek, upcoming};
};
const genPages = async () => {
try {
let {data: events} = await client.query(q.Map(q.Paginate(q.Match(q.Index("all_maker_events"))), q.Lambda("x", q.Select(["data"], q.Get(q.Var("x"))))));
await genEventHomePage(marshallData(events));
genEventPages(events);
} catch(err) {
console.log(err);
}
};
genPages();
/*
const genEventIndex = () => [];
*/