-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.js
82 lines (65 loc) · 1.93 KB
/
widget.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
async function getFoodPlan(locations) {
const query = `
query {
food(locations: [${locations.map((x) => `"${x}"`).join(',')}]) {
foodData {
timestamp
meals {
name {
de
}
category
prices {
student
}
prices {
student
}
static
restaurant
}
}
}
}
`;
const req = new Request('https://api.neuland.app/graphql');
req.method = 'POST';
req.headers = {
'Content-Type': 'application/json',
};
req.body = JSON.stringify({ query });
const response = await req.loadJSON();
return response;
}
async function createWidget() {
const result = await getFoodPlan(['IngolstadtMensa'])
console.log(result)
const [{timestamp, meals}] = result.data.food.foodData
const mainMeals = meals.filter(meal => meal.category === 'main' && meal.static === false)
const widget = new ListWidget()
widget.url = `https://neuland.app/food`
const icon = widget.addText(`Mensa`)
icon.font = Font.boldSystemFont(16)
widget.addSpacer(4)
const date = widget.addText(new Date(timestamp).toLocaleDateString('de', { weekday: 'long', day: 'numeric', month: 'numeric' }))
date.font = Font.mediumSystemFont(14)
widget.addSpacer(8)
for (const meal of mainMeals) {
const stack = widget.addStack()
const typeText = stack.addText(meal.name.de)
typeText.font = Font.systemFont(14)
stack.addSpacer()
const priceText = stack.addText(meal.prices.student?.toLocaleString('de', { style: 'currency', currency: 'EUR' }))
priceText.font = Font.systemFont(14)
priceText.textColor = Color.gray()
widget.addSpacer(8)
}
widget.addSpacer()
return widget
}
const widget = await createWidget()
if (!config.runsInWidget) {
widget.presentMedium()
}
Script.setWidget(widget)
Script.complete()