create-user-form.svelte 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <script lang="ts">
  2. import { api } from '@api';
  3. import { createEventDispatcher } from 'svelte';
  4. import {
  5. notificationController,
  6. NotificationType
  7. } from '../shared-components/notification/notification';
  8. let error: string;
  9. let success: string;
  10. let password = '';
  11. let confirmPassowrd = '';
  12. let canCreateUser = false;
  13. let isCreatingUser = false;
  14. $: {
  15. if (password !== confirmPassowrd && confirmPassowrd.length > 0) {
  16. error = 'Password does not match';
  17. canCreateUser = false;
  18. } else {
  19. error = '';
  20. canCreateUser = true;
  21. }
  22. }
  23. const dispatch = createEventDispatcher();
  24. async function registerUser(event: SubmitEvent) {
  25. if (canCreateUser && !isCreatingUser) {
  26. isCreatingUser = true;
  27. error = '';
  28. const formElement = event.target as HTMLFormElement;
  29. const form = new FormData(formElement);
  30. const email = form.get('email');
  31. const password = form.get('password');
  32. const firstName = form.get('firstName');
  33. const lastName = form.get('lastName');
  34. try {
  35. const { status } = await api.userApi.createUser({
  36. email: String(email),
  37. password: String(password),
  38. firstName: String(firstName),
  39. lastName: String(lastName)
  40. });
  41. if (status === 201) {
  42. success = 'New user created';
  43. dispatch('user-created');
  44. isCreatingUser = false;
  45. return;
  46. } else {
  47. error = 'Error create user account';
  48. isCreatingUser = false;
  49. }
  50. } catch (e) {
  51. error = 'Error create user account';
  52. isCreatingUser = false;
  53. console.log('[ERROR] registerUser', e);
  54. notificationController.show({
  55. message: `Error create new user, check console for more detail`,
  56. type: NotificationType.Error
  57. });
  58. }
  59. }
  60. }
  61. </script>
  62. <div
  63. class="border bg-immich-bg dark:bg-immich-dark-gray dark:border-immich-dark-gray p-4 shadow-sm w-[500px] max-w-[95vw] rounded-3xl py-8 dark:text-immich-dark-fg"
  64. >
  65. <div class="flex flex-col place-items-center place-content-center gap-4 px-4">
  66. <img
  67. class="text-center"
  68. src="/immich-logo.svg"
  69. height="100"
  70. width="100"
  71. alt="immich-logo"
  72. draggable="false"
  73. />
  74. <h1 class="text-2xl text-immich-primary dark:text-immich-dark-primary font-medium">
  75. Create new user
  76. </h1>
  77. <p
  78. class="text-sm border rounded-md p-4 font-mono text-gray-600 dark:border-immich-dark-bg dark:text-gray-300"
  79. >
  80. Please provide your user with the password, they will have to change it on their first sign
  81. in.
  82. </p>
  83. </div>
  84. <form on:submit|preventDefault={registerUser} autocomplete="off">
  85. <div class="m-4 flex flex-col gap-2">
  86. <label class="immich-form-label" for="email">Email</label>
  87. <input class="immich-form-input" id="email" name="email" type="email" required />
  88. </div>
  89. <div class="m-4 flex flex-col gap-2">
  90. <label class="immich-form-label" for="password">Password</label>
  91. <input
  92. class="immich-form-input"
  93. id="password"
  94. name="password"
  95. type="password"
  96. required
  97. bind:value={password}
  98. />
  99. </div>
  100. <div class="m-4 flex flex-col gap-2">
  101. <label class="immich-form-label" for="confirmPassword">Confirm Password</label>
  102. <input
  103. class="immich-form-input"
  104. id="confirmPassword"
  105. name="password"
  106. type="password"
  107. required
  108. bind:value={confirmPassowrd}
  109. />
  110. </div>
  111. <div class="m-4 flex flex-col gap-2">
  112. <label class="immich-form-label" for="firstName">First Name</label>
  113. <input class="immich-form-input" id="firstName" name="firstName" type="text" required />
  114. </div>
  115. <div class="m-4 flex flex-col gap-2">
  116. <label class="immich-form-label" for="lastName">Last Name</label>
  117. <input class="immich-form-input" id="lastName" name="lastName" type="text" required />
  118. </div>
  119. {#if error}
  120. <p class="text-red-400 ml-4 text-sm">{error}</p>
  121. {/if}
  122. {#if success}
  123. <p class="text-immich-primary ml-4 text-sm">{success}</p>
  124. {/if}
  125. <div class="flex w-full">
  126. <button
  127. type="submit"
  128. class="m-4 bg-immich-primary dark:bg-immich-dark-primary hover:bg-immich-primary/75 dark:hover:bg-immich-dark-primary/80 px-6 py-3 text-white dark:text-immich-dark-gray rounded-full shadow-md w-full font-medium"
  129. disabled={isCreatingUser}
  130. >Create
  131. </button>
  132. </div>
  133. </form>
  134. </div>