-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.html
80 lines (73 loc) · 3.4 KB
/
dashboard.html
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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ホーム</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="dashboard-container">
<header class="dashboard-header">
<h1>ホーム</h1>
</header>
<main class="dashboard-main-content">
<h2>賞味期限が近づいている食品</h2>
<div class="dashboard-food-list">
<ul id="expiringFoodsList">
<!-- 食材リストがここに表示される -->
</ul>
</div>
</main>
<footer class="footer">
<a href="add_food.html" class="button">食品登録</a>
</footer>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
fetch('http://localhost:5000/api/get_expiring_foods')
.then(response => response.json())
.then(data => {
const expiringFoodsList = document.getElementById('expiringFoodsList');
data.forEach(food => {
const listItem = document.createElement('li');
listItem.innerHTML = `
<input type="checkbox" class="foodCheckbox" data-id="${food.id}">
${food.foodName} - 残り${food.daysUntilExpiry}日 (賞味期限: ${food.expiryDate})
`;
expiringFoodsList.appendChild(listItem);
});
document.querySelectorAll('.foodCheckbox').forEach(checkbox => {
checkbox.addEventListener('change', function(event) {
const foodId = event.target.getAttribute('data-id');
if (event.target.checked) {
fetch(`http://localhost:5000/api/delete_food/${foodId}`, {
method: 'DELETE',
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// チェックされた食材を画面から削除
event.target.parentElement.remove();
console.log(data.message);
})
.catch(error => {
console.error('Error occurred while deleting food:', error);
alert('食品削除中にエラーが発生しました。');
});
}
});
});
})
.catch(error => {
console.error('Error occurred while fetching expiring foods:', error);
alert('食品の取得中にエラーが発生しました。');
});
});
</script>
</body>
</html>