diff --git a/src/components/MonthlyOverview.svelte b/src/components/MonthlyOverview.svelte index 91feb3c..9ab864a 100644 --- a/src/components/MonthlyOverview.svelte +++ b/src/components/MonthlyOverview.svelte @@ -23,6 +23,7 @@ {:else}

No expenses found.

{/if} +Add Expense

Income

{#if data.income.length} diff --git a/src/components/inputs/AmountInput.svelte b/src/components/inputs/AmountInput.svelte new file mode 100644 index 0000000..4aa6ba9 --- /dev/null +++ b/src/components/inputs/AmountInput.svelte @@ -0,0 +1,17 @@ + + +
+ + +
diff --git a/src/components/inputs/CategoryInput.svelte b/src/components/inputs/CategoryInput.svelte new file mode 100644 index 0000000..f69d0c2 --- /dev/null +++ b/src/components/inputs/CategoryInput.svelte @@ -0,0 +1,47 @@ + + +
+ Category +
+
+ + +
+ {#each categories as { category }, index} +
+ + +
+ {/each} +
+
+{#if selectedCategory === 'NEW_CATEGORY'} +
+ + +
+{/if} diff --git a/src/components/inputs/DateInputs.svelte b/src/components/inputs/DateInputs.svelte new file mode 100644 index 0000000..ad7ade5 --- /dev/null +++ b/src/components/inputs/DateInputs.svelte @@ -0,0 +1,52 @@ + + +
+ Date +
+
+ + +
+
+ + +
+
+ + +
+
+
diff --git a/src/components/inputs/DescriptionInput.svelte b/src/components/inputs/DescriptionInput.svelte new file mode 100644 index 0000000..164afc3 --- /dev/null +++ b/src/components/inputs/DescriptionInput.svelte @@ -0,0 +1,8 @@ + + +
+ + +
diff --git a/src/lib/format-inputs.js b/src/lib/format-inputs.js new file mode 100644 index 0000000..1426e2d --- /dev/null +++ b/src/lib/format-inputs.js @@ -0,0 +1,9 @@ +export const formatAmount = (value) => Number(value.toString().replace(/[^0-9|.]/g, '')); + +export const formatDate = (year, month, day) => { + const numericYear = Number(year); + const numericMonth = Number(month) - 1; + const numericDay = Number(day); + + return new Date(numericYear, numericMonth, numericDay); +}; diff --git a/src/routes/expense/+page.server.js b/src/routes/expense/+page.server.js new file mode 100644 index 0000000..20241dc --- /dev/null +++ b/src/routes/expense/+page.server.js @@ -0,0 +1,48 @@ +import { fail, redirect } from '@sveltejs/kit'; +import { formatAmount, formatDate } from '$lib/format-inputs.js'; + +export const load = async ({ locals: { supabase } }) => { + try { + const { data: categories } = await supabase + .from('expense_categories') + .select('category') + .order('category'); + + return { + categories, + }; + } catch (error) { + return fail(500, { message: 'Server error. Try again later.', success: false }); + } +}; + +export const actions = { + default: async ({ request, locals: { supabase } }) => { + const formData = await request.formData(); + const amount = formatAmount(formData.get('amount')); + + const selectedCategory = formData.get('category'); + const newCategory = formData.get('new-category'); + const category = newCategory ?? selectedCategory; + + const description = formData.get('description') || category; + + const year = formData.get('year'); + const month = formData.get('month'); + const day = formData.get('day'); + const date = formatDate(year, month, day); + + const { + data: { user }, + } = await supabase.auth.getUser(); + const { error } = await supabase + .from('expenses') + .insert({ user_id: user.id, date, category, description, amount }); + + if (error) { + return fail(500, { message: 'Server error. Try again later.', success: false }); + } + + throw redirect(303, '/overview'); + }, +}; diff --git a/src/routes/expense/+page.svelte b/src/routes/expense/+page.svelte new file mode 100644 index 0000000..db1d8b3 --- /dev/null +++ b/src/routes/expense/+page.svelte @@ -0,0 +1,20 @@ + + +

Add Expense

+ +
+ + + + +
+ +
+