-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.js
60 lines (60 loc) · 2.19 KB
/
modal.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
document.addEventListener('DOMContentLoaded',()=>{
const modal = document.querySelector('.js-modal'),
openModalBtn=document.querySelector('.js-show-modal'),
closedModalBtns=document.querySelectorAll('.js-close-modal'),
addButton=document.getElementById('btn'),
inputValue=document.querySelectorAll('.input'),
body=document.querySelector('body');
var payload={}
const closeModal=()=>{
modal.classList.remove('modal--opened');
body.classList.remove('overflowHidden');
document.removeEventListener('keydown',handleEscClose);
}
const openModal=()=>{
modal.classList.add('modal--opened');
body.classList.add('overflowHidden');
document.addEventListener('keydown',handleEscClose)
}
const handleEscClose=(event)=>{
if(event.key === 'Escape'){
closeModal();
}
};
const handleChange=(e)=>{
payload[e.target.name]=e.target.value;
}
const handleSubmit = ()=>{
console.log(payload)
if(JSON.stringify(payload) === "{}"){
alert("Form cannot be empty")
}else{
fetch("http://localhost:5000/product",{
method:"POST",
body:JSON.stringify(payload),
headers:{
"Content-type":"application/json; charset=UTF-8"
}
})
.then(res=>res.json())
.then(response=> console.log(response))
.catch(error => console.error("Error:", error));
closeModal()
}
}
if(modal){
openModalBtn.addEventListener('click', openModal);
inputValue.forEach((input)=>input.addEventListener('input',handleChange));
addButton.addEventListener('click',handleSubmit)
closedModalBtns.forEach((btn)=>btn.addEventListener('click',closeModal));
}
// document.addEventListener('click',(event)=>{
// if(
// modal.classList.contains('modal--opened') &&
// !event.target.closest('.modal__content') &&
// !event.target.closest('.js-show-modal')
// ){
// closeModal()
// }
// })
})