Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
quite committed Aug 26, 2024
1 parent 71536a9 commit 45891e8
Showing 1 changed file with 45 additions and 47 deletions.
92 changes: 45 additions & 47 deletions tmpl_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,62 +106,62 @@
// Replaces the HTML generated when fruktsam was run
async function loadFlagged() {
document.getElementById('flagged').innerHTML = 'Laddar från databasen...'
let html = ''
const flags = await http('GET', `${apiBase}/flags`)

switch (true) {
case 'error' in flags:
html += `fel: ${JSON.stringify(flags)}`
break;

case flags.length == 0:
html += `Inga flaggade träd`
break;

default:
flags.sort((a, b) => {
if (a.flagged_at == b.flagged_at) {
return 0
}
if (a.flagged_at < b.flagged_at) {
// Sort a after b
return 1
}
return -1
})
for (const flagged of flags) {
const tree = await http('GET', `${apiBase}/tree/${flagged.tree}`)
if ('error' in tree) {
html += `<p class="flagged">fel vid hämtning av träd '${JSON.stringify(flagged.tree)}':\n\n${tree.error}</p>`
continue
}
const flaggedAt = formatDate(flagged.flagged_at)
const lastChange = formatDate(tree.added)
let photoHtml = ''
if (tree.file !== '') {
photoHtml = `
if ('error' in flags) {
document.getElementById('flagged').innerHTML = `fel: ${JSON.stringify(flags)}`
return
}

if (flags.length == 0) {
document.getElementById('flagged').innerHTML = `Inga flaggade träd`
return
}

let html = ''
flags.sort((a, b) => {
if (a.flagged_at == b.flagged_at) {
return 0
}
if (a.flagged_at < b.flagged_at) {
// Sort a after b
return 1
}
return -1
})
for (const flagged of flags) {
const tree = await http('GET', `${apiBase}/tree/${flagged.tree}`)
if ('error' in tree) {
html += `<p class="flagged">fel vid hämtning av träd '${JSON.stringify(flagged.tree)}':\n\n${tree.error}</p>`
continue
}
const flaggedAt = formatDate(flagged.flagged_at)
const lastChange = formatDate(tree.added)
let photoHtml = ''
if (tree.file !== '') {
photoHtml = `
<br/>
<span class="photo flagged">
<a href="https://fruktkartan-thumbs.s3.eu-north-1.amazonaws.com/${tree.file}_1200.jpg" target="_blank" rel="noopener">
<img src="https://fruktkartan-thumbs.s3.eu-north-1.amazonaws.com/${tree.file}_1200.jpg" width="130px" />
</a>
</span>
`
}
}
// If we have more than just a few flagged trees, then fetching this
// is too slow/hangs.
// var reverseURL = new URL('https://nominatim.openstreetmap.org/reverse')
// reverseURL.searchParams.set('lat', tree.lat)
// reverseURL.searchParams.set('lon', tree.lon)
// reverseURL.searchParams.set('format', 'json')
// const reverse = await http('GET', reverseURL.href, { 'Accept-Language': 'sv,en-US,en' })
// if (('error' in reverse) || (!('display_name' in reverse))) {
// html += `<p class="flagged">fel vid hämtning av nominatim-reverse för träd '${JSON.stringify(flagged.tree)}':\n\n${reverse.error}</p>`
// continue
// }
// — ${reverse.display_name}
// <br/>
html += `
// var reverseURL = new URL('https://nominatim.openstreetmap.org/reverse')
// reverseURL.searchParams.set('lat', tree.lat)
// reverseURL.searchParams.set('lon', tree.lon)
// reverseURL.searchParams.set('format', 'json')
// const reverse = await http('GET', reverseURL.href, { 'Accept-Language': 'sv,en-US,en' })
// if (('error' in reverse) || (!('display_name' in reverse))) {
// html += `<p class="flagged">fel vid hämtning av nominatim-reverse för träd '${JSON.stringify(flagged.tree)}':\n\n${reverse.error}</p>`
// continue
// }
// — ${reverse.display_name}
// <br/>
html += `
<p class="flagged">
<span class="flagtime">${flaggedAt}</span>
<span class="type">
Expand All @@ -184,8 +184,6 @@
</span>
</p>
`
}
break;
}

document.getElementById('flagged').innerHTML = html
Expand Down

0 comments on commit 45891e8

Please sign in to comment.