diff --git a/frontend/src/app.d.ts b/frontend/src/app.d.ts index f6fa3c9..9dcfa69 100644 --- a/frontend/src/app.d.ts +++ b/frontend/src/app.d.ts @@ -51,7 +51,6 @@ interface User { last_name: string | ''; password: string | ''; phone: string | ''; - notes: string | ''; address: string | ''; zip_code: string | ''; city: string | ''; @@ -60,11 +59,9 @@ interface User { role_id: number | -1; dateofbirth: string | ''; company: string | ''; - profile_picture: string | ''; - payment_status: number | -1; - membership: Membership; - bank_account: BankAccount; - licence: Licence; + membership: Membership | null; + bank_account: BankAccount | null; + licence: Licence | null; notes: string | ''; } @@ -80,9 +77,9 @@ interface Car { end_date: string | ''; color: string | ''; licence_plate: string | ''; - location: Location; - damages: Damage[] | []; - insurances: Insurance[] | []; + location: Location | null; + damages: Damage[] | null; + insurances: Insurance[] | null; notes: string | ''; } @@ -93,8 +90,11 @@ interface Location { interface Damage { id: number | -1; - opponent: User; + name: string | ''; + opponent: User | null; + driver_id: number | -1; insurance: Insurance | null; + date: string | ''; notes: string | ''; } diff --git a/frontend/src/lib/components/CarEditForm.svelte b/frontend/src/lib/components/CarEditForm.svelte index d933d79..5723967 100644 --- a/frontend/src/lib/components/CarEditForm.svelte +++ b/frontend/src/lib/components/CarEditForm.svelte @@ -5,8 +5,10 @@ import { applyAction, enhance } from '$app/forms'; import { hasPrivilige, receive, send } from '$lib/utils/helpers'; import { t } from 'svelte-i18n'; - import { defaultCar } from '$lib/utils/defaults'; + import { defaultDamage, defaultInsurance, defaultOpponent } from '$lib/utils/defaults'; import { PERMISSIONS } from '$lib/utils/constants'; + import Modal from './Modal.svelte'; + import UserEditForm from './UserEditForm.svelte'; const dispatch = createEventDispatcher(); @@ -16,19 +18,54 @@ /** @type {App.Locals['user'] } */ export let editor; - /** @type {App.Types['car'] | null} */ + /** @type {App.Locals['users'] } */ + export let users; + + /** @type {App.Types['car']} */ export let car; - console.log('Opening car modal with:', car); - $: car = car || { ...defaultCar() }; + $: console.log( + 'damage.opponent changed:', + car?.damages.map((d) => d.opponent) + ); + $: console.log( + 'damage.insurance changed:', + car?.damages.map((d) => d.insurance) + ); + // TODO: Remove when working + // $: if (car.damages.length > 0 && !car.damages.every((d) => d.insurance && d.opponent)) { + // car.damages = car.damages.map((damage) => ({ + // ...damage, + // insurance: damage.insurance ?? defaultInsurance(), + // opponent: damage.opponent ?? defaultOpponent() + // })); + // } + let initialized = false; // Prevents infinite loops + + // Ensure damages have default values once `car` is loaded + $: if (car && !initialized) { + car = { + ...car, + damages: + car.damages?.map((damage) => ({ + ...damage, + insurance: damage.insurance ?? defaultInsurance(), + opponent: damage.opponent ?? defaultOpponent() + })) || [] + }; + initialized = true; // Prevents re-running + } $: isLoading = car === undefined || editor === undefined; let isUpdating = false; let readonlyUser = !hasPrivilige(editor, PERMISSIONS.Update); + /** @type {number | null} */ + let editingUserIndex = null; + const TABS = ['car.car', 'insurance', 'car.damages']; let activeTab = TABS[0]; - /** @type {import('../../routes/auth/about/[id]/$types').SubmitFunction} */ + /** @type {import('@sveltejs/kit').SubmitFunction} */ const handleUpdate = async () => { isUpdating = true; return async ({ result }) => { @@ -47,7 +84,7 @@ {:else if editor && car}
- +

{car.id ? $t('car.edit') : $t('car.create')}

@@ -155,48 +192,357 @@ />
- {#each car.insurances as insurance} - - - - - - {/each} +
+ {#each car.insurances as insurance, index} + +
+ + {insurance.company ? insurance.company : ''} + {insurance.reference ? ' (' + insurance.reference + ')' : ''} + +
+ + + + + + {#if hasPrivilige(editor, PERMISSIONS.Delete)} + + {/if} +
+
+ {/each} +
+
+ {#if hasPrivilige(editor, PERMISSIONS.Create)} + + {/if} +
+
+
+
+ {#each car.damages as damage, index (damage.id)} + +
+ + + {damage.name} - + {damage.opponent.first_name} + {damage.opponent.last_name} + + +
+ + + u.role_id > 0) + .map((u) => ({ + value: u.id, + label: `${u.first_name} ${u.last_name}`, + color: '--subtext1' + })) || []} + bind:value={damage.driver_id} + readonly={readonlyUser} + /> +

{$t('user.opponent')}

+ + + + + + + + + + + + + + + + + + + + +
+ + + {#if damage.opponent?.first_name} + {damage.opponent.first_name} {damage.opponent.last_name} + {:else} + {$t('not_set')} + {/if} + + +
+ + + + + + + + + + + + + + + + + + + + + + + +
{$t('email')}{damage.opponent?.email || '-'}
{$t('phone')}{damage.opponent?.phone || '-'}
{$t('address')}{damage.opponent?.address || '-'}
{$t('city')}{damage.opponent?.city || '-'}
{$t('zip_code')}{damage.opponent?.zip_code || '-'}
+
+ +
+
+
+ + + + + + + {#if hasPrivilige(editor, PERMISSIONS.Delete)} + + {/if} +
+
+ {/each} +
+ {#if hasPrivilige(editor, PERMISSIONS.Create)} + + {/if}
{#if isUpdating} @@ -211,7 +557,81 @@ {/if} +{#if editingUserIndex !== null} + + (editingUserIndex = null)} + on:close={() => { + car.damages = car.damages; + editingUserIndex = null; + }} + /> + +{/if} +