Skip to content

Commit

Permalink
feat: fetch data for user's overview page (#4)
Browse files Browse the repository at this point in the history
* refactor: get email on server side

* feat: get data for the current month
  • Loading branch information
dustinwhisman authored Dec 23, 2023
1 parent cb2f1ab commit 5892317
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
10 changes: 0 additions & 10 deletions src/routes/auth/confirm/+page.js

This file was deleted.

11 changes: 10 additions & 1 deletion src/routes/auth/confirm/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ export const actions = {
return fail(500, { message: 'Server error. Try again later.', success: false, email });
}

throw redirect(303, '/');
throw redirect(303, '/overview');
},
};

export const load = async ({ url }) => {
const email = url.searchParams.get('email') ?? '';
if (!email) {
throw redirect(303, '/auth/login');
}

return { email };
};
37 changes: 37 additions & 0 deletions src/routes/overview/+page.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { fail } from '@sveltejs/kit';

export const load = async ({ locals: { supabase } }) => {
try {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const { data: expenses } = await supabase
.from('expenses')
.select('id,date,category,description,amount')
.gte('date', new Date(year, month, 1).toDateString());

const { data: income } = await supabase
.from('income')
.select('id,date,category,description,amount')
.gte('date', new Date(year, month, 1).toDateString());

const { data: savings } = await supabase
.from('savings')
.select('id,date,category,description,amount')
.gte('date', new Date(year, month, 1).toDateString());

const { data: debt } = await supabase
.from('debt')
.select('id,date,category,description,amount,interest_rate,minimum_payment')
.gte('date', new Date(year, month, 1).toDateString());

return {
expenses,
income,
savings,
debt,
};
} catch (error) {
return fail(500, { message: 'Server error. Try again later.', success: false });
}
};
49 changes: 49 additions & 0 deletions src/routes/overview/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<script>
export let data;
</script>

<h1>Overview</h1>

<h2>Expenses</h2>
{#if data.expenses.length}
<ul>
{#each data.expenses as expense}
<li>{expense.description}: {expense.amount}</li>
{/each}
</ul>
{:else}
<p>No expenses found.</p>
{/if}

<h2>Income</h2>
{#if data.income.length}
<ul>
{#each data.income as incomeEntry}
<li>{incomeEntry.description}: {incomeEntry.amount}</li>
{/each}
</ul>
{:else}
<p>No income found.</p>
{/if}

<h2>Savings</h2>
{#if data.savings.length}
<ul>
{#each data.savings as savingsEntry}
<li>{savingsEntry.description}: {savingsEntry.amount}</li>
{/each}
</ul>
{:else}
<p>No savings found.</p>
{/if}

<h2>Debt</h2>
{#if data.debt.length}
<ul>
{#each data.debt as debtEntry}
<li>{debtEntry.description}: {debtEntry.amount}</li>
{/each}
</ul>
{:else}
<p>No debt found.</p>
{/if}

0 comments on commit 5892317

Please sign in to comment.