-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.js
51 lines (45 loc) · 1.98 KB
/
forms.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
import { addCard } from "./cards.js";
import { loadData } from "./weather.js";
export const initForm = () => {
const datePicker = document.getElementById('date-input');
// const form = document.getElementById('form');
// minimum dátum a mai nap
const cardsContainer = document.getElementById('cards-container');
const errorMessage = document.getElementById('error-message');
const submitButton = document.getElementById('submit');
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
datePicker.min = `${year}-${month < 10 ? `0${month}` : month}-${day < 10 ? `0${day}` : day}`;
// maximum dátum a mai + 5 nap
const maxDate = now;
maxDate.setDate(now.getDate() + 5);
const maxDateYear = maxDate.getFullYear();
const maxDateMonth = maxDate.getMonth() + 1;
const maxDateDay = maxDate.getDate();
datePicker.max = `${maxDateYear}-${maxDateMonth < 10 ? `0${maxDateMonth}` : maxDateMonth}-${maxDateDay < 10 ? `0${maxDateDay}` : maxDateDay}`;
console.log(datePicker.max);
console.log(datePicker.min);
form.addEventListener('submit', async e => {
const city = document.getElementById('city-input').value;
const date = document.getElementById('date-input').value;
console.log(city, date)
// loading start
cardsContainer.insertAdjacentHTML('afterbegin', ` <div class="loader" id="loading-indicator"</div>`)
submitButton.disabled = true;
e.preventDefault();
try {
const weatherData = await loadData(city, date);
addCard(city, date, weatherData);
form.reset();
}
catch {
errorMessage.style.display = 'block';
setTimeout(() => errorMessage.style.display = 'none', 2000);
}
submitButton.disabled = false;
// loading stop
cardsContainer.removeChild(document.getElementById('loading-indicator'));
})
}