Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<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
>