-
Notifications
You must be signed in to change notification settings - Fork 0
/
details.js
129 lines (101 loc) · 3.48 KB
/
details.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
const BASE_URL = "https://fnd22-shared.azurewebsites.net/api/";
const id = new URLSearchParams(window.location.search).get("id");
const changeStatusForm = document.querySelector("#changeStatus");
const addCommentForm = document.querySelector("#addComment");
let todo = {};
const radionbtn1 = document.querySelector("#status1");
const radionbtn2 = document.querySelector("#status2");
const radionbtn3 = document.querySelector("#status3");
const fetchTodo = () => {
fetch(BASE_URL + `cases/${id}`)
.then((res) => res.json())
.then((data) => {
todo = data;
renderTodo(todo);
});
};
const renderTodo = (data) => {
const statusBar = document.querySelector(".statusbar");
statusBar.className = `statusbar status-${data.statusId}`;
const subject = document.querySelector(".details-subject");
subject.textContent = data.subject;
const email = document.querySelector(".details-email");
email.textContent = data.email;
const time = document.querySelector(".details-time");
time.textContent = convertTime(data.created);
const message = document.querySelector(".details-message");
message.textContent = data.message;
changeStatusForm[data.statusId - 1].checked = true;
const commentsContainer = document.querySelector(".comments-container");
commentsContainer.innerHTML = "";
data.comments
.sort((a, b) => {
return a.created.localeCompare(b.created);
})
.forEach((comment) => {
commentsContainer.append(createComment(comment));
});
};
const createComment = (data) => {
const comment = document.createElement("div");
comment.className = "comment";
const email = document.createElement("p");
email.className = "comment-email";
email.textContent = data.email;
const time = document.createElement("p");
time.className = "comment-time";
time.textContent = convertTime(data.created);
const message = document.createElement("p");
message.className = "comment-message";
message.textContent = data.message;
comment.append(email, time, message);
return comment;
};
const convertTime = (time) => {
let newTime = time.slice(0, 16);
newTime = newTime.replace("T", " ");
return newTime;
};
fetchTodo();
const statusBar = document.querySelector(".statusbar");
function checkButton() {
if (radionbtn1.checked) {
statusBar.className = "statusbar status-1";
statusBar.id = 1;
} else if (radionbtn2.checked) {
statusBar.className = "statusbar status-2";
statusBar.id = 2;
} else if (radionbtn3.checked) {
statusBar.className = "statusbar status-3";
statusBar.id = 3;
}
}
radionbtn1.addEventListener("click", checkButton);
radionbtn2.addEventListener("click", checkButton);
radionbtn3.addEventListener("click", checkButton);
changeStatusForm.addEventListener("submit", (e) => {
e.preventDefault();
const statusId = changeStatusForm.querySelector("input:checked").value;
fetch(BASE_URL + `cases/${id}`, {
method: "PUT",
headers: { "Content-type": "application/json; charset=UTF-8" },
body: JSON.stringify({
id: id,
statusId: statusId,
}),
}).then((res) => console.log(res));
});
addCommentForm.addEventListener("submit", (e) => {
e.preventDefault();
fetch(BASE_URL + "comments", {
method: "POST",
headers: { "Content-type": "application/json; charset=UTF-8" },
body: JSON.stringify({
caseId: id,
email: addCommentForm[0].value,
message: addCommentForm[1].value,
}),
})
.then((res) => console.log(res))
.then((data) => fetchTodo(id));
});