wip
This commit is contained in:
@@ -30,8 +30,21 @@ export const actions = {
|
||||
updateUser: async ({ request, fetch, cookies, locals }) => {
|
||||
let formData = await request.formData();
|
||||
|
||||
const rawData = formDataToObject(formData);
|
||||
const processedData = processUserFormData(rawData);
|
||||
const rawFormData = formDataToObject(formData);
|
||||
/** @type {{object: Partial<App.Locals['user']>, confirm_password: string}} */
|
||||
const rawData = {
|
||||
object: /** @type {Partial<App.Locals['user']>} */ (rawFormData.object),
|
||||
confirm_password: rawFormData.confirm_password
|
||||
};
|
||||
// confirm password matches and is not empty. Otherwise set password to empty string
|
||||
if (
|
||||
rawData.object.password &&
|
||||
rawData.confirm_password &&
|
||||
(rawData.object.password != rawData.confirm_password || rawData.object.password.trim() == '')
|
||||
) {
|
||||
rawData.object.password = '';
|
||||
}
|
||||
const processedData = processUserFormData(rawData.object);
|
||||
|
||||
// const isCreating = !processedData.user.id || processedData.user.id === 0;
|
||||
// console.log('Is creating: ', isCreating);
|
||||
|
||||
@@ -38,15 +38,23 @@ export const actions = {
|
||||
let formData = await request.formData();
|
||||
|
||||
const rawFormData = formDataToObject(formData);
|
||||
/** @type {{object: Partial<App.Locals['user']>, confirm_password: string}} */
|
||||
/** @type {{object: App.Locals['user'], confirm_password: string}} */
|
||||
const rawData = {
|
||||
object: /** @type {Partial<App.Locals['user']>} */ (rawFormData.object),
|
||||
object: /** @type {App.Locals['user']} */ (rawFormData.object),
|
||||
confirm_password: rawFormData.confirm_password
|
||||
};
|
||||
const processedData = processUserFormData(rawData);
|
||||
// confirm password matches and is not empty. Otherwise set password to empty string
|
||||
if (
|
||||
rawData.object.password &&
|
||||
rawData.confirm_password &&
|
||||
(rawData.object.password != rawData.confirm_password || rawData.object.password.trim() == '')
|
||||
) {
|
||||
rawData.object.password = '';
|
||||
}
|
||||
const user = processUserFormData(rawData.object);
|
||||
|
||||
console.dir(processedData.user.membership);
|
||||
const isCreating = !processedData.user.id || processedData.user.id === 0;
|
||||
console.dir(user.membership);
|
||||
const isCreating = !user.id || user.id === 0;
|
||||
console.log('Is creating: ', isCreating);
|
||||
const apiURL = `${BASE_API_URI}/auth/users`;
|
||||
|
||||
@@ -58,7 +66,7 @@ export const actions = {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `jwt=${cookies.get('jwt')}`
|
||||
},
|
||||
body: JSON.stringify(processedData)
|
||||
body: JSON.stringify(user)
|
||||
};
|
||||
|
||||
const res = await fetch(apiURL, requestOptions);
|
||||
@@ -128,18 +136,13 @@ export const actions = {
|
||||
*/
|
||||
updateCar: async ({ request, fetch, cookies }) => {
|
||||
let formData = await request.formData();
|
||||
console.dir(formData);
|
||||
const rawCar = /**@type {Partial<App.Types['car']>} */ (formDataToObject(formData).object);
|
||||
const car = processCarFormData(rawCar);
|
||||
|
||||
const rawFormData = formDataToObject(formData);
|
||||
/** @type {{object: Partial<App.Types['car']>, confirm_password: string}} */
|
||||
const rawData = {
|
||||
object: /** @type {Partial<App.Types['car']>} */ (rawFormData.object),
|
||||
confirm_password: rawFormData.confirm_password
|
||||
};
|
||||
const processedData = processCarFormData(rawData);
|
||||
|
||||
const isCreating = !processedData.car.id || processedData.car.id === 0;
|
||||
const isCreating = !car.id || car.id === 0;
|
||||
console.log('Is creating: ', isCreating);
|
||||
console.log('sending: ', JSON.stringify(processedData.car));
|
||||
console.log('sending: ', JSON.stringify(car.damages));
|
||||
|
||||
const apiURL = `${BASE_API_URI}/auth/cars`;
|
||||
|
||||
@@ -151,7 +154,7 @@ export const actions = {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `jwt=${cookies.get('jwt')}`
|
||||
},
|
||||
body: JSON.stringify(processedData.car)
|
||||
body: JSON.stringify(car)
|
||||
};
|
||||
|
||||
const res = await fetch(apiURL, requestOptions);
|
||||
@@ -164,6 +167,8 @@ export const actions = {
|
||||
|
||||
const response = await res.json();
|
||||
console.log('Server success response:', response);
|
||||
console.log('Server opponent response:', response.damages[0]?.opponent);
|
||||
console.log('Server insurance response:', response.damages[0]?.insurance);
|
||||
throw redirect(303, `${base}/auth/admin/users`);
|
||||
},
|
||||
|
||||
@@ -179,12 +184,8 @@ export const actions = {
|
||||
let formData = await request.formData();
|
||||
|
||||
const rawFormData = formDataToObject(formData);
|
||||
/** @type {{object: Partial<App.Locals['user']>, confirm_password: string}} */
|
||||
const rawData = {
|
||||
object: /** @type {Partial<App.Locals['user']>} */ (rawFormData.object),
|
||||
confirm_password: rawFormData.confirm_password
|
||||
};
|
||||
const processedData = processUserFormData(rawData);
|
||||
/** @type {Partial<App.Locals['user']>} */
|
||||
const rawUser = /** @type {Partial<App.Locals['user']>} */ (rawFormData.object);
|
||||
|
||||
const apiURL = `${BASE_API_URI}/auth/users`;
|
||||
|
||||
@@ -196,7 +197,7 @@ export const actions = {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `jwt=${cookies.get('jwt')}`
|
||||
},
|
||||
body: JSON.stringify(processedData)
|
||||
body: JSON.stringify({ id: Number(rawUser.id) })
|
||||
};
|
||||
|
||||
const res = await fetch(apiURL, requestOptions);
|
||||
@@ -217,14 +218,15 @@ export const actions = {
|
||||
* @param request - The request object
|
||||
* @param fetch - Fetch object from sveltekit
|
||||
* @param cookies - SvelteKit's cookie object
|
||||
* @param locals - The local object, housing current subscription
|
||||
* @returns
|
||||
*/
|
||||
subscriptionDelete: async ({ request, fetch, cookies }) => {
|
||||
let formData = await request.formData();
|
||||
|
||||
const rawData = formDataToObject(formData);
|
||||
const processedData = processSubscriptionFormData(rawData);
|
||||
|
||||
/** @type {Partial<App.Types['subscription']>} */
|
||||
const subscription = rawData.object;
|
||||
|
||||
const apiURL = `${BASE_API_URI}/auth/subscriptions`;
|
||||
|
||||
@@ -236,7 +238,45 @@ export const actions = {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `jwt=${cookies.get('jwt')}`
|
||||
},
|
||||
body: JSON.stringify(processedData.subscription)
|
||||
body: JSON.stringify({ id: Number(subscription.id), name: subscription.name })
|
||||
};
|
||||
|
||||
const res = await fetch(apiURL, requestOptions);
|
||||
|
||||
if (!res.ok) {
|
||||
const response = await res.json();
|
||||
const errors = formatError(response.errors);
|
||||
return fail(400, { errors: errors });
|
||||
}
|
||||
|
||||
const response = await res.json();
|
||||
console.log('Server success response:', response);
|
||||
throw redirect(303, `${base}/auth/admin/users`);
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @param request - The request object
|
||||
* @param fetch - Fetch object from sveltekit
|
||||
* @param cookies - SvelteKit's cookie object
|
||||
* @returns
|
||||
*/
|
||||
carDelete: async ({ request, fetch, cookies }) => {
|
||||
let formData = await request.formData();
|
||||
console.dir(formData);
|
||||
const rawCar = formDataToObject(formData);
|
||||
|
||||
const apiURL = `${BASE_API_URI}/auth/cars`;
|
||||
console.log('sending delete request to', JSON.stringify({ id: Number(rawCar.object.id) }));
|
||||
/** @type {RequestInit} */
|
||||
const requestOptions = {
|
||||
method: 'DELETE',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Cookie: `jwt=${cookies.get('jwt')}`
|
||||
},
|
||||
body: JSON.stringify({ id: Number(rawCar.object.id) })
|
||||
};
|
||||
|
||||
const res = await fetch(apiURL, requestOptions);
|
||||
@@ -263,12 +303,9 @@ export const actions = {
|
||||
let formData = await request.formData();
|
||||
|
||||
const rawFormData = formDataToObject(formData);
|
||||
/** @type {{object: Partial<App.Types['subscription']>, confirm_password: string}} */
|
||||
const rawData = {
|
||||
object: /** @type {Partial<App.Types['subscription']>} */ (rawFormData.object),
|
||||
confirm_password: rawFormData.confirm_password
|
||||
};
|
||||
const processedData = processUserFormData(rawData);
|
||||
/** @type {App.Locals['user']} */
|
||||
const rawUser = /** @type {App.Locals['user']} */ (rawFormData.object);
|
||||
const processedData = processUserFormData(rawUser);
|
||||
console.dir(processedData);
|
||||
const apiURL = `${BASE_API_URI}/auth/users/activate`;
|
||||
|
||||
|
||||
@@ -145,7 +145,7 @@
|
||||
on:click={() => setActiveSection('subscriptions')}
|
||||
>
|
||||
<i class="fas fa-clipboard-list"></i>
|
||||
{$t('subscription.subscriptions')}
|
||||
{$t('subscriptions.subscriptions')}
|
||||
<span class="nav-badge">{subscriptions.length}</span>
|
||||
</button>
|
||||
</li>
|
||||
@@ -391,47 +391,51 @@
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="button-group">
|
||||
<button
|
||||
class="btn primary"
|
||||
on:click={() => {
|
||||
selected = user;
|
||||
}}
|
||||
>
|
||||
<i class="fas fa-edit"></i>
|
||||
{$t('edit')}
|
||||
</button>
|
||||
<form
|
||||
method="POST"
|
||||
action="?/userDelete"
|
||||
use:enhance={() => {
|
||||
return async ({ result }) => {
|
||||
if (result.type === 'success' || result.type === 'redirect') {
|
||||
await applyAction(result);
|
||||
}
|
||||
};
|
||||
}}
|
||||
on:submit|preventDefault={(/** @type {SubmitEvent} */ e) => {
|
||||
if (
|
||||
!confirm(
|
||||
$t('dialog.user_deletion', {
|
||||
values: {
|
||||
firstname: user.first_name || '',
|
||||
lastname: user.last_name || ''
|
||||
}
|
||||
})
|
||||
)
|
||||
) {
|
||||
e.preventDefault(); // Cancel form submission if user declines
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="user[id]" value={user.id} />
|
||||
<input type="hidden" name="user[last_name]" value={user.last_name} />
|
||||
<button class="btn danger" type="submit">
|
||||
<i class="fas fa-trash"></i>
|
||||
{$t('delete')}
|
||||
{#if hasPrivilige(user, PERMISSIONS.Update)}
|
||||
<button
|
||||
class="btn primary"
|
||||
on:click={() => {
|
||||
selected = user;
|
||||
}}
|
||||
>
|
||||
<i class="fas fa-edit"></i>
|
||||
{$t('edit')}
|
||||
</button>
|
||||
</form>
|
||||
{/if}
|
||||
{#if hasPrivilige(user, PERMISSIONS.Delete)}
|
||||
<form
|
||||
method="POST"
|
||||
action="?/userDelete"
|
||||
use:enhance={() => {
|
||||
return async ({ result }) => {
|
||||
if (result.type === 'success' || result.type === 'redirect') {
|
||||
await applyAction(result);
|
||||
}
|
||||
};
|
||||
}}
|
||||
on:submit|preventDefault={(/** @type {SubmitEvent} */ e) => {
|
||||
if (
|
||||
!confirm(
|
||||
$t('dialog.user_deletion', {
|
||||
values: {
|
||||
firstname: user.first_name || '',
|
||||
lastname: user.last_name || ''
|
||||
}
|
||||
})
|
||||
)
|
||||
) {
|
||||
e.preventDefault(); // Cancel form submission if user declines
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="user[id]" value={user.id} />
|
||||
<input type="hidden" name="user[last_name]" value={user.last_name} />
|
||||
<button class="btn danger" type="submit">
|
||||
<i class="fas fa-trash"></i>
|
||||
{$t('delete')}
|
||||
</button>
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
@@ -439,7 +443,7 @@
|
||||
</div>
|
||||
{:else if activeSection === 'subscriptions'}
|
||||
<div class="section-header">
|
||||
<h2>{$t('subscription.subscriptions')}</h2>
|
||||
<h2>{$t('subscriptions.subscriptions')}</h2>
|
||||
{#if hasPrivilige(user, PERMISSIONS.Super)}
|
||||
<button
|
||||
class="btn primary"
|
||||
@@ -468,7 +472,7 @@
|
||||
<table class="table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>{$t('subscription.monthly_fee')}</th>
|
||||
<th>{$t('subscriptions.monthly_fee')}</th>
|
||||
<td
|
||||
>{subscription.monthly_fee !== -1
|
||||
? subscription.monthly_fee + '€'
|
||||
@@ -476,7 +480,7 @@
|
||||
>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{$t('subscription.hourly_rate')}</th>
|
||||
<th>{$t('subscriptions.hourly_rate')}</th>
|
||||
<td
|
||||
>{subscription.hourly_rate !== -1
|
||||
? subscription.hourly_rate + '€'
|
||||
@@ -484,11 +488,11 @@
|
||||
>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{$t('subscription.included_hours_per_year')}</th>
|
||||
<th>{$t('subscriptions.included_hours_per_year')}</th>
|
||||
<td>{subscription.included_hours_per_year || 0}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{$t('subscription.included_hours_per_month')}</th>
|
||||
<th>{$t('subscriptions.included_hours_per_month')}</th>
|
||||
<td>{subscription.included_hours_per_month || 0}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
@@ -496,13 +500,13 @@
|
||||
<td>{subscription.details || '-'}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{$t('subscription.conditions')}</th>
|
||||
<th>{$t('subscriptions.conditions')}</th>
|
||||
<td>{subscription.conditions || '-'}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
{#if hasPrivilige(user, PERMISSIONS.Super)}
|
||||
<div class="button-group">
|
||||
<div class="button-group">
|
||||
{#if hasPrivilige(user, PERMISSIONS.Super)}
|
||||
<button
|
||||
class="btn primary"
|
||||
on:click={() => {
|
||||
@@ -527,18 +531,31 @@
|
||||
?.scrollTo({ top: 0, behavior: 'smooth' });
|
||||
await applyAction(result);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="subscription[id]" value={subscription.id} />
|
||||
<input type="hidden" name="subscription[name]" value={subscription.name} />
|
||||
<button class="btn danger" type="submit">
|
||||
<i class="fas fa-trash"></i>
|
||||
{$t('delete')}
|
||||
</button>
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
};
|
||||
}}
|
||||
on:submit|preventDefault={(/** @type {SubmitEvent} */ e) => {
|
||||
if (
|
||||
!confirm(
|
||||
$t('dialog.subscription_deletion', {
|
||||
values: {
|
||||
name: subscription.name || ''
|
||||
}
|
||||
})
|
||||
)
|
||||
) {
|
||||
e.preventDefault(); // Cancel form submission if user declines
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="subscription[id]" value={subscription.id} />
|
||||
<input type="hidden" name="subscription[name]" value={subscription.name} />
|
||||
<button class="btn danger" type="submit">
|
||||
<i class="fas fa-trash"></i>
|
||||
{$t('delete')}
|
||||
</button>
|
||||
</form>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
{/each}
|
||||
@@ -597,8 +614,8 @@
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
{#if hasPrivilige(user, PERMISSIONS.Update)}
|
||||
<div class="button-group">
|
||||
<div class="button-group">
|
||||
{#if hasPrivilige(user, PERMISSIONS.Update)}
|
||||
<button
|
||||
class="btn primary"
|
||||
on:click={() => {
|
||||
@@ -608,6 +625,8 @@
|
||||
<i class="fas fa-edit"></i>
|
||||
{$t('edit')}
|
||||
</button>
|
||||
{/if}
|
||||
{#if hasPrivilige(user, PERMISSIONS.Delete)}
|
||||
<form
|
||||
method="POST"
|
||||
action="?/carDelete"
|
||||
@@ -637,14 +656,14 @@
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input type="hidden" name="subscription[id]" value={car.id} />
|
||||
<input type="hidden" name="car[id]" value={car.id} />
|
||||
<button class="btn danger" type="submit">
|
||||
<i class="fas fa-trash"></i>
|
||||
{$t('delete')}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
{/each}
|
||||
@@ -705,7 +724,7 @@
|
||||
</Modal>
|
||||
{:else if selected && 'brand' in selected}
|
||||
<Modal on:close={close}>
|
||||
<CarEditForm {form} editor={user} car={selected} on:cancel={close} on:close={close} />
|
||||
<CarEditForm {form} editor={user} {users} car={selected} on:cancel={close} on:close={close} />
|
||||
</Modal>
|
||||
{/if}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user