EditUserModal.svelte 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { createEventDispatcher } from 'svelte';
  5. import { onMount, getContext } from 'svelte';
  6. import { updateUserById, getUserGroupsById } from '$lib/apis/users';
  7. import Modal from '$lib/components/common/Modal.svelte';
  8. import localizedFormat from 'dayjs/plugin/localizedFormat';
  9. import XMark from '$lib/components/icons/XMark.svelte';
  10. import SensitiveInput from '$lib/components/common/SensitiveInput.svelte';
  11. const i18n = getContext('i18n');
  12. const dispatch = createEventDispatcher();
  13. dayjs.extend(localizedFormat);
  14. export let show = false;
  15. export let selectedUser;
  16. export let sessionUser;
  17. let _user = {
  18. profile_image_url: '',
  19. role: 'pending',
  20. name: '',
  21. email: '',
  22. password: ''
  23. };
  24. let userGroups: any[] | null = null;
  25. const submitHandler = async () => {
  26. const res = await updateUserById(localStorage.token, selectedUser.id, _user).catch((error) => {
  27. toast.error(`${error}`);
  28. });
  29. if (res) {
  30. dispatch('save');
  31. show = false;
  32. }
  33. };
  34. const loadUserGroups = async () => {
  35. if (!selectedUser?.id) return;
  36. userGroups = null;
  37. userGroups = await getUserGroupsById(localStorage.token, selectedUser.id).catch((error) => {
  38. toast.error(`${error}`);
  39. return null;
  40. });
  41. };
  42. onMount(() => {
  43. if (selectedUser) {
  44. _user = selectedUser;
  45. _user.password = '';
  46. loadUserGroups();
  47. }
  48. });
  49. </script>
  50. <Modal size="sm" bind:show>
  51. <div>
  52. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  53. <div class=" text-lg font-medium self-center">{$i18n.t('Edit User')}</div>
  54. <button
  55. class="self-center"
  56. on:click={() => {
  57. show = false;
  58. }}
  59. >
  60. <XMark className={'size-5'} />
  61. </button>
  62. </div>
  63. <div class="flex flex-col md:flex-row w-full md:space-x-4 dark:text-gray-200">
  64. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  65. <form
  66. class="flex flex-col w-full"
  67. on:submit|preventDefault={() => {
  68. submitHandler();
  69. }}
  70. >
  71. <div class=" flex items-center rounded-md px-5 py-2 w-full">
  72. <div class=" self-center mr-5">
  73. <img
  74. src={selectedUser.profile_image_url}
  75. class=" max-w-[55px] object-cover rounded-full"
  76. alt="User profile"
  77. />
  78. </div>
  79. <div>
  80. <div class=" self-center capitalize font-semibold">{selectedUser.name}</div>
  81. <div class="text-xs text-gray-500">
  82. {$i18n.t('Created at')}
  83. {dayjs(selectedUser.created_at * 1000).format('LL')}
  84. </div>
  85. </div>
  86. </div>
  87. <div class=" px-5 pt-3 pb-5">
  88. <div class=" flex flex-col space-y-1.5">
  89. <div class="flex flex-col w-full">
  90. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Role')}</div>
  91. <div class="flex-1">
  92. <select
  93. class="w-full dark:bg-gray-900 text-sm bg-transparent disabled:text-gray-500 dark:disabled:text-gray-500 outline-hidden"
  94. bind:value={_user.role}
  95. disabled={_user.id == sessionUser.id}
  96. required
  97. >
  98. <option value="admin">{$i18n.t('Admin')}</option>
  99. <option value="user">{$i18n.t('User')}</option>
  100. <option value="pending">{$i18n.t('Pending')}</option>
  101. </select>
  102. </div>
  103. </div>
  104. {#if userGroups}
  105. <div class="flex flex-col w-full text-sm">
  106. <div class="mb-1 text-xs text-gray-500">{$i18n.t('User Groups')}</div>
  107. {#if userGroups.length}
  108. <div class="flex flex-wrap gap-1 my-0.5 -mx-1">
  109. {#each userGroups as userGroup}
  110. <span class="px-2 py-0.5 rounded-full bg-gray-100 dark:bg-gray-850 text-xs">
  111. {userGroup.name}
  112. </span>
  113. {/each}
  114. </div>
  115. {:else}
  116. <span>-</span>
  117. {/if}
  118. </div>
  119. {/if}
  120. <div class="flex flex-col w-full">
  121. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Email')}</div>
  122. <div class="flex-1">
  123. <input
  124. class="w-full text-sm bg-transparent disabled:text-gray-500 dark:disabled:text-gray-500 outline-hidden"
  125. type="email"
  126. bind:value={_user.email}
  127. placeholder={$i18n.t('Enter Your Email')}
  128. autocomplete="off"
  129. required
  130. />
  131. </div>
  132. </div>
  133. <div class="flex flex-col w-full">
  134. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name')}</div>
  135. <div class="flex-1">
  136. <input
  137. class="w-full text-sm bg-transparent outline-hidden"
  138. type="text"
  139. bind:value={_user.name}
  140. placeholder={$i18n.t('Enter Your Name')}
  141. autocomplete="off"
  142. required
  143. />
  144. </div>
  145. </div>
  146. <div class="flex flex-col w-full">
  147. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('New Password')}</div>
  148. <div class="flex-1">
  149. <SensitiveInput
  150. class="w-full text-sm bg-transparent outline-hidden"
  151. type="password"
  152. placeholder={$i18n.t('Enter New Password')}
  153. bind:value={_user.password}
  154. autocomplete="new-password"
  155. required={false}
  156. />
  157. </div>
  158. </div>
  159. </div>
  160. <div class="flex justify-end pt-3 text-sm font-medium">
  161. <button
  162. class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
  163. type="submit"
  164. >
  165. {$i18n.t('Save')}
  166. </button>
  167. </div>
  168. </div>
  169. </form>
  170. </div>
  171. </div>
  172. </div>
  173. </Modal>
  174. <style>
  175. input::-webkit-outer-spin-button,
  176. input::-webkit-inner-spin-button {
  177. /* display: none; <- Crashes Chrome on hover */
  178. -webkit-appearance: none;
  179. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  180. }
  181. .tabs::-webkit-scrollbar {
  182. display: none; /* for Chrome, Safari and Opera */
  183. }
  184. .tabs {
  185. -ms-overflow-style: none; /* IE and Edge */
  186. scrollbar-width: none; /* Firefox */
  187. }
  188. input[type='number'] {
  189. -moz-appearance: textfield; /* Firefox */
  190. }
  191. </style>