Add PIS fetch logic
This commit is contained in:
@@ -5,34 +5,51 @@
|
|||||||
import type { PisObjects } from '@owlboard/api-schema-types';
|
import type { PisObjects } from '@owlboard/api-schema-types';
|
||||||
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
|
import { OwlClient, ApiError, ValidationError } from '$lib/owlClient';
|
||||||
|
|
||||||
|
|
||||||
let results = $state<PisObjects[]>([]);
|
let results = $state<PisObjects[]>([]);
|
||||||
let resultsLoaded = $state<boolean>(false);
|
let resultsLoaded = $state<boolean>(false);
|
||||||
let errorState = $state<{status: number, message: message} | null>(null);
|
let errorState = $state<{status: number, message: string} | null>(null);
|
||||||
|
|
||||||
async function handleStartEndSearch(start: string, end: string): Promise<void> {
|
async function handleStartEndSearch(start: string, end: string): Promise<void> {
|
||||||
console.log(`PIS Search: ${start}-${end}`);
|
console.log(`PIS Search: ${start}-${end}`);
|
||||||
errorState = null;
|
errorState = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
results = await OwlClient.pis.getByStartEndCrs(start, end);
|
const response = await OwlClient.pis.getByStartEndCrs(start, end);
|
||||||
|
results = await response.data || [];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof ValidationError) {
|
if (e instanceof ValidationError) {
|
||||||
errorState = { status: 400, message: e.message };
|
errorState = { status: 400, message: e.message };
|
||||||
} else if (e instanceof ApiError) {
|
} else if (e instanceof ApiError) {
|
||||||
errorState = { status: 500, message: e.message };
|
console.log(e)
|
||||||
|
errorState = { status: 20, message: e.message };
|
||||||
} else {
|
} else {
|
||||||
errorState = { status: 0, message: `Unknown Error: ${e.message}` };
|
errorState = { status: 0, message: `Unknown Error: ${e.message}` };
|
||||||
}
|
}
|
||||||
}
|
} finally {
|
||||||
|
|
||||||
resultsLoaded = true;
|
resultsLoaded = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function handleCodeSearch(code: string) {
|
async function handleCodeSearch(code: string) {
|
||||||
console.log(`PIS Search: ${code}`);
|
console.log(`PIS Search: ${code}`);
|
||||||
// Fetch API Results Here
|
errorState = null;
|
||||||
|
try {
|
||||||
|
const response = await OwlClient.pis.getByCode(code);
|
||||||
|
results = response.data || []
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof ValidationError) {
|
||||||
|
errorState = { status: 400, message: e.message };
|
||||||
|
} else if (e instanceof ApiError) {
|
||||||
|
console.log(e)
|
||||||
|
errorState = { status: 20, message: e.message };
|
||||||
|
} else {
|
||||||
|
errorState = { status: 0, message: `Unknown Error: ${e.message}` };
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
resultsLoaded = true;
|
resultsLoaded = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function clearResults() {
|
function clearResults() {
|
||||||
console.log('Clearing Results');
|
console.log('Clearing Results');
|
||||||
@@ -49,16 +66,21 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<div class="result-container">
|
<div class="result-container">
|
||||||
{#if errorState}
|
{#if errorState}
|
||||||
<span class="errCode">{errorState.status}</span>
|
<span class="errCode">Error: {errorState.status}</span>
|
||||||
<span class="errMsg">{errorState.message}</span>
|
<span class="errMsg">{errorState.message}</span>
|
||||||
{:else}
|
{:else}
|
||||||
{#if !results.length}
|
{#if results.length}
|
||||||
<p class="no-results">No results found</p>
|
<h2 class="result-title">{results.length} Result{#if results.length > 1}s{/if} found:</h2>
|
||||||
|
{#each results as result}
|
||||||
|
<p>{JSON.stringify(result)}</p>
|
||||||
|
{/each}
|
||||||
{:else}
|
{:else}
|
||||||
<p class="no-results">Results Loaded - Display logic not present</p>
|
<p class="no-results">No matching results</p>
|
||||||
{/if}{/if}
|
{/if}
|
||||||
|
{/if}
|
||||||
|
<div class="reset-button-container">
|
||||||
<Button onclick={clearResults}>Reset</Button>
|
<Button onclick={clearResults}>Reset</Button>
|
||||||
</div>
|
</div> </div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@@ -82,11 +104,21 @@
|
|||||||
justify-content: center;
|
justify-content: center;
|
||||||
background: var(--color-accent);
|
background: var(--color-accent);
|
||||||
border-radius: 15px;
|
border-radius: 15px;
|
||||||
padding: 0 0 20px 0;
|
padding: 20px 0 20px 0;
|
||||||
margin: auto;
|
margin: auto;
|
||||||
margin-top: 25px;
|
margin-top: 25px;
|
||||||
width: 90%;
|
width: 90%;
|
||||||
max-width: 1000px;
|
max-width: 1000px;
|
||||||
box-shadow: var(--shadow-std);
|
box-shadow: var(--shadow-std);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.errCode {
|
||||||
|
color: rgb(255, 54, 54);
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.reset-button-container {
|
||||||
|
padding: 20px 0 3px 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
Reference in New Issue
Block a user