Skip to content

Commit

Permalink
feat: add pagination to previous/next month
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinwhisman committed Dec 24, 2023
1 parent 7fe0033 commit 1e6d126
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/components/MonthlyOverview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

<h1>{data.formattedDate}</h1>

<ul>
<li>
<a href={data.previous.link}>{data.previous.text}</a>
</li>
<li>
<a href={data.next.link}>{data.next.text}</a>
</li>
</ul>

<h2>Expenses</h2>
{#if data.expenses.length}
<ul>
Expand Down
33 changes: 33 additions & 0 deletions src/lib/monthly-pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export const getMontlyPagination = (year, month) => {
let previousYear = year;
let nextYear = year;
let previousMonth = month;
let nextMonth = month + 2;

if (previousMonth <= 0) {
previousYear -= 1;
previousMonth = 12;
}

if (nextMonth > 12) {
nextYear += 1;
nextMonth = 1;
}

return {
previous: {
link: `/overview/${previousYear}/${previousMonth}`,
text: new Date(previousYear, previousMonth - 1, 1).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
}),
},
next: {
link: `/overview/${nextYear}/${nextMonth}`,
text: new Date(nextYear, nextMonth - 1, 1).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
}),
},
};
};
4 changes: 4 additions & 0 deletions src/routes/overview/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMonthlyOverview } from '$lib/monthly-overview.js';
import { getMontlyPagination } from '$lib/monthly-pagination.js';
import { fail } from '@sveltejs/kit';

export const load = async ({ locals: { supabase } }) => {
Expand All @@ -10,11 +11,14 @@ export const load = async ({ locals: { supabase } }) => {
year: 'numeric',
month: 'short',
});
const { previous, next } = getMontlyPagination(year, month);
const monthlyOverview = await getMonthlyOverview(supabase, year, month);

return {
...monthlyOverview,
formattedDate,
previous,
next,
};
} catch (error) {
return fail(500, { message: 'Server error. Try again later.', success: false });
Expand Down
4 changes: 4 additions & 0 deletions src/routes/overview/[year]/[month]/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getMonthlyOverview } from '$lib/monthly-overview.js';
import { getMontlyPagination } from '$lib/monthly-pagination.js';
import { fail } from '@sveltejs/kit';

export const load = async ({ params: { year, month }, locals: { supabase } }) => {
Expand All @@ -10,11 +11,14 @@ export const load = async ({ params: { year, month }, locals: { supabase } }) =>
year: 'numeric',
month: 'short',
});
const { previous, next } = getMontlyPagination(numericYear, numericMonth);
const monthlyOverview = await getMonthlyOverview(supabase, numericYear, numericMonth);

return {
...monthlyOverview,
formattedDate,
previous,
next,
};
} catch (error) {
return fail(500, { message: 'Server error. Try again later.', success: false });
Expand Down

0 comments on commit 1e6d126

Please sign in to comment.