mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-23 14:59:36 +02:00
pagination! fixed for private
This commit is contained in:
parent
a3784ae164
commit
fc9ba3ad96
2 changed files with 82 additions and 31 deletions
|
@ -3,7 +3,7 @@ import type { PageServerLoad } from './$types';
|
||||||
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
||||||
import type { Adventure } from '$lib/types';
|
import type { Adventure } from '$lib/types';
|
||||||
|
|
||||||
import type { Actions } from '@sveltejs/kit';
|
import type { Actions, RequestEvent } from '@sveltejs/kit';
|
||||||
import { fetchCSRFToken, tryRefreshToken } from '$lib/index.server';
|
import { fetchCSRFToken, tryRefreshToken } from '$lib/index.server';
|
||||||
import { checkLink } from '$lib';
|
import { checkLink } from '$lib';
|
||||||
|
|
||||||
|
@ -41,7 +41,8 @@ export const load = (async (event) => {
|
||||||
props: {
|
props: {
|
||||||
adventures,
|
adventures,
|
||||||
next,
|
next,
|
||||||
previous
|
previous,
|
||||||
|
count
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -395,6 +396,8 @@ export const actions: Actions = {
|
||||||
let previous = null;
|
let previous = null;
|
||||||
let count = 0;
|
let count = 0;
|
||||||
|
|
||||||
|
console.log(filterString);
|
||||||
|
|
||||||
let visitedFetch = await fetch(
|
let visitedFetch = await fetch(
|
||||||
`${serverEndpoint}/api/adventures/filtered?types=${filterString}`,
|
`${serverEndpoint}/api/adventures/filtered?types=${filterString}`,
|
||||||
{
|
{
|
||||||
|
@ -413,6 +416,7 @@ export const actions: Actions = {
|
||||||
previous = res.previous;
|
previous = res.previous;
|
||||||
count = res.count;
|
count = res.count;
|
||||||
adventures = [...adventures, ...visited];
|
adventures = [...adventures, ...visited];
|
||||||
|
console.log(next, previous, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -421,5 +425,52 @@ export const actions: Actions = {
|
||||||
previous,
|
previous,
|
||||||
count
|
count
|
||||||
};
|
};
|
||||||
|
},
|
||||||
|
changePage: async (event) => {
|
||||||
|
const formData = await event.request.formData();
|
||||||
|
const url = formData.get('url');
|
||||||
|
|
||||||
|
console.log('Received URL:', url);
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return {
|
||||||
|
status: 400,
|
||||||
|
body: { error: 'URL is required' }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url.toString(), {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Cookie: `${event.cookies.get('auth')}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
const data = await response.json();
|
||||||
|
let adventures = data.results as Adventure[];
|
||||||
|
let next = data.next;
|
||||||
|
let previous = data.previous;
|
||||||
|
let count = data.count;
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
body: {
|
||||||
|
adventures,
|
||||||
|
next,
|
||||||
|
previous,
|
||||||
|
count
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching data:', error);
|
||||||
|
return {
|
||||||
|
status: 500,
|
||||||
|
body: { error: 'Failed to fetch data' }
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { enhance } from '$app/forms';
|
import { enhance, deserialize } from '$app/forms';
|
||||||
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
import AdventureCard from '$lib/components/AdventureCard.svelte';
|
||||||
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
import EditAdventure from '$lib/components/EditAdventure.svelte';
|
||||||
import NewAdventure from '$lib/components/NewAdventure.svelte';
|
import NewAdventure from '$lib/components/NewAdventure.svelte';
|
||||||
|
@ -18,25 +18,20 @@
|
||||||
let isShowingCreateModal: boolean = false;
|
let isShowingCreateModal: boolean = false;
|
||||||
let newType: string = '';
|
let newType: string = '';
|
||||||
|
|
||||||
let next: string | null = null;
|
let next: string | null = data.props.next || null;
|
||||||
let previous: string | null = null;
|
let previous: string | null = data.props.previous || null;
|
||||||
let count = 0;
|
let count = data.props.count || 0;
|
||||||
|
|
||||||
async function changePage(direction: string) {
|
function handleChangePage() {
|
||||||
let url: string = '';
|
return async ({ result }: any) => {
|
||||||
if (direction === 'next' && next) {
|
if (result.type === 'success') {
|
||||||
url = next;
|
console.log(result.data);
|
||||||
} else if (direction === 'previous' && previous) {
|
adventures = result.data.body.adventures as Adventure[];
|
||||||
url = previous;
|
next = result.data.body.next;
|
||||||
} else {
|
previous = result.data.body.previous;
|
||||||
return;
|
count = result.data.body.count;
|
||||||
}
|
}
|
||||||
let res = await fetch(url);
|
};
|
||||||
let result = await res.json();
|
|
||||||
adventures = result.results as Adventure[];
|
|
||||||
next = result.next;
|
|
||||||
previous = result.previous;
|
|
||||||
count = result.count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSubmit() {
|
function handleSubmit() {
|
||||||
|
@ -52,7 +47,7 @@
|
||||||
next = result.data.next;
|
next = result.data.next;
|
||||||
previous = result.data.previous;
|
previous = result.data.previous;
|
||||||
count = result.data.count;
|
count = result.data.count;
|
||||||
// sort(currentSort);
|
console.log(next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -186,15 +181,20 @@
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
<div class="join grid grid-cols-2">
|
<div class="join grid grid-cols-2">
|
||||||
{#if previous}
|
<div class="join grid grid-cols-2">
|
||||||
<button class="join-item btn btn-outline" on:click={() => changePage('previous')}
|
{#if previous}
|
||||||
>Previous page</button
|
<form action="?/changePage" method="POST" use:enhance={handleChangePage}>
|
||||||
>
|
<input type="hidden" name="url" value={previous} />
|
||||||
{/if}
|
<button class="join-item btn btn-outline" type="submit">Previous page</button>
|
||||||
{#if next}
|
</form>
|
||||||
<button class="join-item btn btn-outline" on:click={() => changePage('next')}>Next</button
|
{/if}
|
||||||
>
|
{#if next}
|
||||||
{/if}
|
<form action="?/changePage" method="POST" use:enhance={handleChangePage}>
|
||||||
|
<input type="hidden" name="url" value={next} />
|
||||||
|
<button class="join-item btn btn-outline" type="submit">Next page</button>
|
||||||
|
</form>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue