From 32f5c1a806b92faa7799286e394f4a55f7cf9090 Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Sun, 4 Aug 2024 22:19:09 -0400 Subject: [PATCH 1/3] chore: Increase page size to 25 for adventures and collections views --- backend/server/adventures/views.py | 2 +- frontend/src/routes/adventures/+page.svelte | 2 +- frontend/src/routes/collections/+page.svelte | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/server/adventures/views.py b/backend/server/adventures/views.py index d208bbd3..e36302c5 100644 --- a/backend/server/adventures/views.py +++ b/backend/server/adventures/views.py @@ -15,7 +15,7 @@ from rest_framework import status class StandardResultsSetPagination(PageNumberPagination): - page_size = 10 + page_size = 25 page_size_query_param = 'page_size' max_page_size = 1000 diff --git a/frontend/src/routes/adventures/+page.svelte b/frontend/src/routes/adventures/+page.svelte index 3ab518b2..9837da60 100644 --- a/frontend/src/routes/adventures/+page.svelte +++ b/frontend/src/routes/adventures/+page.svelte @@ -18,7 +18,7 @@ let isShowingCreateModal: boolean = false; let newType: string = ''; - let resultsPerPage: number = 10; + let resultsPerPage: number = 25; let next: string | null = data.props.next || null; let previous: string | null = data.props.previous || null; diff --git a/frontend/src/routes/collections/+page.svelte b/frontend/src/routes/collections/+page.svelte index 4371a5c9..cdb9e028 100644 --- a/frontend/src/routes/collections/+page.svelte +++ b/frontend/src/routes/collections/+page.svelte @@ -21,7 +21,7 @@ let isShowingCreateModal: boolean = false; let newType: string = ''; - let resultsPerPage: number = 10; + let resultsPerPage: number = 25; let currentView: string = 'cards'; From 44cad305287c00acc440a8af21bf4e8454e3bbaa Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Mon, 5 Aug 2024 09:33:21 -0400 Subject: [PATCH 2/3] better filters --- .../src/routes/adventures/+page.server.ts | 428 +++++++++--------- frontend/src/routes/adventures/+page.svelte | 89 ++-- 2 files changed, 267 insertions(+), 250 deletions(-) diff --git a/frontend/src/routes/adventures/+page.server.ts b/frontend/src/routes/adventures/+page.server.ts index 4f3794ef..db019cf7 100644 --- a/frontend/src/routes/adventures/+page.server.ts +++ b/frontend/src/routes/adventures/+page.server.ts @@ -13,12 +13,33 @@ export const load = (async (event) => { if (!event.locals.user) { return redirect(302, '/login'); } else { - let next = null; - let previous = null; let count = 0; let adventures: Adventure[] = []; + + const visited = event.url.searchParams.get('visited'); + const planned = event.url.searchParams.get('planned'); + + let typeString: string = ''; + + if (visited == 'on') { + typeString += 'visited'; + } + if (planned == 'on') { + if (typeString) { + typeString += ','; + } + typeString += 'planned'; + } else if (!visited && !planned) { + typeString = 'visited,planned'; + } + + const include_collections = event.url.searchParams.get('include_collections') || 'false'; + const order_by = event.url.searchParams.get('order_by') || 'updated_at'; + const order_direction = event.url.searchParams.get('order_direction') || 'asc'; + const page = event.url.searchParams.get('page') || '1'; + let initialFetch = await fetch( - `${serverEndpoint}/api/adventures/filtered?types=visited,planned&include_collections=false`, + `${serverEndpoint}/api/adventures/filtered?types=${typeString}&order_by=${order_by}&order_direction=${order_direction}&include_collections=${include_collections}&page=${page}`, { headers: { Cookie: `${event.cookies.get('auth')}` @@ -31,8 +52,7 @@ export const load = (async (event) => { } else { let res = await initialFetch.json(); let visited = res.results as Adventure[]; - next = res.next; - previous = res.previous; + count = res.count; adventures = [...adventures, ...visited]; } @@ -40,8 +60,6 @@ export const load = (async (event) => { return { props: { adventures, - next, - previous, count } }; @@ -384,203 +402,203 @@ export const actions: Actions = { let image_url = adventure.image; let link_url = adventure.link; return { image_url, link_url }; - }, - get: async (event) => { - if (!event.locals.user) { - } - - const formData = await event.request.formData(); - const visited = formData.get('visited'); - const planned = formData.get('planned'); - - let include_collections = formData.get('include_collections') as string; - - if (include_collections) { - include_collections = 'true'; - } else { - include_collections = 'false'; - } - - const order_direction = formData.get('order_direction') as string; - const order_by = formData.get('order_by') as string; - - console.log(order_direction, order_by); - - let adventures: Adventure[] = []; - - if (!event.locals.user) { - return { - status: 401, - body: { message: 'Unauthorized' } - }; - } - - let filterString = ''; - if (visited) { - filterString += 'visited'; - } - if (planned) { - if (filterString) { - filterString += ','; - } - filterString += 'planned'; - } - if (!filterString) { - filterString = ''; - } - - let next = null; - let previous = null; - let count = 0; - - console.log(filterString); - - let visitedFetch = await fetch( - `${serverEndpoint}/api/adventures/filtered?types=${filterString}&order_by=${order_by}&order_direction=${order_direction}&include_collections=${include_collections}`, - { - headers: { - Cookie: `${event.cookies.get('auth')}` - } - } - ); - if (!visitedFetch.ok) { - console.error('Failed to fetch visited adventures'); - return redirect(302, '/login'); - } else { - let res = await visitedFetch.json(); - let visited = res.results as Adventure[]; - next = res.next; - previous = res.previous; - count = res.count; - adventures = [...adventures, ...visited]; - console.log(next, previous, count); - } - - return { - adventures, - next, - previous, - count - }; - }, - changePage: async (event) => { - const formData = await event.request.formData(); - const next = formData.get('next') as string; - const previous = formData.get('previous') as string; - const page = formData.get('page') as string; - - if (!event.locals.user) { - return { - status: 401, - body: { message: 'Unauthorized' } - }; - } - - if (!page) { - return { - status: 400, - body: { error: 'Missing required fields' } - }; - } - - // Start with the provided URL or default to the filtered adventures endpoint - let url: string = next || previous || '/api/adventures/filtered'; - - // Extract the path starting from '/api/adventures' - const apiIndex = url.indexOf('/api/adventures'); - if (apiIndex !== -1) { - url = url.slice(apiIndex); - } else { - url = '/api/adventures/filtered'; - } - - // 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}`; - } - - const fullUrl = `${serverEndpoint}${url}`; - console.log(fullUrl); - console.log(serverEndpoint); - - try { - const response = await fetch(fullUrl, { - 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, - page - } - }; - } catch (error) { - console.error('Error fetching data:', error); - return { - status: 500, - body: { error: 'Failed to fetch data' } - }; - } - }, - all: async (event) => { - if (!event.locals.user) { - return { - status: 401, - body: { message: 'Unauthorized' } - }; - } - - const formData = await event.request.formData(); - - let include_collections = formData.get('include_collections') as string; - - if (include_collections !== 'true' && include_collections !== 'false') { - include_collections = 'false'; - } - - let adventures: Adventure[] = []; - - let visitedFetch = await fetch( - `${serverEndpoint}/api/adventures/all/?include_collections=${include_collections}`, - { - headers: { - Cookie: `${event.cookies.get('auth')}`, - 'Content-Type': 'application/json' - } - } - ); - if (!visitedFetch.ok) { - console.error('Failed to fetch all adventures'); - return redirect(302, '/login'); - } else { - console.log('Fetched all adventures'); - let res = await visitedFetch.json(); - console.log(res); - adventures = res as Adventure[]; - } - - return { - adventures - }; } + // get: async (event) => { + // if (!event.locals.user) { + // } + + // const formData = await event.request.formData(); + // const visited = formData.get('visited'); + // const planned = formData.get('planned'); + + // let include_collections = formData.get('include_collections') as string; + + // if (include_collections) { + // include_collections = 'true'; + // } else { + // include_collections = 'false'; + // } + + // const order_direction = formData.get('order_direction') as string; + // const order_by = formData.get('order_by') as string; + + // console.log(order_direction, order_by); + + // let adventures: Adventure[] = []; + + // if (!event.locals.user) { + // return { + // status: 401, + // body: { message: 'Unauthorized' } + // }; + // } + + // let filterString = ''; + // if (visited) { + // filterString += 'visited'; + // } + // if (planned) { + // if (filterString) { + // filterString += ','; + // } + // filterString += 'planned'; + // } + // if (!filterString) { + // filterString = ''; + // } + + // let next = null; + // let previous = null; + // let count = 0; + + // console.log(filterString); + + // let visitedFetch = await fetch( + // `${serverEndpoint}/api/adventures/filtered?types=${filterString}&order_by=${order_by}&order_direction=${order_direction}&include_collections=${include_collections}`, + // { + // headers: { + // Cookie: `${event.cookies.get('auth')}` + // } + // } + // ); + // if (!visitedFetch.ok) { + // console.error('Failed to fetch visited adventures'); + // return redirect(302, '/login'); + // } else { + // let res = await visitedFetch.json(); + // let visited = res.results as Adventure[]; + // next = res.next; + // previous = res.previous; + // count = res.count; + // adventures = [...adventures, ...visited]; + // console.log(next, previous, count); + // } + + // return { + // adventures, + // next, + // previous, + // count + // }; + // }, + // changePage: async (event) => { + // const formData = await event.request.formData(); + // const next = formData.get('next') as string; + // const previous = formData.get('previous') as string; + // const page = formData.get('page') as string; + + // if (!event.locals.user) { + // return { + // status: 401, + // body: { message: 'Unauthorized' } + // }; + // } + + // if (!page) { + // return { + // status: 400, + // body: { error: 'Missing required fields' } + // }; + // } + + // // Start with the provided URL or default to the filtered adventures endpoint + // let url: string = next || previous || '/api/adventures/filtered'; + + // // Extract the path starting from '/api/adventures' + // const apiIndex = url.indexOf('/api/adventures'); + // if (apiIndex !== -1) { + // url = url.slice(apiIndex); + // } else { + // url = '/api/adventures/filtered'; + // } + + // // 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}`; + // } + + // const fullUrl = `${serverEndpoint}${url}`; + // console.log(fullUrl); + // console.log(serverEndpoint); + + // try { + // const response = await fetch(fullUrl, { + // 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, + // page + // } + // }; + // } catch (error) { + // console.error('Error fetching data:', error); + // return { + // status: 500, + // body: { error: 'Failed to fetch data' } + // }; + // } + // }, + // all: async (event) => { + // if (!event.locals.user) { + // return { + // status: 401, + // body: { message: 'Unauthorized' } + // }; + // } + + // const formData = await event.request.formData(); + + // let include_collections = formData.get('include_collections') as string; + + // if (include_collections !== 'true' && include_collections !== 'false') { + // include_collections = 'false'; + // } + + // let adventures: Adventure[] = []; + + // let visitedFetch = await fetch( + // `${serverEndpoint}/api/adventures/all/?include_collections=${include_collections}`, + // { + // headers: { + // Cookie: `${event.cookies.get('auth')}`, + // 'Content-Type': 'application/json' + // } + // } + // ); + // if (!visitedFetch.ok) { + // console.error('Failed to fetch all adventures'); + // return redirect(302, '/login'); + // } else { + // console.log('Fetched all adventures'); + // let res = await visitedFetch.json(); + // console.log(res); + // adventures = res as Adventure[]; + // } + + // return { + // adventures + // }; + // } }; diff --git a/frontend/src/routes/adventures/+page.svelte b/frontend/src/routes/adventures/+page.svelte index 9837da60..192bd58d 100644 --- a/frontend/src/routes/adventures/+page.svelte +++ b/frontend/src/routes/adventures/+page.svelte @@ -1,5 +1,7 @@