This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
web/js/find-code.js
2023-05-08 21:31:47 +01:00

99 lines
2.9 KiB
JavaScript

hideLoading()
// eslint-disable-next-line no-unused-vars
async function fetchEntry(){ // This can be condensed
showLoading()
let name = document.getElementById('name')
let crs = document.getElementById('3alpha')
let nlc = document.getElementById('nlc')
let tiploc = document.getElementById('tiploc')
let stanox = document.getElementById('stanox')
let values = {
name: name.value,
crs: crs.value,
nlc: nlc.value,
tiploc: tiploc.value,
stanox: stanox.value
}
parseData(values)
}
async function parseData(values){
vibe()
let data
if (values.crs != ''){
setLoadingDesc(`Searching\n${values.crs.toUpperCase()}`)
data = await getData('crs', values.crs)
} else if (values.nlc != ''){
setLoadingDesc(`Searching\n${values.nlc.toUpperCase()}`)
data = await getData('nlc', values.nlc)
} else if (values.tiploc != ''){
setLoadingDesc(`Searching\n${values.tiploc.toUpperCase()}`)
data = await getData('tiploc', values.tiploc)
} else if (values.stanox != ''){
setLoadingDesc(`Searching\n${values.stanox.toUpperCase()}`)
data = await getData('stanox', values.stanox)
} else if (values.name != ''){
setLoadingDesc(`Searching\n${values.name}`)
data = await getData('name', values.name)
} else {
log('find-code.parseData: No data entered', 'WARN')
await clearForm()
document.getElementById('name').value = 'No data entered'
vibe('err')
hideLoading()
return
}
displayData(data)
}
async function getData(type, value){
log(`find-code.getData: Looking for: ${type} '${value}'`, 'INFO')
try {
var url = `${window.location.origin}/api/v1/find/${type}/${value}`
var resp = await fetch(url)
return await resp.json()
} catch (err) {
log(`find-code.getData: Error getting data: ${err}`, 'WARN')
vibe('err')
return ''
}
}
async function displayData(data){
hideLoading()
if (data.status === 'failed' || data == ''){
log('find-code.displayData: Unable to find data', 'WARN')
clearForm()
document.getElementById('name').value = 'Not Found'
} else {
log('find-code.displayData: Inserting data', 'INFO')
vibe('ok')
try {
document.getElementById('name').value = data['0']['NLCDESC']
} catch (err) {log(err)}
try {
document.getElementById('3alpha').value = data['0']['3ALPHA']
} catch (err) {log(err)}
try {
document.getElementById('nlc').value = data['0']['NLC']
} catch (err) {log(err)}
try {
document.getElementById('tiploc').value = data['0']['TIPLOC']
} catch (err) {log(err)}
try {
document.getElementById('stanox').value = data['0']['STANOX']
} catch (err) {log(err)}
}
}
async function clearForm(){
document.getElementById('name').value = ''
document.getElementById('3alpha').value = ''
document.getElementById('nlc').value = ''
document.getElementById('tiploc').value = ''
document.getElementById('stanox').value = ''
vibe('ok')
hideLoading()
}