AddUserModal.svelte 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { createEventDispatcher } from 'svelte';
  4. import { onMount, getContext } from 'svelte';
  5. import { addUser } from '$lib/apis/auths';
  6. import Modal from '../common/Modal.svelte';
  7. import { WEBUI_BASE_URL } from '$lib/constants';
  8. const i18n = getContext('i18n');
  9. const dispatch = createEventDispatcher();
  10. export let show = false;
  11. let loading = false;
  12. let tab = '';
  13. let inputFiles;
  14. let _user = {
  15. name: '',
  16. email: '',
  17. password: '',
  18. role: 'user'
  19. };
  20. $: if (show) {
  21. _user = {
  22. name: '',
  23. email: '',
  24. password: '',
  25. role: 'user'
  26. };
  27. }
  28. const submitHandler = async () => {
  29. const stopLoading = () => {
  30. dispatch('save');
  31. loading = false;
  32. };
  33. if (tab === '') {
  34. loading = true;
  35. const res = await addUser(
  36. localStorage.token,
  37. _user.name,
  38. _user.email,
  39. _user.password,
  40. _user.role
  41. ).catch((error) => {
  42. toast.error(error);
  43. });
  44. if (res) {
  45. stopLoading();
  46. show = false;
  47. }
  48. } else {
  49. if (inputFiles) {
  50. loading = true;
  51. const file = inputFiles[0];
  52. const reader = new FileReader();
  53. reader.onload = async (e) => {
  54. const csv = e.target.result;
  55. const rows = csv.split('\n');
  56. let userCount = 0;
  57. for (const [idx, row] of rows.entries()) {
  58. const columns = row.split(',').map((col) => col.trim());
  59. console.log(idx, columns);
  60. if (idx > 0) {
  61. if (
  62. columns.length === 4 &&
  63. ['admin', 'user', 'pending'].includes(columns[3].toLowerCase())
  64. ) {
  65. const res = await addUser(
  66. localStorage.token,
  67. columns[0],
  68. columns[1],
  69. columns[2],
  70. columns[3].toLowerCase()
  71. ).catch((error) => {
  72. toast.error(`Row ${idx + 1}: ${error}`);
  73. return null;
  74. });
  75. if (res) {
  76. userCount = userCount + 1;
  77. }
  78. } else {
  79. toast.error(`Row ${idx + 1}: invalid format.`);
  80. }
  81. }
  82. }
  83. toast.success(`Successfully imported ${userCount} users.`);
  84. inputFiles = null;
  85. const uploadInputElement = document.getElementById('upload-user-csv-input');
  86. if (uploadInputElement) {
  87. uploadInputElement.value = null;
  88. }
  89. stopLoading();
  90. };
  91. reader.readAsText(file);
  92. } else {
  93. toast.error($i18n.t('File not found.'));
  94. }
  95. }
  96. };
  97. </script>
  98. <Modal size="sm" bind:show>
  99. <div>
  100. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  101. <div class=" text-lg font-medium self-center">{$i18n.t('Add User')}</div>
  102. <button
  103. class="self-center"
  104. on:click={() => {
  105. show = false;
  106. }}
  107. >
  108. <svg
  109. xmlns="http://www.w3.org/2000/svg"
  110. viewBox="0 0 20 20"
  111. fill="currentColor"
  112. class="w-5 h-5"
  113. >
  114. <path
  115. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  116. />
  117. </svg>
  118. </button>
  119. </div>
  120. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  121. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  122. <form
  123. class="flex flex-col w-full"
  124. on:submit|preventDefault={() => {
  125. submitHandler();
  126. }}
  127. >
  128. <div class="flex text-center text-sm font-medium rounded-xl bg-transparent/10 p-1 mb-2">
  129. <button
  130. class="w-full rounded-lg p-1.5 {tab === '' ? 'bg-gray-50 dark:bg-gray-850' : ''}"
  131. type="button"
  132. on:click={() => {
  133. tab = '';
  134. }}>{$i18n.t('Form')}</button
  135. >
  136. <button
  137. class="w-full rounded-lg p-1 {tab === 'import' ? 'bg-gray-50 dark:bg-gray-850' : ''}"
  138. type="button"
  139. on:click={() => {
  140. tab = 'import';
  141. }}>{$i18n.t('CSV Import')}</button
  142. >
  143. </div>
  144. <div class="px-1">
  145. {#if tab === ''}
  146. <div class="flex flex-col w-full">
  147. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Role')}</div>
  148. <div class="flex-1">
  149. <select
  150. class="w-full capitalize rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 disabled:text-gray-500 dark:disabled:text-gray-500 outline-none"
  151. bind:value={_user.role}
  152. placeholder={$i18n.t('Enter Your Role')}
  153. required
  154. >
  155. <option value="pending"> {$i18n.t('pending')} </option>
  156. <option value="user"> {$i18n.t('user')} </option>
  157. <option value="admin"> {$i18n.t('admin')} </option>
  158. </select>
  159. </div>
  160. </div>
  161. <div class="flex flex-col w-full mt-2">
  162. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name')}</div>
  163. <div class="flex-1">
  164. <input
  165. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 disabled:text-gray-500 dark:disabled:text-gray-500 outline-none"
  166. type="text"
  167. bind:value={_user.name}
  168. placeholder={$i18n.t('Enter Your Full Name')}
  169. autocomplete="off"
  170. required
  171. />
  172. </div>
  173. </div>
  174. <hr class=" dark:border-gray-800 my-3 w-full" />
  175. <div class="flex flex-col w-full">
  176. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Email')}</div>
  177. <div class="flex-1">
  178. <input
  179. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 disabled:text-gray-500 dark:disabled:text-gray-500 outline-none"
  180. type="email"
  181. bind:value={_user.email}
  182. placeholder={$i18n.t('Enter Your Email')}
  183. autocomplete="off"
  184. required
  185. />
  186. </div>
  187. </div>
  188. <div class="flex flex-col w-full mt-2">
  189. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Password')}</div>
  190. <div class="flex-1">
  191. <input
  192. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 disabled:text-gray-500 dark:disabled:text-gray-500 outline-none"
  193. type="password"
  194. bind:value={_user.password}
  195. placeholder={$i18n.t('Enter Your Password')}
  196. autocomplete="off"
  197. />
  198. </div>
  199. </div>
  200. {:else if tab === 'import'}
  201. <div>
  202. <div class="mb-3 w-full">
  203. <input
  204. id="upload-user-csv-input"
  205. hidden
  206. bind:files={inputFiles}
  207. type="file"
  208. accept=".csv"
  209. />
  210. <button
  211. class="w-full text-sm font-medium py-3 bg-transparent hover:bg-gray-100 border border-dashed dark:border-gray-800 dark:hover:bg-gray-850 text-center rounded-xl"
  212. type="button"
  213. on:click={() => {
  214. document.getElementById('upload-user-csv-input')?.click();
  215. }}
  216. >
  217. {#if inputFiles}
  218. {inputFiles.length > 0 ? `${inputFiles.length}` : ''} document(s) selected.
  219. {:else}
  220. {$i18n.t('Click here to select a csv file.')}
  221. {/if}
  222. </button>
  223. </div>
  224. <div class=" text-xs text-gray-500">
  225. ⓘ {$i18n.t(
  226. 'Ensure your CSV file includes 4 columns in this order: Name, Email, Password, Role.'
  227. )}
  228. <a
  229. class="underline dark:text-gray-200"
  230. href="{WEBUI_BASE_URL}/static/user-import.csv"
  231. >
  232. {$i18n.t('Click here to download user import template file.')}
  233. </a>
  234. </div>
  235. </div>
  236. {/if}
  237. </div>
  238. <div class="flex justify-end pt-3 text-sm font-medium">
  239. <button
  240. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg flex flex-row space-x-1 items-center {loading
  241. ? ' cursor-not-allowed'
  242. : ''}"
  243. type="submit"
  244. disabled={loading}
  245. >
  246. {$i18n.t('Submit')}
  247. {#if loading}
  248. <div class="ml-2 self-center">
  249. <svg
  250. class=" w-4 h-4"
  251. viewBox="0 0 24 24"
  252. fill="currentColor"
  253. xmlns="http://www.w3.org/2000/svg"
  254. ><style>
  255. .spinner_ajPY {
  256. transform-origin: center;
  257. animation: spinner_AtaB 0.75s infinite linear;
  258. }
  259. @keyframes spinner_AtaB {
  260. 100% {
  261. transform: rotate(360deg);
  262. }
  263. }
  264. </style><path
  265. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  266. opacity=".25"
  267. /><path
  268. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  269. class="spinner_ajPY"
  270. /></svg
  271. >
  272. </div>
  273. {/if}
  274. </button>
  275. </div>
  276. </form>
  277. </div>
  278. </div>
  279. </div>
  280. </Modal>
  281. <style>
  282. input::-webkit-outer-spin-button,
  283. input::-webkit-inner-spin-button {
  284. /* display: none; <- Crashes Chrome on hover */
  285. -webkit-appearance: none;
  286. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  287. }
  288. .tabs::-webkit-scrollbar {
  289. display: none; /* for Chrome, Safari and Opera */
  290. }
  291. .tabs {
  292. -ms-overflow-style: none; /* IE and Edge */
  293. scrollbar-width: none; /* Firefox */
  294. }
  295. input[type='number'] {
  296. -moz-appearance: textfield; /* Firefox */
  297. }
  298. </style>