refactor(badges): standardize helpers and apply batch formatting

- Standardized 'prevent_default' helper names across badges module.
- Corrected native 'event.preventDefault()' calls in view and template components.
- Applied batch formatting (printWidth: 80) to all badges files.
This commit is contained in:
Scott Idem
2026-02-06 14:55:43 -05:00
parent 67752ccdfe
commit 5385eacc0f
11 changed files with 648 additions and 290 deletions

View File

@@ -15,10 +15,7 @@
// *** Import Aether specific variables and functions
import { ae_util } from '$lib/ae_utils/ae_utils';
import {
ae_loc,
ae_api
} from '$lib/stores/ae_stores';
import { ae_loc, ae_api } from '$lib/stores/ae_stores';
import { db_events } from '$lib/ae_events/db_events';
import {
@@ -39,13 +36,20 @@
// *** Initialization & Store Guard ***
// Ensure all search fields are initialized to prevent circular undefined triggers
if ($events_loc.badges) {
if (typeof $events_loc.badges.search_version === 'undefined') $events_loc.badges.search_version = 0;
if (typeof $events_loc.badges.qry__remote_first === 'undefined') $events_loc.badges.qry__remote_first = false;
if (typeof $events_loc.badges.fulltext_search_qry_str === 'undefined') $events_loc.badges.fulltext_search_qry_str = '';
if (typeof $events_loc.badges.search_badge_type_code === 'undefined') $events_loc.badges.search_badge_type_code = '';
if (typeof $events_loc.badges.qry_printed_status === 'undefined') $events_loc.badges.qry_printed_status = 'all';
if (typeof $events_loc.badges.qry_affiliations === 'undefined') $events_loc.badges.qry_affiliations = '';
if (typeof $events_loc.badges.qry_sort_order === 'undefined') $events_loc.badges.qry_sort_order = '';
if (typeof $events_loc.badges.search_version === 'undefined')
$events_loc.badges.search_version = 0;
if (typeof $events_loc.badges.qry__remote_first === 'undefined')
$events_loc.badges.qry__remote_first = false;
if (typeof $events_loc.badges.fulltext_search_qry_str === 'undefined')
$events_loc.badges.fulltext_search_qry_str = '';
if (typeof $events_loc.badges.search_badge_type_code === 'undefined')
$events_loc.badges.search_badge_type_code = '';
if (typeof $events_loc.badges.qry_printed_status === 'undefined')
$events_loc.badges.qry_printed_status = 'all';
if (typeof $events_loc.badges.qry_affiliations === 'undefined')
$events_loc.badges.qry_affiliations = '';
if (typeof $events_loc.badges.qry_sort_order === 'undefined')
$events_loc.badges.qry_sort_order = '';
}
// Variables
@@ -61,18 +65,28 @@
let lq__event_badge_obj_li = $derived.by(() => {
const ids = event_badge_id_li;
const event_id = $events_slct?.event_id;
return liveQuery(async () => {
// SCENARIO 1: Specific IDs provided (Search Results)
if (Array.isArray(ids) && ids.length > 0) {
if (log_lvl) console.log(`Badge Page LQ: bulkGet ${ids.length} IDs`);
if (log_lvl)
console.log(`Badge Page LQ: bulkGet ${ids.length} IDs`);
const results = await db_events.badge.bulkGet(ids);
return results.filter(item => item !== undefined);
return results.filter((item) => item !== undefined);
}
// SCENARIO 2: Fallback broad search (Only if no active filters)
if (event_id && !$events_loc.badges.fulltext_search_qry_str && $events_loc.badges.qry_printed_status === 'all' && !$events_loc.badges.qry_affiliations && !$events_loc.badges.search_badge_type_code) {
if (log_lvl) console.log(`Badge Page LQ: Fallback search for event: ${event_id}`);
if (
event_id &&
!$events_loc.badges.fulltext_search_qry_str &&
$events_loc.badges.qry_printed_status === 'all' &&
!$events_loc.badges.qry_affiliations &&
!$events_loc.badges.search_badge_type_code
) {
if (log_lvl)
console.log(
`Badge Page LQ: Fallback search for event: ${event_id}`
);
return await db_events.badge
.where('event_id_random')
.equals(event_id)
@@ -88,7 +102,9 @@
// 1. Isolate dependencies into a stable derived object
let search_params = $derived({
v: $events_loc.badges.search_version,
str: ($events_loc.badges.fulltext_search_qry_str ?? '').toLowerCase().trim(),
str: ($events_loc.badges.fulltext_search_qry_str ?? '')
.toLowerCase()
.trim(),
type: $events_loc.badges.search_badge_type_code,
printed: $events_loc.badges.qry_printed_status,
aff: ($events_loc.badges.qry_affiliations ?? '').toLowerCase().trim(),
@@ -122,9 +138,12 @@
const current_search_id = ++last_search_id;
const event_id = params.event_id;
const remote_first = params.remote_first;
if (log_lvl) console.log(`[Badge Search #${current_search_id}] Refreshing (remote=${remote_first}, event=${event_id}, str=${params.str})...`);
if (log_lvl)
console.log(
`[Badge Search #${current_search_id}] Refreshing (remote=${remote_first}, event=${event_id}, str=${params.str})...`
);
untrack(() => {
$events_sess.badges.search_status = 'loading';
$events_sess.badges.search_complete = false;
@@ -142,36 +161,56 @@
let local_results = await db_events.badge
.where('event_id_random')
.equals(event_id)
.filter(badge => {
if (type_code && badge.badge_type_code !== type_code) return false;
.filter((badge) => {
if (
type_code &&
badge.badge_type_code !== type_code
)
return false;
if (printed_status !== 'all') {
const is_printed = (badge.print_count ?? 0) > 0;
if (printed_status === 'printed' && !is_printed) return false;
if (printed_status === 'not_printed' && is_printed) return false;
if (printed_status === 'printed' && !is_printed)
return false;
if (
printed_status === 'not_printed' &&
is_printed
)
return false;
}
if (qry_str) {
const given_name = (badge.given_name ?? '').toLowerCase();
const family_name = (badge.family_name ?? '').toLowerCase();
const full_name = `${given_name} ${family_name}`.toLowerCase();
const given_name = (
badge.given_name ?? ''
).toLowerCase();
const family_name = (
badge.family_name ?? ''
).toLowerCase();
const full_name =
`${given_name} ${family_name}`.toLowerCase();
const email = (badge.email ?? '').toLowerCase();
const qry_string = (badge.default_qry_str ?? '').toLowerCase();
const match = full_name.includes(qry_str) ||
given_name.includes(qry_str) ||
family_name.includes(qry_str) ||
email.includes(qry_str) ||
qry_string.includes(qry_str);
const qry_string = (
badge.default_qry_str ?? ''
).toLowerCase();
const match =
full_name.includes(qry_str) ||
given_name.includes(qry_str) ||
family_name.includes(qry_str) ||
email.includes(qry_str) ||
qry_string.includes(qry_str);
if (!match) return false;
}
if (aff_str) {
const affiliations = (badge.affiliations ?? '').toLowerCase();
if (!affiliations.includes(aff_str)) return false;
const affiliations = (
badge.affiliations ?? ''
).toLowerCase();
if (!affiliations.includes(aff_str))
return false;
}
return true;
})
.toArray();
@@ -180,27 +219,57 @@
local_results.sort((a, b) => {
switch (params.sort) {
case 'name_asc':
return (a.given_name ?? '').localeCompare(b.given_name ?? '') || (a.family_name ?? '').localeCompare(b.family_name ?? '');
return (
(a.given_name ?? '').localeCompare(
b.given_name ?? ''
) ||
(a.family_name ?? '').localeCompare(
b.family_name ?? ''
)
);
case 'name_desc':
return (b.given_name ?? '').localeCompare(a.given_name ?? '') || (b.family_name ?? '').localeCompare(a.family_name ?? '');
return (
(b.given_name ?? '').localeCompare(
a.given_name ?? ''
) ||
(b.family_name ?? '').localeCompare(
a.family_name ?? ''
)
);
case 'updated_desc':
return new Date(b.updated_on || 0).getTime() - new Date(a.updated_on || 0).getTime();
return (
new Date(b.updated_on || 0).getTime() -
new Date(a.updated_on || 0).getTime()
);
case 'updated_asc':
return new Date(a.updated_on || 0).getTime() - new Date(b.updated_on || 0).getTime();
return (
new Date(a.updated_on || 0).getTime() -
new Date(b.updated_on || 0).getTime()
);
case 'print_count_desc':
return (b.print_count ?? 0) - (a.print_count ?? 0);
return (
(b.print_count ?? 0) - (a.print_count ?? 0)
);
default:
return (a.given_name ?? '').localeCompare(b.given_name ?? '');
return (a.given_name ?? '').localeCompare(
b.given_name ?? ''
);
}
});
const local_ids = local_results.map(b => b.id || b.event_badge_id_random).filter(Boolean);
const local_ids = local_results
.map((b) => b.id || b.event_badge_id_random)
.filter(Boolean);
if (current_search_id === last_search_id) {
if (log_lvl) console.log(`[Badge Search #${current_search_id}] Fast Path found ${local_ids.length} items locally.`);
if (log_lvl)
console.log(
`[Badge Search #${current_search_id}] Fast Path found ${local_ids.length} items locally.`
);
untrack(() => {
event_badge_id_li = local_ids;
if (local_ids.length > 0) $events_sess.badges.search_status = 'done';
if (local_ids.length > 0)
$events_sess.badges.search_status = 'done';
});
}
}
@@ -218,12 +287,23 @@
// Map sort param to API order_by_li
let order_by_li: any = {};
switch (params.sort) {
case 'name_asc': order_by_li = { given_name: 'ASC', family_name: 'ASC' }; break;
case 'name_desc': order_by_li = { given_name: 'DESC', family_name: 'DESC' }; break;
case 'updated_desc': order_by_li = { updated_on: 'DESC' }; break;
case 'updated_asc': order_by_li = { updated_on: 'ASC' }; break;
case 'print_count_desc': order_by_li = { print_count: 'DESC' }; break;
default: order_by_li = { given_name: 'ASC' };
case 'name_asc':
order_by_li = { given_name: 'ASC', family_name: 'ASC' };
break;
case 'name_desc':
order_by_li = { given_name: 'DESC', family_name: 'DESC' };
break;
case 'updated_desc':
order_by_li = { updated_on: 'DESC' };
break;
case 'updated_asc':
order_by_li = { updated_on: 'ASC' };
break;
case 'print_count_desc':
order_by_li = { print_count: 'DESC' };
break;
default:
order_by_li = { given_name: 'ASC' };
}
const results = await events_func.search__event_badge({
@@ -240,15 +320,20 @@
if (current_search_id === last_search_id) {
const api_results = results || [];
const api_ids = api_results.map((b: any) => b.id || b.event_badge_id_random).filter(Boolean);
const api_ids = api_results
.map((b: any) => b.id || b.event_badge_id_random)
.filter(Boolean);
untrack(() => {
$events_sess.badge_li = api_results;
event_badge_id_li = api_ids;
$events_sess.badges.search_status = 'done';
$events_sess.badges.search_complete = true;
});
if (log_lvl) console.log(`[Badge Search #${current_search_id}] Revalidation Complete. Found ${api_ids.length} items.`);
if (log_lvl)
console.log(
`[Badge Search #${current_search_id}] Revalidation Complete. Found ${api_ids.length} items.`
);
}
} catch (error) {
if (current_search_id === last_search_id) {
@@ -265,24 +350,24 @@
<svelte:head>
<title>
Badges -
{ae_util.shorten_string({ string: $events_slct?.event_obj?.name ?? '-- not set --', max_length: 12 })}
{ae_util.shorten_string({
string: $events_slct?.event_obj?.name ?? '-- not set --',
max_length: 12
})}
- OSIT's &AElig; Events
</title>
</svelte:head>
<Comp_badge_search
event_id={$events_slct?.event_id ?? ''}
log_lvl={1}
<Comp_badge_search event_id={$events_slct?.event_id ?? ''} log_lvl={1}
></Comp_badge_search>
{#if $events_sess?.badges?.search_status === 'loading' && event_badge_id_li.length === 0}
<div class="flex flex-col items-center justify-center p-10 opacity-50 text-center">
<div
class="flex flex-col items-center justify-center p-10 opacity-50 text-center"
>
<LoaderCircle size="3em" class="animate-spin mb-4 mx-auto" />
<p class="text-xl">Loading badges...</p>
</div>
{:else}
<Comp_badge_obj_li
lq__event_badge_obj_li={lq__event_badge_obj_li}
log_lvl={1}
></Comp_badge_obj_li>
{/if}
<Comp_badge_obj_li {lq__event_badge_obj_li} log_lvl={1}></Comp_badge_obj_li>
{/if}