InvitedForm.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React, { useCallback, useState } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { useRouter } from 'next/router';
  4. import { apiv3Post } from '~/client/util/apiv3-client';
  5. import { useCurrentUser } from '../stores/context';
  6. export type InvitedFormProps = {
  7. invitedFormUsername: string,
  8. invitedFormName: string,
  9. }
  10. export const InvitedForm = (props: InvitedFormProps): JSX.Element => {
  11. const { t } = useTranslation();
  12. const router = useRouter();
  13. const { data: user } = useCurrentUser();
  14. const [loginErrors, setLoginErrors] = useState<Error[]>([]);
  15. const { invitedFormUsername, invitedFormName } = props;
  16. const submitHandler = useCallback(async(e) => {
  17. e.preventDefault();
  18. const formData = e.target.elements;
  19. const {
  20. 'invitedForm[name]': { value: name },
  21. 'invitedForm[password]': { value: password },
  22. 'invitedForm[username]': { value: username },
  23. } = formData;
  24. const invitedForm = {
  25. name,
  26. password,
  27. username,
  28. };
  29. try {
  30. const res = await apiv3Post('/invited', { invitedForm });
  31. const { redirectTo } = res.data;
  32. router.push(redirectTo);
  33. }
  34. catch (err) {
  35. setLoginErrors(err);
  36. }
  37. }, [router]);
  38. const formNotification = useCallback(() => {
  39. return (
  40. <>
  41. { loginErrors != null && loginErrors.length > 0 ? (
  42. <p className="alert alert-danger">
  43. { loginErrors.map((err, index) => {
  44. return <span key={index}>{ t(err.message) }<br/></span>;
  45. }) }
  46. </p>
  47. ) : (
  48. <p className="alert alert-success">
  49. <strong>{ t('invited.discription_heading') }</strong><br></br>
  50. <small>{ t('invited.discription') }</small>
  51. </p>
  52. ) }
  53. </>
  54. );
  55. }, [loginErrors, t]);
  56. if (user == null) {
  57. return <></>;
  58. }
  59. return (
  60. <div className="noLogin-dialog px-3 pb-3 mx-auto" id="noLogin-dialog">
  61. { formNotification() }
  62. <form role="form" onSubmit={submitHandler} id="invited-form">
  63. {/* Email Form */}
  64. <div className="input-group">
  65. <div className="input-group-prepend">
  66. <span className="input-group-text">
  67. <i className="icon-envelope"></i>
  68. </span>
  69. </div>
  70. <input
  71. type="text"
  72. className="form-control"
  73. disabled
  74. placeholder={t('Email')}
  75. name="invitedForm[email]"
  76. defaultValue={user.email}
  77. required
  78. />
  79. </div>
  80. {/* UserID Form */}
  81. <div className="input-group" id="input-group-username">
  82. <div className="input-group-prepend">
  83. <span className="input-group-text">
  84. <i className="icon-user"></i>
  85. </span>
  86. </div>
  87. <input
  88. type="text"
  89. className="form-control"
  90. placeholder={t('User ID')}
  91. name="invitedForm[username]"
  92. value={invitedFormUsername}
  93. required
  94. />
  95. </div>
  96. {/* Name Form */}
  97. <div className="input-group">
  98. <div className="input-group-prepend">
  99. <span className="input-group-text">
  100. <i className="icon-tag"></i>
  101. </span>
  102. </div>
  103. <input
  104. type="text"
  105. className="form-control"
  106. placeholder={t('Name')}
  107. name="invitedForm[name]"
  108. value={invitedFormName}
  109. required
  110. />
  111. </div>
  112. {/* Password Form */}
  113. <div className="input-group">
  114. <div className="input-group-prepend">
  115. <span className="input-group-text">
  116. <i className="icon-lock"></i>
  117. </span>
  118. </div>
  119. <input
  120. type="password"
  121. className="form-control"
  122. placeholder={t('Password')}
  123. name="invitedForm[password]"
  124. required
  125. minLength={6}
  126. />
  127. </div>
  128. {/* Create Button */}
  129. <div className="input-group justify-content-center d-flex mt-4">
  130. <button type="submit" className="btn btn-fill" id="register">
  131. <div className="eff"></div>
  132. <span className="btn-label"><i className="icon-user-follow"></i></span>
  133. <span className="btn-label-text">{t('Create')}</span>
  134. </button>
  135. </div>
  136. </form>
  137. <div className="input-group mt-4 d-flex justify-content-center">
  138. <a href="https://growi.org" className="link-growi-org">
  139. <span className="growi">GROWI</span>.<span className="org">ORG</span>
  140. </a>
  141. </div>
  142. </div>
  143. );
  144. };