-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
75 lines (65 loc) · 2.71 KB
/
main.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
window.addEventListener("load", function () {
const url = "https://label-predictor.herokuapp.com/api";
// const url = "http://127.0.0.1:5000";
const myHeaders = new Headers({
'Access-Control-Allow-Origin': '*',
'content-type': 'application/json'
});
let fecthingData = false;
const issueTitleField = document.getElementById("issue-title");
const issueBodyField = document.getElementById("issue-body");
const invalidIssueBody = document.getElementById("invalid-issue-body");
const requestError = document.getElementById("request-error");
const submitButton = document.getElementById("predictlabel");
const progressSpinner = document.getElementById("progress-spinner");
const predictedLabel = document.getElementById("predicted-label");
const dataBeforePreProcessing = document.getElementById("data-before-preprocessing");
const processedData = document.getElementById("data-after-preprocessing");
submitButton.addEventListener("click", function () {
let issueTitle = issueTitleField.value;
let issueBody = issueBodyField.value;
let requestData = {'title': issueTitle, 'body': issueBody};
dataBeforePreProcessing.innerHTML = issueTitle + " " + issueBody;
if (issueBody) {
progressSpinner.classList.toggle('display-none');
fetch(`${url}/prediction`, {
method: "POST",
body: JSON.stringify(requestData),
mode: 'cors',
headers: myHeaders
})
.then((res) => res.json())
.then((data) => {
fecthingData = true;
if (fecthingData) {
progressSpinner.classList.toggle('display-none');
fecthingData = false;
}
let prediction = data.predictedLabel.trim();
if (prediction === "bug") {
predictedLabel.innerHTML = `🐞 ${prediction}`;
predictedLabel.style.backgroundColor = "#F87171";
} else if (prediction === "enhancement") {
predictedLabel.innerHTML = `💡 ${prediction}`;
predictedLabel.style.backgroundColor = "#a2eeef"
} else if (prediction === "question") {
predictedLabel.innerHTML = `❔ ${prediction}`;
predictedLabel.style.backgroundColor = "#d876e3"
}
processedData.innerHTML = data.processedData;
})
.catch((error) => {
console.log(error);
requestError.classList.toggle("invisible");
})
}
if (issueBody) {
issueBodyField.classList.remove("border-red-500");
invalidIssueBody.classList.add("display-none");
} else {
console.log('Issue body can not empty!');
issueBodyField.classList.add("border-red-500");
invalidIssueBody.classList.remove("display-none");
}
});
});