1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-07-24 07:19:36 +02:00

pagination! fixed for private

This commit is contained in:
Sean Morley 2024-07-12 09:11:00 -04:00
parent 680a46e798
commit 1929227104
3 changed files with 41 additions and 18 deletions

View file

@ -12,7 +12,7 @@ from .permissions import IsOwnerOrReadOnly, IsPublicReadOnly
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
class StandardResultsSetPagination(PageNumberPagination): class StandardResultsSetPagination(PageNumberPagination):
page_size = 6 page_size = 10
page_size_query_param = 'page_size' page_size_query_param = 'page_size'
max_page_size = 1000 max_page_size = 1000

View file

@ -428,19 +428,37 @@ export const actions: Actions = {
}, },
changePage: async (event) => { changePage: async (event) => {
const formData = await event.request.formData(); const formData = await event.request.formData();
const url = formData.get('url'); const next = formData.get('next') as string;
const previous = formData.get('previous') as string;
const page = formData.get('page') as string;
console.log('Received URL:', url); if (!page) {
if (!url) {
return { return {
status: 400, status: 400,
body: { error: 'URL is required' } body: { error: 'Missing required fields' }
}; };
} }
// Start with the current URL if next and previous are not provided
let url: string = next || previous || event.url.toString();
let index = url.indexOf('/api');
let newUrl = url.substring(index);
console.log('NEW URL' + newUrl);
url = serverEndpoint + newUrl;
console.log('URL' + url);
// Replace or add the page number in the URL
if (url.includes('page=')) {
url = url.replace(/page=\d+/, `page=${page}`);
} else {
// If 'page=' is not in the URL, add it
url += url.includes('?') ? '&' : '?';
url += `page=${page}`;
}
try { try {
const response = await fetch(url.toString(), { const response = await fetch(url, {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Cookie: `${event.cookies.get('auth')}` Cookie: `${event.cookies.get('auth')}`

View file

@ -18,9 +18,12 @@
let isShowingCreateModal: boolean = false; let isShowingCreateModal: boolean = false;
let newType: string = ''; let newType: string = '';
let resultsPerPage: number = 10;
let next: string | null = data.props.next || null; let next: string | null = data.props.next || null;
let previous: string | null = data.props.previous || null; let previous: string | null = data.props.previous || null;
let count = data.props.count || 0; let count = data.props.count || 0;
let totalPages = Math.ceil(count / resultsPerPage);
function handleChangePage() { function handleChangePage() {
return async ({ result }: any) => { return async ({ result }: any) => {
@ -30,6 +33,7 @@
next = result.data.body.next; next = result.data.body.next;
previous = result.data.body.previous; previous = result.data.body.previous;
count = result.data.body.count; count = result.data.body.count;
totalPages = Math.ceil(count / resultsPerPage);
} }
}; };
} }
@ -47,6 +51,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;
totalPages = Math.ceil(count / resultsPerPage);
console.log(next); console.log(next);
} }
} }
@ -182,17 +187,17 @@
</div> </div>
<div class="join grid grid-cols-2"> <div class="join grid grid-cols-2">
<div class="join grid grid-cols-2"> <div class="join grid grid-cols-2">
{#if previous} {#if next || previous}
<form action="?/changePage" method="POST" use:enhance={handleChangePage}> <div class="join">
<input type="hidden" name="url" value={previous} /> {#each Array.from({ length: totalPages }, (_, i) => i + 1) as page}
<button class="join-item btn btn-outline" type="submit">Previous page</button> <form action="?/changePage" method="POST" use:enhance={handleChangePage}>
</form> <input type="hidden" name="page" value={page} />
{/if} <input type="hidden" name="next" value={next} />
{#if next} <input type="hidden" name="previous" value={previous} />
<form action="?/changePage" method="POST" use:enhance={handleChangePage}> <button class="join-item btn">{page}</button>
<input type="hidden" name="url" value={next} /> </form>
<button class="join-item btn btn-outline" type="submit">Next page</button> {/each}
</form> </div>
{/if} {/if}
</div> </div>
</div> </div>