mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 11:03:44 +00:00
chore(*): Clean up migration artifacts and optimize data repository queries
This commit is contained in:
@@ -59,10 +59,6 @@
|
||||
break;
|
||||
}
|
||||
|
||||
// if (strippedPokedexNumber == '869') {
|
||||
// console.log(sanitisedPokemonName, sanitisedForm);
|
||||
// }
|
||||
|
||||
// If the form is contained in the identifier, use that
|
||||
if (
|
||||
pokeApiPokemon.find(
|
||||
|
||||
@@ -10,22 +10,24 @@
|
||||
export let showForms: boolean;
|
||||
export let showShiny: boolean;
|
||||
|
||||
// Create a working copy for editing
|
||||
$: workingCatchRecord = catchRecord || {
|
||||
_id: `temp-${Date.now()}-${pokedexEntry._id}`, // Generate temporary ID
|
||||
userId: '', // TODO: Get from session context
|
||||
pokedexEntryId: pokedexEntry._id,
|
||||
haveToEvolve: false,
|
||||
caught: false,
|
||||
inHome: false,
|
||||
hasGigantamaxed: false,
|
||||
personalNotes: ''
|
||||
};
|
||||
// Create a default catch record if none exists
|
||||
$: if (!catchRecord) {
|
||||
catchRecord = {
|
||||
_id: '', // Empty string, not temp ID - will be created by server
|
||||
userId: '',
|
||||
pokedexEntryId: pokedexEntry._id,
|
||||
haveToEvolve: false,
|
||||
caught: false,
|
||||
inHome: false,
|
||||
hasGigantamaxed: false,
|
||||
personalNotes: ''
|
||||
};
|
||||
}
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
|
||||
function updateCatchRecord() {
|
||||
dispatch('updateCatch', { pokedexEntry, catchRecord: workingCatchRecord });
|
||||
dispatch('updateCatch', { pokedexEntry, catchRecord });
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -87,7 +89,7 @@
|
||||
<span class="block font-bold mr-2">Caught:</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={workingCatchRecord.caught}
|
||||
bind:checked={catchRecord.caught}
|
||||
class="checkbox checkbox-primary border-black"
|
||||
on:change={updateCatchRecord}
|
||||
/>
|
||||
@@ -100,7 +102,7 @@
|
||||
<span class="block font-bold mr-2">Needs to evolve:</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={workingCatchRecord.haveToEvolve}
|
||||
bind:checked={catchRecord.haveToEvolve}
|
||||
class="checkbox checkbox-primary border-black"
|
||||
on:change={updateCatchRecord}
|
||||
/>
|
||||
@@ -113,7 +115,7 @@
|
||||
<span class="block font-bold mr-2">In home:</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={workingCatchRecord.inHome}
|
||||
bind:checked={catchRecord.inHome}
|
||||
class="checkbox checkbox-primary border-black"
|
||||
on:change={updateCatchRecord}
|
||||
/>
|
||||
@@ -127,7 +129,7 @@
|
||||
<span class="block font-bold mr-2">Has Gigantamaxed:</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={workingCatchRecord.hasGigantamaxed}
|
||||
bind:checked={catchRecord.hasGigantamaxed}
|
||||
class="checkbox checkbox-primary border-black"
|
||||
on:change={updateCatchRecord}
|
||||
/>
|
||||
@@ -141,7 +143,7 @@
|
||||
for={`personalNotesInput-${catchRecord?._id || pokedexEntry._id}`}>Notes:</label
|
||||
>
|
||||
<textarea
|
||||
bind:value={workingCatchRecord.personalNotes}
|
||||
bind:value={catchRecord.personalNotes}
|
||||
id={`personalNotesInput-${catchRecord?._id || pokedexEntry._id}`}
|
||||
class="form-textarea w-full p-2 border rounded"
|
||||
on:change={updateCatchRecord}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
export let failedToLoad = false;
|
||||
export let updateACatch = (event: any) => {};
|
||||
export let createCatchRecords = () => {};
|
||||
export let userId: string | null = null;
|
||||
</script>
|
||||
|
||||
<main class="flex-1 p-4 w-full">
|
||||
|
||||
@@ -54,10 +54,7 @@ class CombinedDataRepository {
|
||||
region: string = '',
|
||||
game: string = ''
|
||||
): Promise<CombinedData[]> {
|
||||
let query = this.supabase.from('pokedex_entries').select(`
|
||||
*,
|
||||
catch_records(*)
|
||||
`);
|
||||
let query = this.supabase.from('pokedex_entries').select('*');
|
||||
|
||||
// Apply filters
|
||||
if (!enableForms) {
|
||||
@@ -85,18 +82,36 @@ class CombinedDataRepository {
|
||||
.order('boxPlacementColumn', { ascending: true });
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
const { data: entries, error: entriesError } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error finding combined data:', error);
|
||||
if (entriesError) {
|
||||
console.error('Error finding combined data:', entriesError);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Transform the data and filter catch records by user
|
||||
return (data || []).map((entry) => {
|
||||
const userCatchRecord = Array.isArray(entry.catch_records)
|
||||
? entry.catch_records.find((record: CatchRecordDB) => record.userId === userId)
|
||||
: null;
|
||||
if (!entries || entries.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get catch records for all entries
|
||||
let catchRecords: CatchRecordDB[] = [];
|
||||
if (userId) {
|
||||
const entryIds = entries.map((entry) => entry.id);
|
||||
const { data: records, error: recordsError } = await this.supabase
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('userId', userId)
|
||||
.in('pokedexEntryId', entryIds);
|
||||
|
||||
if (!recordsError && records) {
|
||||
catchRecords = records;
|
||||
}
|
||||
}
|
||||
|
||||
// Combine the data exactly like master branch
|
||||
return entries.map((entry) => {
|
||||
const userCatchRecord =
|
||||
catchRecords.find((record) => record.pokedexEntryId === entry.id) || null;
|
||||
|
||||
const transformedEntry = this.transformPokedexEntry(entry);
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
@@ -121,15 +136,7 @@ class CombinedDataRepository {
|
||||
const from = (page - 1) * limit;
|
||||
const to = from + limit - 1;
|
||||
|
||||
let query = this.supabase
|
||||
.from('pokedex_entries')
|
||||
.select(
|
||||
`
|
||||
*,
|
||||
catch_records(*)
|
||||
`
|
||||
)
|
||||
.range(from, to);
|
||||
let query = this.supabase.from('pokedex_entries').select('*').range(from, to);
|
||||
|
||||
// Apply filters
|
||||
if (!enableForms) {
|
||||
@@ -157,18 +164,36 @@ class CombinedDataRepository {
|
||||
.order('boxPlacementColumn', { ascending: true });
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
const { data: entries, error: entriesError } = await query;
|
||||
|
||||
if (error) {
|
||||
console.error('Error finding paginated combined data:', error);
|
||||
if (entriesError) {
|
||||
console.error('Error finding paginated combined data:', entriesError);
|
||||
return [];
|
||||
}
|
||||
|
||||
// Transform the data and filter catch records by user
|
||||
return (data || []).map((entry) => {
|
||||
const userCatchRecord = Array.isArray(entry.catch_records)
|
||||
? entry.catch_records.find((record: CatchRecordDB) => record.userId === userId)
|
||||
: null;
|
||||
if (!entries || entries.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Get catch records for these entries
|
||||
let catchRecords: CatchRecordDB[] = [];
|
||||
if (userId) {
|
||||
const entryIds = entries.map((entry) => entry.id);
|
||||
const { data: records, error: recordsError } = await this.supabase
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('userId', userId)
|
||||
.in('pokedexEntryId', entryIds);
|
||||
|
||||
if (!recordsError && records) {
|
||||
catchRecords = records;
|
||||
}
|
||||
}
|
||||
|
||||
// Combine the data exactly like master branch
|
||||
return entries.map((entry) => {
|
||||
const userCatchRecord =
|
||||
catchRecords.find((record) => record.pokedexEntryId === entry.id) || null;
|
||||
|
||||
const transformedEntry = this.transformPokedexEntry(entry);
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
|
||||
@@ -99,7 +99,6 @@
|
||||
};
|
||||
|
||||
try {
|
||||
// Use enhanced fetch that includes authentication context
|
||||
const response = await fetch('/api/catch-records', requestOptions);
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
@@ -107,6 +106,9 @@
|
||||
alert('Failed to update catch record');
|
||||
throw new Error('Failed to update catch record');
|
||||
}
|
||||
|
||||
// Refresh the data to reflect changes from server
|
||||
await getData(false);
|
||||
} catch (error) {
|
||||
console.error('Error updating catch record:', error);
|
||||
}
|
||||
@@ -226,13 +228,11 @@
|
||||
|
||||
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();
|
||||
@@ -292,6 +292,7 @@
|
||||
bind:creatingRecords
|
||||
bind:totalRecordsCreated
|
||||
bind:failedToLoad
|
||||
userId={localUser?.id}
|
||||
{updateACatch}
|
||||
{createCatchRecords}
|
||||
/>
|
||||
|
||||
@@ -16,18 +16,22 @@
|
||||
onDestroy(unsubscribe);
|
||||
|
||||
async function getUser() {
|
||||
const {
|
||||
data: { session }
|
||||
} = await supabase.auth.getSession();
|
||||
try {
|
||||
const {
|
||||
data: { session }
|
||||
} = await supabase.auth.getSession();
|
||||
|
||||
if (session) {
|
||||
localUser = session.user;
|
||||
} else {
|
||||
localUser = null;
|
||||
if (session) {
|
||||
localUser = session.user;
|
||||
user.set(localUser);
|
||||
await goto('/mydex', { replace: true });
|
||||
} else {
|
||||
localUser = null;
|
||||
user.set(localUser);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting user session:', error);
|
||||
}
|
||||
|
||||
user.set(localUser);
|
||||
goto('/mydex', { replace: true });
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user