-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
157 lines (132 loc) · 3.56 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
import snackbar from 'snackbar'
import { renderChart } from './chart.js'
import {
FetchWrapper,
calculateCalories,
capitalize,
formatDecimal,
formatGrams,
} from './helpers.js'
import { Store } from './store.js'
const api = new FetchWrapper(import.meta.env.VITE_API_URL)
const store = new Store()
const form = document.querySelector('#create-form')
const nameSelect = form?.querySelector('#create-name')
const carbsInput = form?.querySelector('#create-carbs')
const proteinInput = form?.querySelector('#create-protein')
const fatInput = form?.querySelector('#create-fat')
const submitButton = form?.querySelector('input[type="submit"]')
const list = document.querySelector('#food-list')
const totalCalories = document.querySelector('#total-calories')
form?.addEventListener('submit', async (event) => {
event.preventDefault()
submitButton?.setAttribute('disabled', 'disabled')
try {
const data = await addFood({
name: nameSelect?.value,
carbs: carbsInput?.value,
protein: proteinInput?.value,
fat: fatInput?.value,
})
if (data.error) {
return snackbar.show('Some data is missing.')
}
snackbar.show('Food added successfully.')
const { name, carbs, protein, fat } = data.fields
displayEntry({
name: name.stringValue,
carbs: carbs.integerValue,
protein: protein.integerValue,
fat: fat.integerValue,
})
render()
// Reset form fields
resetForm(event)
} catch (error) {
console.error(error)
} finally {
submitButton?.removeAttribute('disabled')
}
})
function resetForm() {
form?.reset()
nameSelect?.focus()
}
async function addFood(food) {
const object = {
fields: {
name: { stringValue: food.name },
carbs: { integerValue: food.carbs },
protein: { integerValue: food.protein },
fat: { integerValue: food.fat },
},
}
const data = await api.post('/', object)
return data
}
function updateTotalCalories() {
if (!totalCalories) {
return
}
totalCalories.textContent = formatDecimal(store.totalCalories)
}
function displayEntry({ name, carbs, protein, fat }) {
store.addFood({ carbs, protein, fat })
const title = capitalize(name)
const totalCalories = calculateCalories({ carbs, protein, fat })
list.insertAdjacentHTML(
'beforeend',
`<li class="card">
<div>
<h3 class="name">
${title}
</h3>
<div class="calories">
${formatDecimal(totalCalories)} calories
</div>
<ul class="macros">
<li class="carbs">
<div>Carbs</div>
<div class="value">
${formatGrams(carbs)}
</div>
</li>
<li class="protein">
<div>Protein</div>
<div class="value">
${formatGrams(protein)}
</div>
</li>
<li class="fat">
<div>Fat</div>
<div class="value">
${formatGrams(fat)}
</div>
</li>
</ul>
</div>
</li>`,
)
}
function render() {
renderChart(store)
updateTotalCalories()
}
async function init() {
// TODO: Get the saved entries and list them
const data = await api.get('/?pageSize=100')
if (!data.documents) {
return
}
for (const doc of data.documents) {
const { name, carbs, protein, fat } = doc.fields
displayEntry({
name: name.stringValue,
carbs: carbs.integerValue,
protein: protein.integerValue,
fat: fat.integerValue,
})
}
render()
}
init()