-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
170 lines (164 loc) · 7.59 KB
/
index.html
File metadata and controls
170 lines (164 loc) · 7.59 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Daily Words</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-router@3.5.3/dist/vue-router.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<style>
@import url('https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css');
</style>
</head>
<body class="bg-gray-100 p-4">
<div id="app" class="max-w-4xl mx-auto">
<router-view></router-view>
</div>
<script>
Vue.config.productionTip = false;
Vue.config.devtools = false;
const WordOfTheDay = {
template: `
<div>
<h1 class="text-3xl font-bold text-center mb-4">Words of the day for {{ formattedDate }}</h1>
<div class="flex justify-between mb-4">
<div>
<button v-if="previousExists" @click="navigateToPrevious" class="bg-blue-500 text-white px-4 py-2 rounded">Previous</button>
</div>
<div class="flex-grow"></div>
<div>
<button v-if="nextExists" @click="navigateToNext" class="bg-blue-500 text-white px-4 py-2 rounded">Next</button>
</div>
</div>
<div v-if="words.length === 0" class="text-center">
<p class="text-red-500">There are no words for this day.</p>
<button @click="goToToday" class="bg-blue-500 text-white px-4 py-2 rounded mt-4">Go to today's date</button>
</div>
<div v-else>
<div v-for="(wordGroup, groupIndex) in words" :key="groupIndex" class="bg-white p-4 my-4 rounded-lg shadow-lg">
<div v-for="(wordData, wordIndex) in wordGroup" :key="wordData.word + wordIndex">
<h2 class="text-2xl font-bold mb-2">{{ wordData.word }}</h2>
<p v-if="wordData.phonetic" class="italic text-gray-600 mb-2">{{ wordData.phonetic }}</p>
<div v-for="(meaning, meaningIndex) in wordData.meanings" :key="meaning.partOfSpeech + meaningIndex" class="mb-4">
<h3 class="text-xl font-semibold">{{ meaning.partOfSpeech }}</h3>
<ul class="list-disc pl-5">
<li v-for="(definition, defIndex) in meaning.definitions" :key="definition.definition + defIndex" class="mb-1">
<p>{{ definition.definition }}</p>
<p v-if="definition.example" class="text-sm text-gray-500">Example: {{ definition.example }}</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
`,
data() {
return {
words: [],
previousExists: false,
nextExists: false
};
},
computed: {
formattedDate() {
const date = this.$route.params.date;
const options = { year: 'numeric', month: 'long', day: 'numeric' };
return new Date(date).toLocaleDateString(undefined, options);
}
},
methods: {
formatDate(date) {
const d = new Date(date);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
},
loadWords(date) {
const formattedDate = this.formatDate(date);
axios.get(`archive/${formattedDate}.json`)
.then(response => {
this.words = response.data;
})
.catch(() => {
this.words = [];
this.previousExists = false;
this.nextExists = false;
});
this.checkPrevious(date);
this.checkNext(date);
},
checkPrevious(date) {
const previousDate = new Date(date);
previousDate.setDate(previousDate.getDate() - 1);
const formattedDate = this.formatDate(previousDate);
axios.get(`archive/${formattedDate}.json`)
.then(() => {
this.previousExists = true;
})
.catch(() => {
this.previousExists = false;
});
},
checkNext(date) {
const nextDate = new Date(date);
nextDate.setDate(nextDate.getDate() + 1);
const formattedDate = this.formatDate(nextDate);
axios.get(`archive/${formattedDate}.json`)
.then(() => {
this.nextExists = true;
})
.catch(() => {
this.nextExists = false;
});
},
navigateToPrevious() {
if (this.previousExists) {
const previousDate = new Date(this.$route.params.date);
previousDate.setDate(previousDate.getDate() - 1);
this.$router.push({ name: 'words', params: { date: this.formatDate(previousDate) } });
}
},
navigateToNext() {
if (this.nextExists) {
const nextDate = new Date(this.$route.params.date);
nextDate.setDate(nextDate.getDate() + 1);
this.$router.push({ name: 'words', params: { date: this.formatDate(nextDate) } });
}
},
goToToday() {
const today = new Date();
this.$router.push({ name: 'words', params: { date: this.formatDate(today) } });
}
},
watch: {
'$route.params.date'(newDate) {
this.loadWords(newDate);
}
},
mounted() {
this.loadWords(this.$route.params.date);
}
};
const routes = [
{ path: '/words-for/:date', name: 'words', component: WordOfTheDay, props: true },
{ path: '*', redirect: () => {
const today = new Date();
const year = today.getFullYear();
const month = String(today.getMonth() + 1).padStart(2, '0');
const day = String(today.getDate()).padStart(2, '0');
return `/words-for/${year}-${month}-${day}`;
}}
];
const router = new VueRouter({
routes
});
new Vue({
el: '#app',
router
});
</script>
</body>
</html>