-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (90 loc) · 2.99 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
export const fetchEmployees = (currentPage, numberOfRecords) => {
return new Promise( function (resolve, reject) {
console.log("Hello")
var endpoint = 'employee_list' // Default
if (numberOfRecords == "all") {
var data = {
'page_number': currentPage
}
} else {
endpoint = 'employee_search'
const name = document.getElementById('employee').value
var data = {
'employee_name': name
}
}
data = JSON.stringify(data)
var xhr = new XMLHttpRequest();
xhr.open("POST", `http://127.0.0.1:5000/${endpoint}`);
xhr.onload = function () {
const res = JSON.parse(xhr.response)
const statusMsg = res.status ? res.status : res.msg
console.log(xhr.response)
localStorage.setItem('employees', JSON.stringify(res.employee_list ? res.employee_list : [res]))
localStorage.setItem('totalPages', res.total_pages ? res.total_pages : 1)
var statusCode = xhr.status
if (statusCode == 200) {
resolve(statusCode)
} else {
reject(statusMsg)
}
};
var token = localStorage.getItem('token')
token = `Bearer ${token}`
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.setRequestHeader('Authorization', token)
xhr.send(data);
})
}
const addSearchButtonListener = () => {
const search = document.getElementById('search')
if (search) {
search.addEventListener('click', async function() {
try {
var status = await fetchEmployees(1, 'all')
console.log(status)
} catch (error) {
alert(error)
if (error.slice(-7,) == "expired") {
localStorage.clear()
window.location.href = './login.html'
}
} finally {
if (status == 200) {
window.location.href = './employee.html'
}
}
})
}
}
const checkIfLoggedIn = () => {
const token = localStorage.getItem('token')
if (!token) {
window.location.href = './login.html'
}
}
// Main
checkIfLoggedIn()
addSearchButtonListener()
// Response
// {
// "employee_list": [
// {
// "designation": "Manager",
// "employee_name": "SivaGuru"
// },
// {
// "designation": "Manager",
// "employee_name": "Balamurugan"
// },
// {
// "designation": "Manager",
// "employee_name": "Balamurugan_32232"
// },
// {
// "designation": "Manager",
// "employee_name": "Balamurugan_32232_767766"
// }
// ],
// "total_pages": 1
// }