feat(leads): implement reactive search for exhibitors and lead tracking
- Implemented V3-style reactive search (Local Cache -> Remote Revalidation) for exhibitors. - Standardized search fields to 'name' for Exhibits and 'event_badge_full_name' for Lead Tracking. - Refactored Leads UI with standardized search components and grid layout. - Updated event routing to exclusively use string-based IDs (Triple-ID pattern). - Hardened 'ae_EventSession' type definitions to handle null values from V3 API/Dexie.
This commit is contained in:
@@ -1,32 +1,193 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { onMount, untrack } from 'svelte';
|
||||
import { liveQuery } from 'dexie';
|
||||
import { db_events } from '$lib/ae_events/db_events';
|
||||
import { events_slct } from '$lib/stores/ae_events_stores';
|
||||
import { events_loc, events_sess, events_slct } from '$lib/stores/ae_events_stores';
|
||||
import { ae_api, ae_loc } from '$lib/stores/ae_stores';
|
||||
import { page } from '$app/state';
|
||||
import { events_func } from '$lib/ae_events_functions';
|
||||
import { LoaderCircle, Store } from 'lucide-svelte';
|
||||
import Comp_exhibit_search from './ae_comp__exhibit_search.svelte';
|
||||
|
||||
let event_exhibit_obj_li = liveQuery(() => {
|
||||
// *** Initialization & Store Guard ***
|
||||
if ($events_loc.leads) {
|
||||
if (typeof $events_loc.leads.search_version === 'undefined') $events_loc.leads.search_version = 0;
|
||||
if (typeof $events_loc.leads.qry__remote_first === 'undefined') $events_loc.leads.qry__remote_first = false;
|
||||
if (typeof $events_loc.leads.qry__search_text === 'undefined') $events_loc.leads.qry__search_text = '';
|
||||
if (typeof $events_loc.leads.qry__sort_order === 'undefined') $events_loc.leads.qry__sort_order = 'name_asc';
|
||||
}
|
||||
|
||||
let exhibit_id_li: Array<string> = $state([]);
|
||||
let search_debounce_timer: any = null;
|
||||
let last_search_id = 0;
|
||||
let last_executed_key = '';
|
||||
let log_lvl = 1;
|
||||
|
||||
// Stable LiveQuery Pattern
|
||||
let lq__event_exhibit_obj_li = $derived.by(() => {
|
||||
const ids = exhibit_id_li;
|
||||
const event_id = page.params.event_id;
|
||||
if (!event_id) return [];
|
||||
return db_events.exhibit.where({ event_id_random: event_id }).sortBy('name');
|
||||
|
||||
return liveQuery(async () => {
|
||||
// SCENARIO 1: Specific IDs provided (Search Results)
|
||||
if (Array.isArray(ids) && ids.length > 0) {
|
||||
const results = await db_events.exhibit.bulkGet(ids);
|
||||
return results.filter(item => item !== undefined);
|
||||
}
|
||||
|
||||
// SCENARIO 2: Fallback broad search
|
||||
if (event_id && !$events_loc.leads.qry__search_text) {
|
||||
return await db_events.exhibit
|
||||
.where('event_id_random')
|
||||
.equals(event_id)
|
||||
.sortBy('name');
|
||||
}
|
||||
|
||||
return [];
|
||||
});
|
||||
});
|
||||
|
||||
// Standardized Reactive Search Pattern
|
||||
let search_params = $derived({
|
||||
v: $events_loc.leads.search_version,
|
||||
str: ($events_loc.leads.qry__search_text ?? '').toLowerCase().trim(),
|
||||
sort: $events_loc.leads.qry__sort_order,
|
||||
event_id: page.params.event_id,
|
||||
remote_first: $events_loc.leads.qry__remote_first
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
const params = search_params;
|
||||
if (search_debounce_timer) clearTimeout(search_debounce_timer);
|
||||
search_debounce_timer = setTimeout(() => {
|
||||
untrack(() => {
|
||||
handle_search_refresh(params);
|
||||
});
|
||||
}, 300);
|
||||
return () => {
|
||||
if (search_debounce_timer) clearTimeout(search_debounce_timer);
|
||||
};
|
||||
});
|
||||
|
||||
async function handle_search_refresh(params: any) {
|
||||
const qry_key = JSON.stringify(params);
|
||||
if (qry_key === last_executed_key) return;
|
||||
last_executed_key = qry_key;
|
||||
|
||||
const current_search_id = ++last_search_id;
|
||||
const event_id = params.event_id;
|
||||
const remote_first = params.remote_first;
|
||||
|
||||
if (!event_id) return;
|
||||
|
||||
untrack(() => {
|
||||
$events_sess.leads.submit_status__search = 'searching';
|
||||
});
|
||||
|
||||
const qry_str = params.str;
|
||||
|
||||
// 1. Local Search
|
||||
if (!remote_first) {
|
||||
try {
|
||||
let local_results = await db_events.exhibit
|
||||
.where('event_id_random')
|
||||
.equals(event_id)
|
||||
.filter(exhibit => {
|
||||
if (qry_str) {
|
||||
const name = (exhibit.name ?? '').toLowerCase();
|
||||
const code = (exhibit.code ?? '').toLowerCase();
|
||||
if (!name.includes(qry_str) && !code.includes(qry_str)) return false;
|
||||
}
|
||||
return true;
|
||||
})
|
||||
.toArray();
|
||||
|
||||
local_results.sort((a, b) => {
|
||||
switch (params.sort) {
|
||||
case 'name_asc': return (a.name ?? '').localeCompare(b.name ?? '');
|
||||
case 'name_desc': return (b.name ?? '').localeCompare(a.name ?? '');
|
||||
case 'code_asc': return (a.code ?? '').localeCompare(b.code ?? '');
|
||||
case 'code_desc': return (b.code ?? '').localeCompare(a.code ?? '');
|
||||
case 'updated_desc': return new Date(b.updated_on || 0).getTime() - new Date(a.updated_on || 0).getTime();
|
||||
default: return (a.name ?? '').localeCompare(b.name ?? '');
|
||||
}
|
||||
});
|
||||
|
||||
const local_ids = local_results.map(e => String(e.id || e.event_exhibit_id_random)).filter(Boolean);
|
||||
if (current_search_id === last_search_id) {
|
||||
untrack(() => {
|
||||
exhibit_id_li = local_ids;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Exhibit Local Search failed.', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Remote Revalidation
|
||||
try {
|
||||
let order_by_li: any = {};
|
||||
switch (params.sort) {
|
||||
case 'name_asc': order_by_li = { name: 'ASC' }; break;
|
||||
case 'name_desc': order_by_li = { name: 'DESC' }; break;
|
||||
case 'code_asc': order_by_li = { code: 'ASC' }; break;
|
||||
case 'code_desc': order_by_li = { code: 'DESC' }; break;
|
||||
case 'updated_desc': order_by_li = { updated_on: 'DESC' }; break;
|
||||
default: order_by_li = { name: 'ASC' };
|
||||
}
|
||||
|
||||
const results = await events_func.search__exhibit({
|
||||
api_cfg: $ae_api,
|
||||
event_id: event_id,
|
||||
fulltext_search_qry_str: qry_str || null,
|
||||
order_by_li,
|
||||
limit: 100
|
||||
});
|
||||
|
||||
if (current_search_id === last_search_id) {
|
||||
const api_ids = results.map((e: any) => String(e.id || e.event_exhibit_id_random)).filter(Boolean);
|
||||
untrack(() => {
|
||||
exhibit_id_li = api_ids;
|
||||
$events_sess.leads.submit_status__search = 'done';
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
if (current_search_id === last_search_id) {
|
||||
untrack(() => {
|
||||
$events_sess.leads.submit_status__search = 'error';
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<section class="ae_events_leads_new h-full w-full flex flex-col items-center space-y-4">
|
||||
<section class="ae_events_leads_new h-full w-full flex flex-col items-center space-y-4 p-4">
|
||||
<h1 class="h2">Exhibitor Leads</h1>
|
||||
|
||||
{#if $event_exhibit_obj_li && $event_exhibit_obj_li.length > 0}
|
||||
<Comp_exhibit_search
|
||||
event_id={page.params.event_id ?? ''}
|
||||
/>
|
||||
|
||||
{#if $events_sess.leads.submit_status__search === 'searching' && exhibit_id_li.length === 0}
|
||||
<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">Searching exhibits...</p>
|
||||
</div>
|
||||
{:else if $lq__event_exhibit_obj_li && $lq__event_exhibit_obj_li.length > 0}
|
||||
<h2 class="h3">Select your exhibit from the list</h2>
|
||||
<ul class="list">
|
||||
{#each $event_exhibit_obj_li as exhibit_obj}
|
||||
<li>
|
||||
<a href="/events/{page.params.event_id}/leads/exhibit/{exhibit_obj.id_random}">
|
||||
{exhibit_obj.name} (Booth #{exhibit_obj.code})
|
||||
</a>
|
||||
</li>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 w-full max-w-6xl">
|
||||
{#each $lq__event_exhibit_obj_li as exhibit_obj}
|
||||
<a
|
||||
href="/events/{page.params.event_id}/leads/exhibit/{exhibit_obj.id_random}"
|
||||
class="card card-hover p-4 flex flex-col items-center justify-center text-center space-y-2 preset-tonal"
|
||||
>
|
||||
<Store size="2em" />
|
||||
<div class="font-bold text-lg">{exhibit_obj.name}</div>
|
||||
<div class="badge preset-filled-surface-500">Booth #{exhibit_obj.code}</div>
|
||||
</a>
|
||||
{/each}
|
||||
</ul>
|
||||
</div>
|
||||
{:else}
|
||||
<p>No exhibits found for this event.</p>
|
||||
<p class="opacity-50 mt-10">No exhibits found matching your search.</p>
|
||||
{/if}
|
||||
</section>
|
||||
</section>
|
||||
Reference in New Issue
Block a user