Skip to content
Extraits de code Groupes Projets
Valider a60ada62 rédigé par Brieuc Dubois's avatar Brieuc Dubois
Parcourir les fichiers

Implement user edition in admin pane #55

parent f10fdc4d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
<script lang="ts">
import { t } from '$lib/services/i18n';
import type User from '$lib/types/user';
import { toastAlert, toastSuccess } from '$lib/utils/toasts';
import { Icon, PencilSquare, Trash } from 'svelte-hero-icons';
export let user: User;
let nickname = user.nickname;
let email = user.email;
let type = user.type.toString();
let is_active = user.is_active;
let inEdit = false;
async function onEdit() {
if (!inEdit) {
nickname = user.nickname;
email = user.email;
type = user.type.toString();
is_active = user.is_active;
inEdit = true;
return;
}
if (
nickname === user.nickname &&
email === user.email &&
type === user.type.toString() &&
is_active === user.is_active
) {
inEdit = false;
return;
}
const res = await user.patch({
nickname,
email,
type: parseInt(type),
is_active
});
if (res) {
inEdit = false;
toastSuccess('Successfully updated user');
return;
}
toastAlert('Failed to update user');
}
</script>
<tr>
<td>{user.id}</td>
<td>
{#if inEdit}
<input type="text" class="input" bind:value={nickname} />
{:else}
{user.nickname}
{/if}
</td>
<td>
{#if inEdit}
<input type="email" class="input" bind:value={email} />
{:else}
{user.email}
{/if}
</td>
<td>
{#if inEdit}
<select class="select select-sm select-bordered" bind:value={type}>
<option value="2">{$t('users.type.student')}</option>
<option value="1">{$t('users.type.tutor')}</option>
<option value="0">{$t('users.type.admin')}</option>
</select>
{:else if user.type === 0}
{$t('users.type.admin')}
{:else if user.type === 1}
{$t('users.type.tutor')}
{:else}
{$t('users.type.student')}
{/if}
</td>
<td>
{#if inEdit}
<input type="checkbox" class="checkbox" bind:checked={is_active} />
{:else if user.is_active}
<span>{$t('utils.bool.true')}</span>
{:else}
<span>{$t('utils.bool.false')}</span>
{/if}
</td>
<td class="py-3 px-6 flex justify-center">
<button on:click={onEdit}>
<Icon src={PencilSquare} class="w-5" />
</button>
<Icon
src={Trash}
class="w-5 hover:cursor-not-allowed stroke-gray-400 hover:text-secondaryHover"
/>
</td></tr
>
...@@ -151,6 +151,24 @@ export default class User { ...@@ -151,6 +151,24 @@ export default class User {
}); });
} }
async patch(data: any): Promise<boolean> {
const res = await patchUserAPI(this.id, data);
if (res) {
if (data.email) this._email = data.email;
if (data.nickname) this._nickname = data.nickname;
if (data.type) this._type = data.type;
if (data.availability) this._availability = BigInt(data.availability);
if (data.is_active) this._is_active = data.is_active;
if (data.ui_language) this._ui_language = data.ui_language;
if (data.home_language) this._home_language = data.home_language;
if (data.target_language) this._target_language = data.target_language;
if (data.birthdate) this._birthdate = data.birthdate;
if (data.gender) this._gender = data.gender;
if (data.calcum_link) this._calcom_link = data.calcom_link;
}
return res;
}
static find(user_id: number): User | undefined { static find(user_id: number): User | undefined {
return get(users).find((user) => user.id === user_id); return get(users).find((user) => user.id === user_id);
} }
......
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import Header from '$lib/components/header.svelte';
import User, { users } from '$lib/types/user'; import User, { users } from '$lib/types/user';
import { getUsersAPI } from '$lib/api/users'; import { getUsersAPI } from '$lib/api/users';
import { t } from '$lib/services/i18n'; import { t } from '$lib/services/i18n';
import { Icon, Trash } from 'svelte-hero-icons'; import { Icon, Trash } from 'svelte-hero-icons';
import UserItem from '$lib/components/users/userItem.svelte';
let ready = false; let ready = false;
...@@ -57,27 +57,7 @@ ...@@ -57,27 +57,7 @@
</thead> </thead>
<tbody> <tbody>
{#each $users as user (user.id)} {#each $users as user (user.id)}
<tr> <UserItem {user} />
<td>{user.id}</td>
<td>{user.nickname}</td>
<td>{user.email}</td>
<td>
{#if user.type === 0}
{$t('users.type.admin')}
{:else if user.type === 1}
{$t('users.type.tutor')}
{:else}
{$t('users.type.student')}
{/if}
</td>
<td>{$t('utils.bool.' + user.is_active)}</td>
<td class="py-3 px-6 flex justify-center">
<Icon
src={Trash}
class="w-5 hover:cursor-not-allowed stroke-gray-400 hover:text-secondaryHover"
/>
</td></tr
>
{/each} {/each}
</tbody> </tbody>
<tfoot class=""> <tfoot class="">
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter