diff --git a/my-app/src/lib/formatter.ts b/my-app/src/lib/formatter.ts index 9f1ebce..6b46084 100644 --- a/my-app/src/lib/formatter.ts +++ b/my-app/src/lib/formatter.ts @@ -1,3 +1,11 @@ +export function formatDateInput(dateString: string): string { + const date = new Date(Date.parse(dateString)); + const day = date.getDate().toString().padStart(2, '0'); + const month = (date.getMonth() + 1).toString().padStart(2, '0'); + const year = date.getFullYear(); + return `${year}-${month}-${day}`; +} + export function formatDateString(dateString: string): string { const date = new Date(Date.parse(dateString)); const day = date.getDate().toString().padStart(2, '0'); diff --git a/my-app/src/lib/server/api.ts b/my-app/src/lib/server/api.ts index 5225cf5..897e3ca 100644 --- a/my-app/src/lib/server/api.ts +++ b/my-app/src/lib/server/api.ts @@ -83,5 +83,5 @@ export const categoryService = { ? put(`category/${data.id}`, data, getAuthHeader(cookies)) : post('category', data, getAuthHeader(cookies)), get: (id: Id, cookies: Cookies) => get(`category/${id}`, getAuthHeader(cookies)), - list: (groupId: Id, cookies: Cookies) => get(`group/${groupId}/category`, getAuthHeader(cookies)) + list: (groupId: Id, cookies: Cookies) => get(`category/${groupId}`, getAuthHeader(cookies)) }; diff --git a/my-app/src/routes/api/categories/+server.ts b/my-app/src/routes/api/categories/+server.ts new file mode 100644 index 0000000..b05fb76 --- /dev/null +++ b/my-app/src/routes/api/categories/+server.ts @@ -0,0 +1,8 @@ +import { categoryService } from '$lib/server/api'; +import type { RequestHandler } from './$types'; + +export const GET: RequestHandler = async ({ url, cookies }) => { + const groupId = url.searchParams.get('groupId') || 0; + const body = await categoryService.list(+groupId, cookies); + return new Response(JSON.stringify(body)); +}; diff --git a/my-app/src/routes/budgets/details/[[id=integer]]/+page.svelte b/my-app/src/routes/budgets/details/[[id=integer]]/+page.svelte index 601db1e..f55bf5d 100644 --- a/my-app/src/routes/budgets/details/[[id=integer]]/+page.svelte +++ b/my-app/src/routes/budgets/details/[[id=integer]]/+page.svelte @@ -2,12 +2,17 @@ import { title } from '$lib'; import { onMount } from 'svelte'; import type { PageData } from './$types'; - import { formatMoney } from '$lib/formatter'; + import { formatDateInput, formatMoney } from '$lib/formatter'; export let data: PageData; const edit = data.budget.id !== 0; let timezoneOffset = 0; let suggestions: Map = new Map(); + let categories: Category[] = []; + + async function onGroupUpdate(groupId: number) { + await Promise.all([updateSuggestions(groupId), updateCategories(groupId)]); + } async function updateSuggestions(groupId: number) { const newSuggestions: Map = new Map(); @@ -23,6 +28,17 @@ suggestions = newSuggestions; } + async function updateCategories(groupId: number) { + if (groupId != 0) { + try { + const response = await fetch(`/api/categories?groupId=${groupId}`); + categories = await response.json(); + return; + } catch {} + } + categories = []; + } + function formatSuggestion({ description, amount }: Budget) { return `${description} (${formatMoney(amount)})`; } @@ -32,11 +48,12 @@ if (!budget) return; data.budget.amount = budget.amount; data.budget.description = budget.description; + data.budget.category_id = budget.category_id; } onMount(async () => { timezoneOffset = new Date().getTimezoneOffset(); - await updateSuggestions(data.budget.group_id); + await onGroupUpdate(data.budget.group_id); }); @@ -55,8 +72,9 @@ + {#each categories as category, i} + + {/each} + + {#if edit} diff --git a/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts b/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts index 2f10c94..0ece8b4 100644 --- a/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts +++ b/my-app/src/routes/categories/details/[[id=integer]]/+page.server.ts @@ -1,15 +1,17 @@ import { error } from '@sveltejs/kit'; import type { Actions } from './$types'; -import { categoryService } from '$lib/server/api'; +import { categoryService, groupService } from '$lib/server/api'; import type { PageServerLoad } from './$types'; -export const load: PageServerLoad = async ({ params }) => { - const category: Category = { id: 0, name: '' }; - const id = params.id; +export const load: PageServerLoad = async ({ params, url, cookies }) => { + const group_id = url.searchParams.get('group_id') || ''; + const id = Number(params.id) || 0; + let category: Category = { id: 0, group_id, name: '', description: '', strategy: '' }; if (id) { - // TODO: load real category + category = await categoryService.get(id, cookies); } - return { category }; + const groups: Group[] = await groupService.list(cookies); + return { category, groups }; }; export const actions: Actions = { @@ -17,12 +19,21 @@ export const actions: Actions = { const id = Number(params.id) || 0; const data = await request.formData(); const name = data.get('name')?.toString(); + const description = data.get('description')?.toString(); + const group_id = data.get('groupId')?.toString(); + const strategy = ''; if (!name) { throw error(400, 'Name is required'); } + if (!description) { + throw error(400, 'Description is required'); + } + if (!group_id) { + throw error(400, 'Group is required'); + } - const category: Category = { id, name }; + const category: Category = { id, group_id, name, description, strategy }; await categoryService.save(category, cookies); return { success: true }; } diff --git a/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte b/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte index eb8f53b..a218125 100644 --- a/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte +++ b/my-app/src/routes/categories/details/[[id=integer]]/+page.svelte @@ -1,5 +1,8 @@ @@ -9,10 +12,22 @@

Nueva Categoría

+ +
diff --git a/my-app/src/routes/groups/budgets/[[id=integer]]/+page.server.ts b/my-app/src/routes/groups/budgets/[[id=integer]]/+page.server.ts index 7baec5b..be65711 100644 --- a/my-app/src/routes/groups/budgets/[[id=integer]]/+page.server.ts +++ b/my-app/src/routes/groups/budgets/[[id=integer]]/+page.server.ts @@ -1,9 +1,10 @@ -import { budgetService, groupService } from '$lib/server/api'; +import { budgetService, categoryService, groupService } from '$lib/server/api'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ cookies, params }) => { const id = Number(params.id); const group: Group = await groupService.get(id, cookies); const budgets: Budget[] = await budgetService.list(id, cookies); - return { group, budgets }; + const categories: Category[] = await categoryService.list(id, cookies); + return { group, budgets, categories }; }; diff --git a/my-app/src/routes/groups/budgets/[[id=integer]]/+page.svelte b/my-app/src/routes/groups/budgets/[[id=integer]]/+page.svelte index 799449b..b2717a8 100644 --- a/my-app/src/routes/groups/budgets/[[id=integer]]/+page.svelte +++ b/my-app/src/routes/groups/budgets/[[id=integer]]/+page.svelte @@ -22,6 +22,14 @@ Nuevo presupuesto + +
+ Categorias: + {#each data.categories as category} + + {/each} +
+ {#each data?.budgets as budget}
@@ -51,4 +59,10 @@ .text-right { text-align: right; } + + .btn-sm { + font-size: small; + padding: 0.5em; + margin: 0.3em; + } diff --git a/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts b/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts index 8d095b8..e5bf3cd 100644 --- a/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts +++ b/my-app/src/routes/groups/movements/[id=integer]/+page.server.ts @@ -1,4 +1,4 @@ -import { budgetService, groupService, spendingService } from '$lib/server/api'; +import { budgetService, categoryService, groupService, spendingService } from '$lib/server/api'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ cookies, params }) => { @@ -6,5 +6,6 @@ export const load: PageServerLoad = async ({ cookies, params }) => { const group: Group = await groupService.get(id, cookies); const spendings: Spending[] = await spendingService.list(id, cookies); const budgets: Budget[] = await budgetService.list(id, cookies); - return { group, spendings, budgets }; + const categories: Category[] = await categoryService.list(id, cookies); + return { group, spendings, budgets, categories }; }; diff --git a/my-app/src/routes/groups/movements/[id=integer]/+page.svelte b/my-app/src/routes/groups/movements/[id=integer]/+page.svelte index 33ebf43..bd4cb0a 100644 --- a/my-app/src/routes/groups/movements/[id=integer]/+page.svelte +++ b/my-app/src/routes/groups/movements/[id=integer]/+page.svelte @@ -29,21 +29,29 @@
-
- -
-
Gastos
-

{formatMoney(totalSpendings)}

-
-
-
Balance
-

{formatMoney(balance)}

-
+
+
+ +
+
Gastos
+

{formatMoney(totalSpendings)}

+
+
+
Balance
+

{formatMoney(balance)}

+
+
+
+ Categorías: + {#each data.categories as category} + + {/each} +
{#each data.spendings as spending} @@ -64,4 +72,10 @@ .text-right { text-align: right; } + + .btn-sm { + font-size: small; + padding: 0.5em; + margin: 0.3em; + } diff --git a/my-app/src/types.d.ts b/my-app/src/types.d.ts index ed2135b..b1d6509 100644 --- a/my-app/src/types.d.ts +++ b/my-app/src/types.d.ts @@ -8,7 +8,10 @@ declare global { }; type Category = { id: Id; + group_id: string; // ? name: string; + description: string; + strategy: string; }; type Budget = { id: Id; diff --git a/roadmap b/roadmap index 0579779..bfc5e40 100644 --- a/roadmap +++ b/roadmap @@ -8,9 +8,9 @@ groups: [x] create [ ] details spendings: -[ ] list -[ ] details +[x] list +[x] details budget: -[ ] create +[x] create categories: -[ ] category \ No newline at end of file +[x] create \ No newline at end of file