-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskript.js
145 lines (132 loc) · 5.96 KB
/
skript.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
let schoolname = "아름고등학교";
let atptCode = "";
let sdSchulCode = "";
const urlbase = 'https://gubsicmenu.overjjang99.workers.dev/api';
let isReturing = "";
let date = new Date(new Date().getTime() + 9 * 60 * 60 * 1000).toISOString().slice(0, 10).replace(/-/g, '');
console.log(date);
const container = document.getElementById('container');
const title = document.getElementById('title');
const menu = document.getElementById('menuList');
const otherSchool = document.getElementById('otherschoolcheak');
const schoollist = document.getElementById('schoolList');
function setDate(){
document.getElementById('dateinput').value = new Date().toISOString().slice(0, 10);
}
setDate()
function schoolNameInput(fixedSchoolName = '') {
const input = document.getElementById('schoolnameinput').value;
const dateinput = document.getElementById('dateinput').value.replace(/-/g, '');
console.log(dateinput);
title.classList.remove('error');
date = dateinput ? dateinput : date;
title.innerHTML = `오늘의 급식은?`;
container.style.visibility = `hidden`;
otherSchool.style.visibility = `hidden`;
otherSchool.style.display = `none`;
schoolname = fixedSchoolName ? fixedSchoolName : (input ? input : schoolname);
main()
}
async function getSchoolInfo(schoolName) {
let apiUrl = `${urlbase}?mode=name&schoolName=${schoolName}`;
listLabel = document.getElementById('listLabel');
const schoolSelect = document.getElementById('schoolSelect');
try {
let response = await fetch(apiUrl);
let data = await response.json();
console.log(data);
//학교 정보가 있을 경우 atptCode, sdSchulCode를 저장
if(data.schoolInfo) {
if (data.schoolInfo[0].head[0].list_total_count === 1) {
atptCode = data.schoolInfo[1].row[0].ATPT_OFCDC_SC_CODE;
sdSchulCode = data.schoolInfo[1].row[0].SD_SCHUL_CODE;
isReturing = data.schoolInfo[0].head[1].RESULT.CODE;
}
else{
listLabel.innerHTML = `학교 이름이 ${schoolName}인 학교가 ${data.schoolInfo[0].head[0].list_total_count}개 입니다. 선택해주세요.`;
schoolSelect.innerHTML = "";
schoolSelect.innerHTML += `<option value="">학교를 선택해주세요</option>`;
for(let i = 0; i < data.schoolInfo[0].head[0].list_total_count; i++) {
schoolSelect.innerHTML += `<option value="${data.schoolInfo[1].row[i].ATPT_OFCDC_SC_CODE},${data.schoolInfo[1].row[i].SD_SCHUL_CODE}">${data.schoolInfo[1].row[i].SCHUL_NM}(${data.schoolInfo[1].row[i].ATPT_OFCDC_SC_NM})</option>`;
}
schoollist.style.visibility = `visible`;
schoollist.style.display = `inline-block`;
isReturing="needSelect";
}
}
else isReturing = data.RESULT.CODE;
console.log(atptCode, sdSchulCode);
} catch (error) {
console.log(error);
container.style.visibility = `hidden`;
title.innerHTML = `해당 학교를 찾을 수 없습니다`;
menu.innerHTML = "";
}
}
function getMealInfo(inAtptCode, inSdSchulCode) {
console.log(inAtptCode, inSdSchulCode);
let apiUrl = `${urlbase}?mode=menu&atptCode=${inAtptCode}&schoolCode=${inSdSchulCode}&date=${date}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
console.log(data);
title.innerHTML = `${data.mealServiceDietInfo[1].row[0].SCHUL_NM}의<br>`
if (date === new Date().toISOString().slice(0, 10).replace(/-/g, '')) {
title.innerHTML += `오늘의 급식은?`;
} else {
title.innerHTML += `${date.slice(0, 4)}년 ${date.slice(4, 6)}월 ${date.slice(6, 8)}일 급식은?`;
}
if (data.mealServiceDietInfo) {
menu.innerHTML = "";
for (let i = 0; i < data.mealServiceDietInfo[1].row.length; i++) {
menu.innerHTML += `<h3 class="fs-3 fw-bold" align="center">${data.mealServiceDietInfo[1].row[i].MMEAL_SC_NM}</h3>${data.mealServiceDietInfo[1].row[i].DDISH_NM}`;
}
otherSchool.style.visibility = `visible`;
otherSchool.style.display = `inline-block`;
container.style.visibility = `visible`;
container.style.display = `inline-block`;
schoollist.style.visibility = `hidden`;
schoollist.style.display = `none`;
title.classList.remove('error');
} else{
title.classList.add('error');
console.log(apiUrl);
otherSchool.style.visibility = `visible`;
otherSchool.style.display = `inline-block`;
title.innerHTML = `급식 정보가 없습니다.`;
menu.innerHTML = "";
}
})
.catch(error => {
title.classList.add('error');
console.log(error);
console.log(apiUrl);
otherSchool.style.visibility = `visible`;
otherSchool.style.display = `inline-block`;
title.innerHTML = `급식 정보가 없습니다.`;
menu.innerHTML = "";
});
}
async function main() {
await getSchoolInfo(schoolname);
console.log(isReturing);
if(isReturing === "INFO-200") {
otherSchool.style.visibility = `visible`;
otherSchool.style.display = `inline-block`;
console.log("school not found");
container.style.visibility = `hidden`;
title.innerHTML = `해당 학교를 찾을 수 없습니다`;
menu.innerHTML = "";
title.classList.add('error');
}
else if(isReturing === "needSelect") {
console.log("need select");
title.innerHTML = `중복되는 학교명이 있습니다. 선택해주세요.`;
container.style.visibility = `hidden`;
container.style.display = `none`;
}
else {
getMealInfo(atptCode, sdSchulCode);
}
}
main();