AddUserModal.svelte 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 (columns.length === 4 && ['admin', 'user', 'pending'].includes(columns[3])) {
  62. const res = await addUser(
  63. localStorage.token,
  64. columns[0],
  65. columns[1],
  66. columns[2],
  67. columns[3]
  68. ).catch((error) => {
  69. toast.error(`Row ${idx + 1}: ${error}`);
  70. return null;
  71. });
  72. if (res) {
  73. userCount = userCount + 1;
  74. }
  75. } else {
  76. toast.error(`Row ${idx + 1}: invalid format.`);
  77. }
  78. }
  79. }
  80. toast.success(`Successfully imported ${userCount} users.`);
  81. inputFiles = null;
  82. const uploadInputElement = document.getElementById('upload-user-csv-input');
  83. if (uploadInputElement) {
  84. uploadInputElement.value = null;
  85. }
  86. stopLoading();
  87. };
  88. reader.readAsText(file);
  89. } else {
  90. toast.error($i18n.t('File not found.'));
  91. }
  92. }
  93. };
  94. </script>
  95. <Modal size="sm" bind:show>
  96. <div>
  97. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
  98. <div class=" text-lg font-medium self-center">{$i18n.t('Add User')}</div>
  99. <button
  100. class="self-center"
  101. on:click={() => {
  102. show = false;
  103. }}
  104. >
  105. <svg
  106. xmlns="http://www.w3.org/2000/svg"
  107. viewBox="0 0 20 20"
  108. fill="currentColor"
  109. class="w-5 h-5"
  110. >
  111. <path
  112. 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"
  113. />
  114. </svg>
  115. </button>
  116. </div>
  117. <div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
  118. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  119. <form
  120. class="flex flex-col w-full"
  121. on:submit|preventDefault={() => {
  122. submitHandler();
  123. }}
  124. >
  125. <div class="flex text-center text-sm font-medium rounded-xl bg-transparent/10 p-1 mb-2">
  126. <button
  127. class="w-full rounded-lg p-1.5 {tab === '' ? 'bg-gray-50 dark:bg-gray-850' : ''}"
  128. type="button"
  129. on:click={() => {
  130. tab = '';
  131. }}>Form</button
  132. >
  133. <button
  134. class="w-full rounded-lg p-1 {tab === 'import' ? 'bg-gray-50 dark:bg-gray-850' : ''}"
  135. type="button"
  136. on:click={() => {
  137. tab = 'import';
  138. }}>CSV Import</button
  139. >
  140. </div>
  141. <div class="px-1">
  142. {#if tab === ''}
  143. <div class="flex flex-col w-full">
  144. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Role')}</div>
  145. <div class="flex-1">
  146. <select
  147. 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"
  148. bind:value={_user.role}
  149. placeholder={$i18n.t('Enter Your Role')}
  150. required
  151. >
  152. <option value="pending"> pending </option>
  153. <option value="user"> user </option>
  154. <option value="admin"> admin </option>
  155. </select>
  156. </div>
  157. </div>
  158. <div class="flex flex-col w-full mt-2">
  159. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name')}</div>
  160. <div class="flex-1">
  161. <input
  162. 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"
  163. type="text"
  164. bind:value={_user.name}
  165. placeholder={$i18n.t('Enter Your Full Name')}
  166. autocomplete="off"
  167. required
  168. />
  169. </div>
  170. </div>
  171. <hr class=" dark:border-gray-800 my-3 w-full" />
  172. <div class="flex flex-col w-full">
  173. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Email')}</div>
  174. <div class="flex-1">
  175. <input
  176. 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"
  177. type="email"
  178. bind:value={_user.email}
  179. placeholder={$i18n.t('Enter Your Email')}
  180. autocomplete="off"
  181. required
  182. />
  183. </div>
  184. </div>
  185. <div class="flex flex-col w-full mt-2">
  186. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Password')}</div>
  187. <div class="flex-1">
  188. <input
  189. 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"
  190. type="password"
  191. bind:value={_user.password}
  192. placeholder={$i18n.t('Enter Your Password')}
  193. autocomplete="off"
  194. />
  195. </div>
  196. </div>
  197. {:else if tab === 'import'}
  198. <div>
  199. <div class="mb-3 w-full">
  200. <input
  201. id="upload-user-csv-input"
  202. hidden
  203. bind:files={inputFiles}
  204. type="file"
  205. accept=".csv"
  206. />
  207. <button
  208. 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"
  209. type="button"
  210. on:click={() => {
  211. document.getElementById('upload-user-csv-input')?.click();
  212. }}
  213. >
  214. {#if inputFiles}
  215. {inputFiles.length > 0 ? `${inputFiles.length}` : ''} document(s) selected.
  216. {:else}
  217. {$i18n.t('Click here to select a csv file.')}
  218. {/if}
  219. </button>
  220. </div>
  221. <div class=" text-xs text-gray-500">
  222. ⓘ {$i18n.t(
  223. 'Ensure your CSV file includes 4 columns in this order: Name, Email, Password, Role.'
  224. )}
  225. <a
  226. class="underline dark:text-gray-200"
  227. href="{WEBUI_BASE_URL}/static/user-import.csv"
  228. >
  229. Click here to download user import template file.
  230. </a>
  231. </div>
  232. </div>
  233. {/if}
  234. </div>
  235. <div class="flex justify-end pt-3 text-sm font-medium">
  236. <button
  237. 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
  238. ? ' cursor-not-allowed'
  239. : ''}"
  240. type="submit"
  241. disabled={loading}
  242. >
  243. {$i18n.t('Submit')}
  244. {#if loading}
  245. <div class="ml-2 self-center">
  246. <svg
  247. class=" w-4 h-4"
  248. viewBox="0 0 24 24"
  249. fill="currentColor"
  250. xmlns="http://www.w3.org/2000/svg"
  251. ><style>
  252. .spinner_ajPY {
  253. transform-origin: center;
  254. animation: spinner_AtaB 0.75s infinite linear;
  255. }
  256. @keyframes spinner_AtaB {
  257. 100% {
  258. transform: rotate(360deg);
  259. }
  260. }
  261. </style><path
  262. 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"
  263. opacity=".25"
  264. /><path
  265. 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"
  266. class="spinner_ajPY"
  267. /></svg
  268. >
  269. </div>
  270. {/if}
  271. </button>
  272. </div>
  273. </form>
  274. </div>
  275. </div>
  276. </div>
  277. </Modal>
  278. <style>
  279. input::-webkit-outer-spin-button,
  280. input::-webkit-inner-spin-button {
  281. /* display: none; <- Crashes Chrome on hover */
  282. -webkit-appearance: none;
  283. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  284. }
  285. .tabs::-webkit-scrollbar {
  286. display: none; /* for Chrome, Safari and Opera */
  287. }
  288. .tabs {
  289. -ms-overflow-style: none; /* IE and Edge */
  290. scrollbar-width: none; /* Firefox */
  291. }
  292. input[type='number'] {
  293. -moz-appearance: textfield; /* Firefox */
  294. }
  295. </style>