Skip to content

Commit

Permalink
refactor: use raw names as keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan-Kuan committed Aug 28, 2023
1 parent 75cfa37 commit 653ac54
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 64 deletions.
2 changes: 1 addition & 1 deletion site/history.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"graduate":{"date":["2023-08-01","2023-08-02","2023-08-07","2023-08-08","2023-08-09","2023-08-10","2023-08-11","2023-08-14","2023-08-15","2023-08-16","2023-08-17","2023-08-18","2023-08-20","2023-08-21","2023-08-22","2023-08-23","2023-08-24","2023-08-25"],"male":[8,19,85,86,95,108,114,170,213,233,239,239,239,299,310,319,336,361],"female":[6,6,15,15,19,19,19,27,27,27,27,27,27,47,47,49,56,85]},"undergraduate":{"date":["2023-08-20","2023-08-21","2023-08-22","2023-08-23","2023-08-24","2023-08-25"],"male":[58,58,58,77,82,82],"female":[0,0,0,0,0,0]},"summer":{"date":["2023-08-20","2023-08-21"],"male":[249,249],"female":[149,149]},"move":{"date":["2023-08-21","2023-08-22","2023-08-23","2023-08-24"],"male":[0,0,0,0],"female":[0,0,0,0]},"new-and-transfer":{"date":[],"male":[],"female":[]}}
{"history":{"112-1研究所宿舍申請":{"date":["2023-08-01","2023-08-02","2023-08-07","2023-08-08","2023-08-09","2023-08-10","2023-08-11","2023-08-14","2023-08-15","2023-08-16","2023-08-17","2023-08-18","2023-08-20","2023-08-21","2023-08-22","2023-08-23","2023-08-24","2023-08-25"],"male":[8,19,85,86,95,108,114,170,213,233,239,239,239,299,310,319,336,361],"female":[6,6,15,15,19,19,19,27,27,27,27,27,27,47,47,49,56,85]}, "112-1學士班學生申請校內宿舍(舊生候補)":{"date":["2023-08-20","2023-08-21","2023-08-22","2023-08-23","2023-08-24","2023-08-25"],"male":[58,58,58,77,82,82],"female":[0,0,0,0,0,0]}, "學士班新生宿舍暑假住宿申請作業":{"date":["2023-08-20","2023-08-21"],"male":[249,249],"female":[149,149]}, "111-2新生宿舍遷出及高年級宿舍調遷作業":{"date":["2023-08-21","2023-08-22","2023-08-23","2023-08-24"],"male":[0,0,0,0],"female":[0,0,0,0]}},"last_saved_date":"2023-08-25"}
135 changes: 72 additions & 63 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ <h1 class="font-500">112-1 臺灣大學宿舍候補紀錄</h1>
</p>
<p>
※ 08/01 至 08/18 研究生宿舍的紀錄為友人提供的資料。
<br />
※ 本站於 08/22 自行決定停止更新「學士班新生宿舍暑假住宿申請作業」。
</p>
<p>
資料來源:
Expand All @@ -44,25 +46,25 @@ <h1 class="font-500">112-1 臺灣大學宿舍候補紀錄</h1>
<div class="border-b-1 border-b-solid border-gray"></div>

<div
v-for="group_key, group_name in groups"
v-for="group, name in history"
class="mb-10"
>
<h2 class="
pl-2
border-l-5 border-l-solid border-red-8
font-200"
>
{{ group_name }}
{{ name }}
<span
v-if="ignored_groups.includes(group_key)"
v-if="group.date.slice(-1)[0] !== today"
class="text-red-600 font-500"
>
(本站停止更新)
(停止更新)
</span>
</h2>

<div class="mb-5">
<canvas :id="`chart-${ group_key }`"></canvas>
<canvas :id="`chart-${name}`"></canvas>
</div>

<table
Expand All @@ -85,13 +87,14 @@ <h1 class="font-500">112-1 臺灣大學宿舍候補紀錄</h1>
</tr>
</thead>
<tbody>
<tr v-for="_, i in history[group_key].date">
<td v-for="key in ['date', 'male', 'female']"
<tr v-for="_, i in group.date">
<td
v-for="key in ['date', 'male', 'female']"
class="
p-1
border-solid border-size-1"
>
{{ history[group_key][key][i] }}
{{ group[key][i] }}
</td>
</tr>
</tbody>
Expand All @@ -118,86 +121,92 @@ <h1 class="font-500">112-1 臺灣大學宿舍候補紀錄</h1>

createApp({
setup() {
const groups = {
'112-1研究所宿舍申請': 'graduate',
'學士班新生暨轉學生申請宿舍': 'new-and-transfer',
'112-1學士班學生申請校內宿舍(舊生候補)': 'undergraduate',
'學士班新生宿舍暑假住宿申請作業': 'summer',
'111-2新生宿舍遷出及高年級宿舍調遷作業': 'move',
};
const ignored_groups = [ 'summer', 'move' ];
const ignored_groups = [
'學士班新生宿舍暑假住宿申請作業',
];
const today = (new Date()).toISOString().split('T')[0];
const history = ref({});

return {
groups,
ignored_groups,
today,
history,
};
},
async mounted() {
this.history = await fetch('./history.json')
const data = await fetch('./history.json')
.then(res => res.json());

const today = (new Date()).toISOString().split('T')[0];
this.history = data.history;

if (data.last_saved_date !== this.today) {
await this.fetchLatestData();
}

if (this.history.graduate.date.slice(-1)[0] !== today) {
const data_endpoint = 'https://housing.ntu.edu.tw/index/get.progress.json';
this.drawCharts();
},
methods: {
async fetchLatestData() {
const endpoint = 'https://housing.ntu.edu.tw/index/get.progress.json';

await fetch(data_endpoint)
await fetch(endpoint)
.then(res => res.json())
.then(data => {
for (const item of data) {
const key = this.groups[item.name];
const name = item.name;

if (this.ignored_groups.includes(key)) {
if (this.ignored_groups.includes(name)) {
continue
};

const group = this.history[key];

group.date.push(today);
group.male.push(item.male);
group.female.push(item.female);
if (name in this.history) {
const group = this.history[name];

group.date.push(this.today);
group.male.push(item.male);
group.female.push(item.female);
} else {
this.history[name] = {
date: [this.today],
male: [item.male],
female: [item.female],
};
}
}
});
}

for (const group_key of Object.values(this.groups)) {
const ctx = document.getElementById(`chart-${ group_key }`);

new Chart(ctx, {
type: 'line',
data: {
labels: this.history[group_key].date,
datasets: [
{
label: '男生',
data: this.history[group_key].male,
},
{
label: '女生',
data: this.history[group_key].female,
}
]
},
options: {
scales: {
x: {
title: {
display: true,
text: '日期',
},
drawCharts() {
for (const [ name, group ] of Object.entries(this.history)) {
const ctx = document.getElementById(`chart-${name}`);

new Chart(ctx, {
type: 'line',
data: {
labels: group.date,
datasets: [
{ label: '男生', data: group.male },
{ label: '女生', data: group.female },
]
},
options: {
scales: {
x: {
title: {
display: true,
text: '日期',
},
},
},
y: {
title: {
display: true,
text: '已補到的候補序號',
y: {
title: {
display: true,
text: '已補到的候補序號',
},
},
},
},
},
});
}
});
}
},
},
}).mount('#app');
</script>
Expand Down

0 comments on commit 653ac54

Please sign in to comment.