-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
137 lines (131 loc) · 3.71 KB
/
app.vue
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
<template>
<NuxtRouteAnnouncer/>
<h1 class="hidden">{{ $t('pageDescription') }}</h1>
<UCard :ui="{ body: { base: 'grow' } }" class="flex flex-col min-h-dvh">
<template #header>
<PageHeader class="h-10"/>
</template>
<div class="flex justify-center items-center flex-wrap mb-4 gap-5">
<DateSelect v-model:date="date" :dates="dates"/>
<OptionsMenu v-model:config="filterConfig"/>
</div>
<UContainer class="flex flex-wrap justify-center max-w-7xl" role="main">
<RestaurantMenu
v-for="restaurant in restaurants"
:key="restaurant.name"
:date="date"
:restaurant="restaurant"
/>
</UContainer>
<template #footer>
<PageFooter class="h-10"/>
</template>
</UCard>
</template>
<script>
export default defineNuxtComponent({
name: "app",
data() {
return {
date: new Date(),
filterConfig: {
filters: {
vegan: false,
eggFree: false,
milkFree: false,
glutenFree: false,
lactoseFree: false,
recommended: false,
},
method: "highlight",
},
};
},
async setup() {
useSeoMeta({
title: "OUF",
description: "Oulu University lunch menus",
ogImage: "/logo.png",
});
const orderCookie = useCookie('order', { maxAge: 60 * 60 * 24 * 365 }); // 1 year
const order = useState('order', () => {
return orderCookie.value;
});
const campus = CAMPUSES.OULU.LINNANMAA;
const response = await useFetch(`/api/menu?city=${campus.city}&campus=${campus.campus}`, {
key: "restaurants",
server: true,
transform: (data) => {
if(!order.value) {
order.value = data.map(r => r.name);
}
return data.map(r => {
r.menu = r.menu.map(m => {
m.date = new Date(m.date);
return m;
});
return r;
}).sort((a, b) => {
return order.value.indexOf(a.name) - order.value.indexOf(b.name);
});
}
});
// Check for removed or added restaurants and add new ones to the end of the list
if(Array.isArray(order.value)) {
const r = response.data.value.map(r => r.name);
const newItems = r.filter(i => !order.value.includes(i));
const removedItems = order.value.filter(i => !r.includes(i));
if(newItems.length) order.value.push(...newItems);
if(removedItems.length) order.value = order.value.filter(i => !removedItems.includes(i));
}
return {
apiRestaurants: response.data,
order,
};
},
created() {
useHead({
titleTemplate: (titleChunk) => {
return titleChunk ? `${titleChunk} - ${this.$t("title")}` : this.$t("title");
},
});
this.date = this.dates[0];
},
beforeMount() {
if(!this.restaurants?.length) {
alert("Could not fetch data from the server. Please try again later.");
}
},
mounted() {
umTrackView();
},
computed: {
restaurants() {
if(!this.apiRestaurants) return [];
if(!this.order) return this.apiRestaurants;
return this.apiRestaurants.filter(r => r.menu.length).sort((a, b) => {
return this.order.indexOf(a.name) - this.order.indexOf(b.name);
});
},
dates() {
if(!this.restaurants?.length) return [];
const dates = new Set();
this.restaurants.forEach(r => {
r.menu.forEach(m => {
if(m.fi?.length)
dates.add(m.date.toISOString());
});
});
return Array.from(dates)
.map(d => new Date(d))
.sort((a, b) => a - b)
.slice(0, 6);
},
}
});
</script>
<style>
* {
transition: background-color 0.5s ease-out, border-color 0.5s ease-out, box-shadow 0.5s ease-out;
}
</style>