-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·86 lines (67 loc) · 2.11 KB
/
index.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
const $ = (element) => document.querySelector(element);
let inputElement = $("#input-value");
let changeValueButton = $("#decrement");
let errorMessages = [];
document.getElementById("form-submit").addEventListener("click", (event) => {
event.preventDefault();
if (validate()) {
$("#success-message").innerText = "Formulário enviado com sucesso!";
$("#error-message").innerText = "";
} else {
$("#error-message").innerText = errorMessages.map(
(message) => ` ${message}`
);
$("#success-message").innerText = "";
}
});
changeButtonStatus();
inputElement.addEventListener("change", (element) => {
handleChangeInputValue(element);
});
let inputValue = inputElement.value;
function incrementValue() {
inputValue++;
updateInputValue();
changeButtonStatus(inputValue);
}
function decrementValue() {
if (inputValue > 0) inputValue--;
updateInputValue();
changeButtonStatus(inputValue);
}
const updateInputValue = () => (inputElement.value = inputValue);
const handleChangeInputValue = ({ target }) => {
const { value } = target;
inputValue = value;
changeButtonStatus(inputValue);
};
function changeButtonStatus(value) {
if (!value) {
changeValueButton.setAttribute("disabled", true);
} else {
changeValueButton.removeAttribute("disabled");
}
}
function validate() {
errorMessages = [];
const stickerQuantityIsValid = inputValue > 0;
const hasSomeoneStickerSelected = document.querySelector(
".ckeckbox-sticker:checked"
);
const areThereAnyComments =
document.querySelector(".descricao").value.length > 0;
console.log(areThereAnyComments);
if (!stickerQuantityIsValid) {
inputElement.classList.add("input-error-value");
errorMessages.push("É necessário inserir um número maior que 0");
} else {
inputElement.classList.remove("input-error-value");
}
!hasSomeoneStickerSelected &&
errorMessages.push("É necessário selecionar algum sticker");
!areThereAnyComments &&
errorMessages.push("É necessário fazer alguma observação");
return (
stickerQuantityIsValid && hasSomeoneStickerSelected && areThereAnyComments
);
}