mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Update pokedex entry data structure and re-add error handling
This commit is contained in:
@@ -10,6 +10,10 @@ export const GET = async ({ url }) => {
|
||||
await dbConnect();
|
||||
const repo = new CombinedDataRepository();
|
||||
const combinedData = await repo.findCombinedData(page, limit);
|
||||
|
||||
if (combinedData.length === 0) {
|
||||
return json({ error: 'No combined data found' }, { status: 404 });
|
||||
}
|
||||
const totalCount = await repo.countCombinedData();
|
||||
const totalPages = Math.ceil(totalCount / limit);
|
||||
|
||||
|
||||
@@ -7,11 +7,15 @@
|
||||
import PokedexEntryCatchRecord from '$lib/components/pokedex/PokedexEntryCatchRecord.svelte';
|
||||
import Pagination from '$lib/components/Pagination.svelte';
|
||||
import { browser } from '$app/environment';
|
||||
import type { PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
|
||||
let combinedData = null as CombinedData[] | null;
|
||||
let currentPage = 1 as number;
|
||||
let itemsPerPage = 20 as number;
|
||||
let totalPages = 0 as number;
|
||||
let creatingRecords = false;
|
||||
let totalRecordsCreated = 0;
|
||||
let failedToLoad = false;
|
||||
|
||||
let localUser: User | null;
|
||||
const unsubscribe = user.subscribe((value) => {
|
||||
@@ -32,17 +36,19 @@
|
||||
|
||||
async function getData() {
|
||||
combinedData = null;
|
||||
|
||||
const response = await fetch(`/api/combined-data?page=${currentPage}&limit=${itemsPerPage}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.error) {
|
||||
failedToLoad = true;
|
||||
return;
|
||||
}
|
||||
|
||||
combinedData = data.combinedData;
|
||||
totalPages = data.totalPages;
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
await getData();
|
||||
});
|
||||
|
||||
$: {
|
||||
if (browser) {
|
||||
currentPage, itemsPerPage, getData();
|
||||
@@ -73,6 +79,66 @@
|
||||
console.error('Error updating catch record:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getPokedexEntries() {
|
||||
const response = await fetch('/api/pokedexentries');
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch Pokémon data');
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
async function createCatchRecords() {
|
||||
// this user doesn't have any catch records, so we need to create them
|
||||
await getPokedexEntries().then(async (pokedexEntries) => {
|
||||
// If there are no catch records, make one for each pokedex entry
|
||||
creatingRecords = true;
|
||||
const newCatchRecords = pokedexEntries.map((entry: PokedexEntry) => ({
|
||||
userId: localUser?.id,
|
||||
pokedexEntryId: entry._id,
|
||||
haveToEvolve: false,
|
||||
caught: false,
|
||||
inHome: false,
|
||||
hasGigantamaxed: false,
|
||||
personalNotes: ''
|
||||
}));
|
||||
|
||||
if (newCatchRecords.length === 0) {
|
||||
alert('No pokedex entries to create catch records for');
|
||||
return;
|
||||
}
|
||||
|
||||
const batchSize = 500;
|
||||
for (let i = 0; i < newCatchRecords.length; i += batchSize) {
|
||||
const batch = newCatchRecords.slice(i, i + batchSize);
|
||||
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(batch)
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/catch-records', requestOptions);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to create catch records');
|
||||
}
|
||||
|
||||
const createdRecords = await response.json();
|
||||
totalRecordsCreated += createdRecords.length;
|
||||
console.log(`Created ${createdRecords.length} catch records`);
|
||||
} catch (error) {
|
||||
console.error('Error creating catch records:', error);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Created catch records for each pokedex entry');
|
||||
creatingRecords = false;
|
||||
failedToLoad = false;
|
||||
await getData();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
@@ -110,6 +176,21 @@
|
||||
on:updateCatch={() => updateACatch(catchRecord)}
|
||||
/>
|
||||
{/each}
|
||||
{:else if failedToLoad}
|
||||
{#if creatingRecords && totalRecordsCreated > 0}
|
||||
<p>Processed {totalRecordsCreated} Pokédex entries so far...</p>
|
||||
<p>Please be patient, this may take some time.</p>
|
||||
{:else if creatingRecords}
|
||||
<p>Processing...</p>
|
||||
<p>Please be patient, this may take some time.</p>
|
||||
{:else}
|
||||
<h1>Failed to load</h1>
|
||||
<p>
|
||||
If you're seeing this, you probably haven't created your Pokédex data yet. Please do so
|
||||
by clicking this button.
|
||||
</p>
|
||||
<button class="btn" on:click={createCatchRecords}>Create Pokédex data</button>
|
||||
{/if}
|
||||
{:else}
|
||||
<div class="min-w-max mx-auto">
|
||||
<h1>Loading Pokédex</h1>
|
||||
|
||||
Reference in New Issue
Block a user