Search lots and devices

This commit is contained in:
RubenPX 2022-04-11 11:44:48 +00:00
parent 63d99998a2
commit 756c7bd1c4
2 changed files with 574 additions and 409 deletions

View File

@ -180,3 +180,145 @@ function export_file(type_file) {
$("#exportAlertModal").click();
}
}
window.addEventListener("DOMContentLoaded", () => {
var searchForm = document.getElementById("SearchForm")
var inputSearch = document.querySelector("#SearchForm > input")
var doSearch = true
const Api = {
/**
* get lots id
* @returns get lots
*/
async get_lots() {
var request = await this.doRequest(API_URLS.lots, "GET", null)
if (request != undefined) return request.items
throw request
},
/**
* Get filtered devices info
* @param {number[]} ids devices ids
* @returns full detailed device list
*/
async get_devices(id) {
var request = await this.doRequest(API_URLS.devices + '?filter={"devicehub_id": ["' + id + '"]}', "GET", null)
if (request != undefined) return request.items
throw request
},
/**
*
* @param {string} url URL to be requested
* @param {String} type Action type
* @param {String | Object} body body content
* @returns {Object[]}
*/
async doRequest(url, type, body) {
var result;
try {
result = await $.ajax({
url: url,
type: type,
headers: {
"Authorization": API_URLS.Auth_Token
},
body: body
});
return result
} catch (error) {
console.error(error)
throw error
}
}
}
searchForm.addEventListener("submit", (event) => {
event.preventDefault();
})
var timeoutHandler = setTimeout(() => { }, 1000)
var dropdownList = document.getElementById("dropdown-search-list")
inputSearch.addEventListener("input", (e) => {
console.log("Timeout restart")
clearTimeout(timeoutHandler)
timeoutHandler = setTimeout(async () => {
console.log("timeout start")
dropdownList.innerHTML = `
<li id="deviceSearchLoader" class="dropdown-item">
<i class="bi bi-laptop"></i>
<div class="spinner-border spinner-border-sm" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</li>
<li id="lotSearchLoader" class="dropdown-item">
<i class="bi bi-folder2"></i>
<div class="spinner-border spinner-border-sm" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</li>
`
var searchText = e.target.value
try {
Api.get_devices(searchText.toUpperCase()).then(devices => {
dropdownList.querySelector("#deviceSearchLoader").style = "display: none"
for (let i = 0; i < devices.length; i++) {
const device = devices[i];
// See: ereuse_devicehub/resources/device/models.py
var verboseName = `${device.type} ${device.manufacturer} ${device.model}`
const templateString = `
<li>
<a class="dropdown-item" href="${API_URLS.devices_detail.replace("ReplaceTEXT", device.devicehubID)}" style="display: flex; align-items: center;" href="#">
<i class="bi bi-laptop"></i>
<span style="margin-right: 10px">${verboseName}</span>
<span class="badge bg-secondary" style="margin-left: auto;">${device.devicehubID}</span>
</a>
</li>`
dropdownList.innerHTML += templateString
if (i == 4) { // Limit to 4 resullts
break;
}
}
})
} catch (error) {
console.log(error)
}
try {
Api.get_lots().then(lots => {
dropdownList.querySelector("#lotSearchLoader").style = "display: none"
for (let i = 0; i < lots.length; i++) {
const lot = lots[i];
if (lot.name.toUpperCase().includes(searchText.toUpperCase())) {
const templateString = `
<li>
<a class="dropdown-item" href="${API_URLS.lots_detail.replace("ReplaceTEXT", lot.id)}" style="display: flex; align-items: center;" href="#">
<i class="bi bi-folder2"></i>
<span style="margin-right: 10px">${lot.name}</span>
</a>
</li>`
dropdownList.innerHTML += templateString
if (i == 4) {
break;
}
}
}
})
} catch (error) {
console.log(error)
}
}, 1000)
})
})

View File

@ -12,9 +12,20 @@
</div><!-- End Logo -->
<div class="search-bar">
<form class="search-form d-flex align-items-center" method="POST" action="#">
<input type="text" name="query" placeholder="Search" title="Enter search keyword">
<form class="search-form d-flex align-items-center" method="POST" id="SearchForm" action="#">
<input class="dropdown-toggle" type="text" name="query" placeholder="Search" title="Enter search keyword" autocomplete="off" id="dropdownSearch" data-bs-toggle="dropdown" aria-expanded="false">
<button type="submit" title="Search"><i class="bi bi-search"></i></button>
<ul class="dropdown-menu" autoClose="outside" aria-labelledby="dropdownSearch" id="dropdown-search-list" style="min-width: 100px;">
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i><span style="margin-right: 10px">This is a extra large device name to test long titles</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i> <span>Device Name 2</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i> <span>Device Name 3</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-laptop"></i> <span>Device Name 4</span> <span class="badge bg-secondary" style="margin-left: auto;">{DHID ID}</span></a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 1</a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 2</a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 3</a></li>
<li><a class="dropdown-item" style="display: flex; align-items: center;" href="#"><i class="bi bi-folder2"></i> lot 4</a></li>
</ul>
</form>
</div><!-- End Search Bar -->
@ -224,4 +235,16 @@
</div>
</footer><!-- End Footer -->
<!-- API_CALLS -->
<script>
const API_URLS = {
Auth_Token: `Basic ${btoa("{{ current_user.token }}:")}`, //
currentUserID: "{{ current_user.id }}",
lots: "{{ url_for('Lot.main') }}",
lots_detail: "{{ url_for('inventory.lotdevicelist', lot_id='ReplaceTEXT') }}",
devices: "{{ url_for('Device.main') }}",
devices_detail: "{{ url_for('inventory.device_details', id='ReplaceTEXT')}}"
}
</script>
{% endblock body %}