InstallerForm.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import type { FormEventHandler } from 'react';
  2. import { memo, useCallback, useState } from 'react';
  3. import { Lang, AllLang } from '@growi/core';
  4. import { LoadingSpinner } from '@growi/ui/dist/components';
  5. import { useTranslation } from 'next-i18next';
  6. import { useRouter } from 'next/router';
  7. import { i18n as i18nConfig } from '^/config/next-i18next.config';
  8. import { apiv3Post } from '~/client/util/apiv3-client';
  9. import { toastError } from '~/client/util/toastr';
  10. import styles from './InstallerForm.module.scss';
  11. const moduleClass = styles['installer-form'] ?? '';
  12. const InstallerForm = memo((): JSX.Element => {
  13. const { t, i18n } = useTranslation();
  14. const router = useRouter();
  15. const isSupportedLang = AllLang.includes(i18n.language as Lang);
  16. const [isValidUserName, setValidUserName] = useState(true);
  17. const [isLoading, setIsLoading] = useState(false);
  18. const [currentLocale, setCurrentLocale] = useState(isSupportedLang ? i18n.language : Lang.en_US);
  19. const checkUserName = useCallback(async(event) => {
  20. const axios = require('axios').create({
  21. headers: {
  22. 'Content-Type': 'application/json',
  23. 'X-Requested-With': 'XMLHttpRequest',
  24. },
  25. responseType: 'json',
  26. });
  27. const res = await axios.get('/_api/v3/check-username', { params: { username: event.target.value } });
  28. setValidUserName(res.data.valid);
  29. }, []);
  30. const onClickLanguageItem = useCallback((locale) => {
  31. i18n.changeLanguage(locale);
  32. setCurrentLocale(locale);
  33. }, [i18n]);
  34. const submitHandler: FormEventHandler = useCallback(async(e: any) => {
  35. e.preventDefault();
  36. setIsLoading(true);
  37. const formData = e.target.elements;
  38. const {
  39. 'registerForm[username]': { value: username },
  40. 'registerForm[name]': { value: name },
  41. 'registerForm[email]': { value: email },
  42. 'registerForm[password]': { value: password },
  43. } = formData;
  44. const data = {
  45. registerForm: {
  46. username,
  47. name,
  48. email,
  49. password,
  50. 'app:globalLang': currentLocale,
  51. },
  52. };
  53. try {
  54. await apiv3Post('/installer', data);
  55. router.push('/');
  56. }
  57. catch (errs) {
  58. const err = errs[0];
  59. const code = err.code;
  60. setIsLoading(false);
  61. if (code === 'failed_to_login_after_install') {
  62. toastError(t('installer.failed_to_login_after_install'));
  63. setTimeout(() => { router.push('/login') }, 700); // Wait 700 ms to show toastr
  64. }
  65. toastError(t('installer.failed_to_install'));
  66. }
  67. }, [currentLocale, router, t]);
  68. const hasErrorClass = isValidUserName ? '' : ' has-error';
  69. const unavailableUserId = isValidUserName
  70. ? ''
  71. : <span><span className="material-symbols-outlined">block</span>{ t('installer.unavaliable_user_id') }</span>;
  72. return (
  73. <div data-testid="installerForm" className={`${moduleClass} nologin-dialog py-3 px-4 rounded-4 rounded-top-0 mx-auto${hasErrorClass}`}>
  74. <div className="row mt-3">
  75. <div className="col-md-12">
  76. <p className="alert alert-success">
  77. <strong>{ t('installer.create_initial_account') }</strong><br />
  78. <small>{ t('installer.initial_account_will_be_administrator_automatically') }</small>
  79. </p>
  80. </div>
  81. </div>
  82. <div className="row mt-2">
  83. <form role="form" id="register-form" className="ps-1" onSubmit={submitHandler}>
  84. <div className="dropdown mb-3">
  85. <div className="input-group dropdown-with-icon">
  86. <span className="p-2 text-white opacity-75">
  87. <span className="material-symbols-outlined">language</span>
  88. </span>
  89. <button
  90. type="button"
  91. className="btn btn-secondary dropdown-toggle form-control text-end rounded"
  92. id="dropdownLanguage"
  93. data-testid="dropdownLanguage"
  94. data-bs-toggle="dropdown"
  95. aria-haspopup="true"
  96. aria-expanded="true"
  97. >
  98. <span className="float-start">
  99. {t('meta.display_name')}
  100. </span>
  101. </button>
  102. <input
  103. type="hidden"
  104. name="registerForm[app:globalLang]"
  105. />
  106. <div className="dropdown-menu" aria-labelledby="dropdownLanguage">
  107. {
  108. i18nConfig.locales.map((locale) => {
  109. let fixedT;
  110. if (i18n != null) {
  111. fixedT = i18n.getFixedT(locale);
  112. i18n.loadLanguages(i18nConfig.locales);
  113. }
  114. return (
  115. <button
  116. key={locale}
  117. data-testid={`dropdownLanguageMenu-${locale}`}
  118. className="dropdown-item"
  119. type="button"
  120. onClick={() => { onClickLanguageItem(locale) }}
  121. >
  122. {fixedT?.('meta.display_name')}
  123. </button>
  124. );
  125. })
  126. }
  127. </div>
  128. </div>
  129. </div>
  130. <div className={`input-group mb-3${hasErrorClass}`}>
  131. <span className="p-2 text-white opacity-75">
  132. <span className="material-symbols-outlined">person</span>
  133. </span>
  134. <input
  135. data-testid="tiUsername"
  136. type="text"
  137. className="form-control rounded"
  138. placeholder={t('User ID')}
  139. name="registerForm[username]"
  140. // onBlur={checkUserName} // need not to check username before installation -- 2020.07.24 Yuki Takei
  141. required
  142. />
  143. </div>
  144. <p className="form-text">{ unavailableUserId }</p>
  145. <div className="input-group mb-3">
  146. <span className="p-2 text-white opacity-75">
  147. <span className="material-symbols-outlined">sell</span>
  148. </span>
  149. <input
  150. data-testid="tiName"
  151. type="text"
  152. className="form-control rounded"
  153. placeholder={t('Name')}
  154. name="registerForm[name]"
  155. required
  156. />
  157. </div>
  158. <div className="input-group mb-3">
  159. <span className="p-2 text-white opacity-75">
  160. <span className="material-symbols-outlined">mail</span>
  161. </span>
  162. <input
  163. data-testid="tiEmail"
  164. type="email"
  165. className="form-control rounded"
  166. placeholder={t('Email')}
  167. name="registerForm[email]"
  168. required
  169. />
  170. </div>
  171. <div className="input-group mb-3">
  172. <span className="p-2 text-white opacity-75">
  173. <span className="material-symbols-outlined">lock</span>
  174. </span>
  175. <input
  176. data-testid="tiPassword"
  177. type="password"
  178. className="form-control rounded"
  179. placeholder={t('Password')}
  180. name="registerForm[password]"
  181. required
  182. />
  183. </div>
  184. <div className="input-group mt-4 justify-content-center">
  185. <button
  186. data-testid="btnSubmit"
  187. type="submit"
  188. className="btn btn-secondary btn-register col-6 d-flex"
  189. disabled={isLoading}
  190. >
  191. <span>
  192. {isLoading ? (
  193. <LoadingSpinner />
  194. ) : (
  195. <span className="material-symbols-outlined">person_add</span>
  196. )}
  197. </span>
  198. <span className="flex-grow-1">{ t('Create') }</span>
  199. </button>
  200. </div>
  201. <div>
  202. <a href="https://growi.org" className="link-growi-org">
  203. <span className="growi">GROWI</span><span className="org">.org</span>
  204. </a>
  205. </div>
  206. </form>
  207. </div>
  208. </div>
  209. );
  210. });
  211. InstallerForm.displayName = 'InstallerForm';
  212. export default InstallerForm;