|
| 1 | +<script lang="ts" generics="ResultItemType extends ResultItemBase"> |
| 2 | + import Campaign from '$lib/components/Campaign.svelte'; |
| 3 | + import { publicConfig } from '$lib/config'; |
| 4 | + import type { S3FileInfo } from '$lib/server/s3'; |
| 5 | + import { Pagination } from '@skeletonlabs/skeleton-svelte'; |
| 6 | + import { base } from '$app/paths'; |
| 7 | +
|
| 8 | + interface ResultItemBase { |
| 9 | + prefix: string; |
| 10 | + } |
| 11 | +
|
| 12 | + // get props from data loader |
| 13 | + let { |
| 14 | + results, |
| 15 | + resultsTotal, |
| 16 | + tableHeaders, |
| 17 | + handlePageChange, |
| 18 | + } = $props(); |
| 19 | +
|
| 20 | + const headers = ["Campaign"].concat(Object.values(tableHeaders)); |
| 21 | +
|
| 22 | + // Pagination of Campaigns |
| 23 | + let currentPage = $state(1); |
| 24 | + let pageSize = publicConfig.PUBLIC_RESULTS_PER_PAGE; |
| 25 | +
|
| 26 | + // State for the fetched detailed data for the main content |
| 27 | + let detailedContent = $state<S3FileInfo[] | null>(null); |
| 28 | + let isLoadingDetails = $state(false); |
| 29 | + let detailError = $state<string | null>(null); |
| 30 | + let activeResultItem = $state<ResultItemType | null>(null); |
| 31 | +
|
| 32 | + async function fetchDetails(campaignPath: string) { |
| 33 | + isLoadingDetails = true; |
| 34 | + detailError = null; |
| 35 | + detailedContent = null; |
| 36 | + try { |
| 37 | + // Adjust the URL to your actual API endpoint structure |
| 38 | + const response = await fetch(`${base}/api/${campaignPath}`); |
| 39 | + if (!response.ok) { |
| 40 | + const errorData = await response.json().catch(() => ({ message: `HTTP error! status: ${response.status}` })); |
| 41 | + throw new Error(errorData.message || `Failed to fetch details. Status: ${response.status}`); |
| 42 | + } |
| 43 | + const fetchedDetails: S3FileInfo[] = await response.json(); |
| 44 | + detailedContent = fetchedDetails; |
| 45 | + } catch (err: any) { |
| 46 | + console.error('Error fetching details:', err); |
| 47 | + detailError = err.message || 'An unknown error occurred.'; |
| 48 | + } finally { |
| 49 | + isLoadingDetails = false; |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + function handleRowClick(result: ResultItemType) { |
| 54 | + activeResultItem = result; |
| 55 | + if (result && result.prefix) { |
| 56 | + fetchDetails(result.prefix); |
| 57 | + } else { |
| 58 | + // Reset main content if row is invalid or deselected (if implementing deselection) |
| 59 | + detailedContent = null; |
| 60 | + isLoadingDetails = false; |
| 61 | + detailError = null; |
| 62 | + } |
| 63 | + } |
| 64 | +
|
| 65 | + $effect(() => { |
| 66 | + // This effect runs whenever the `results` array changes |
| 67 | + if (results && results.length > 0) { |
| 68 | + // Automatically select the first item of the list |
| 69 | + const firstItem = results[0]; |
| 70 | + handleRowClick(firstItem); |
| 71 | + } else { |
| 72 | + // If the new page has no results, clear the details view |
| 73 | + activeResultItem = null; |
| 74 | + detailedContent = null; |
| 75 | + } |
| 76 | + }); |
| 77 | +</script> |
| 78 | + |
| 79 | +<div class="bg-tertiary-50-800 space-y-4 rounded p-4"> |
| 80 | + <div class="table-wrap bg-tertiary-50-800 overflow-x-auto rounded-lg shadow"> |
| 81 | + <table class="table caption-bottom"> |
| 82 | + <thead> |
| 83 | + <tr> |
| 84 | + {#each headers as header} |
| 85 | + <th>{header}</th> |
| 86 | + {/each} |
| 87 | + </tr> |
| 88 | + </thead> |
| 89 | + <tbody class="[&>tr]:hover:bg-tertiary-100-900"> |
| 90 | + {#each results as result, i} |
| 91 | + <tr |
| 92 | + onclick={() => handleRowClick(result)} |
| 93 | + class="cursor-pointer" |
| 94 | + class:bg-tertiary-200-800={activeResultItem?.prefix === result.prefix} |
| 95 | + > |
| 96 | + {#each Object.values(result) as value, key} |
| 97 | + <td> |
| 98 | + {#if Array.isArray(value)} |
| 99 | + <ul class="list-disc pl-5"> |
| 100 | + {#each value as item} |
| 101 | + <li>{item}</li> |
| 102 | + {/each} |
| 103 | + </ul> |
| 104 | + {:else} |
| 105 | + {value} |
| 106 | + {/if} |
| 107 | + </td> |
| 108 | + {/each} |
| 109 | + </tr> |
| 110 | + {/each} |
| 111 | + </tbody> |
| 112 | + </table> |
| 113 | + </div> |
| 114 | + <footer class=""> |
| 115 | + <Pagination |
| 116 | + data={results} |
| 117 | + {currentPage} |
| 118 | + onPageChange={(e) => handlePageChange(e)} |
| 119 | + pageSize={pageSize} |
| 120 | + siblingCount={4} |
| 121 | + count={resultsTotal} |
| 122 | + alternative |
| 123 | + /> |
| 124 | + </footer> |
| 125 | +</div> |
| 126 | +<Campaign |
| 127 | + isLoading={isLoadingDetails} |
| 128 | + error={detailError} |
| 129 | + campaignFiles={detailedContent?.files} |
| 130 | + activeCampaign={activeResultItem?.prefix} |
| 131 | + title={activeResultItem?.prefix} |
| 132 | +/> |
0 commit comments