-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (55 loc) · 1.84 KB
/
Copy pathscript.js
File metadata and controls
63 lines (55 loc) · 1.84 KB
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
const formPost = document.querySelector("#formPost")
const titlePost = document.querySelector("#titlePost")
const contentPost = document.querySelector("#contentPost")
const btnSubmit = document.querySelector("#btnSubmit")
const titleRender = document.querySelector(".titleRender")
const contentRender = document.querySelector(".contentRender")
formPost.addEventListener("submit", (e) =>{
e.preventDefault()
sendData(comunicationApi(titlePost.value, contentPost.value))
renderPost(titlePost.value, contentPost.value)
validationMsgs(titlePost.value, contentPost.value)
})
function comunicationApi(titlePost, contentPost){
const apiPost = fetch("https://jsonplaceholder.typicode.com/posts", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: titlePost,
body: contentPost,
userId: 1
})
})
return apiPost
}
function sendData(apiPost){
apiPost.then(response =>{
if(!response.ok){
throw Error("Falha na requisição da API")
}
return response.json()
})
.then(data =>{
console.log(data)
})
}
function renderPost(titlePost, contentPost){
titleRender.textContent = titlePost
contentRender.textContent = contentPost
}
function validationMsgs(titlePost, contentPost){
const valitationTitle = document.querySelector(".validationText--title");
const valitationContent = document.querySelector(".validationText--content");
if(titlePost === ""){
valitationTitle.classList.add("showValidation")
}else{
valitationTitle.classList.remove("showValidation")
}
if(contentPost === ""){
valitationContent.classList.add("showValidation")
}else{
valitationContent.classList.remove("showValidation")
}
}