SignUp.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { useRef, useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Alert, Button, Result, message } from "antd";
  4. import type { ProFormInstance } from "@ant-design/pro-components";
  5. import {
  6. CheckCard,
  7. ProForm,
  8. ProFormCheckbox,
  9. ProFormText,
  10. StepsForm,
  11. } from "@ant-design/pro-components";
  12. import { post } from "../../request";
  13. import { IInviteRequest, IInviteResponse } from "../api/Auth";
  14. import { dashboardBasePath } from "../../utils";
  15. import { get as getUiLang } from "../../locales";
  16. interface IFormData {
  17. email: string;
  18. lang: string;
  19. }
  20. const SingUpWidget = () => {
  21. const intl = useIntl();
  22. const formRef = useRef<ProFormInstance>();
  23. const [error, setError] = useState<string>();
  24. const [agree, setAgree] = useState(false);
  25. return (
  26. <StepsForm<IFormData>
  27. formRef={formRef}
  28. onFinish={async (values: IFormData) => {}}
  29. formProps={{
  30. validateMessages: {
  31. required: "此项为必填项",
  32. },
  33. }}
  34. submitter={{
  35. render(props, dom) {
  36. if (props.step === 0) {
  37. return (
  38. <Button
  39. type="primary"
  40. disabled={!agree}
  41. onClick={() => props.onSubmit?.()}
  42. >
  43. {"下一步"}
  44. </Button>
  45. );
  46. } else if (props.step === 2) {
  47. return <></>;
  48. } else {
  49. return dom;
  50. }
  51. },
  52. }}
  53. >
  54. <StepsForm.StepForm<{
  55. name: string;
  56. }>
  57. name="welcome"
  58. title="注册"
  59. stepProps={{
  60. description: "注册wikipali教育版",
  61. }}
  62. onFinish={async () => {
  63. return true;
  64. }}
  65. >
  66. <Alert
  67. message={"wikipali的阅读,字典,搜索功能无需注册就能使用。"}
  68. style={{ marginBottom: 8 }}
  69. />
  70. <CheckCard.Group
  71. onChange={(value) => {
  72. console.log("value", value);
  73. }}
  74. defaultValue="A"
  75. style={{ width: "100%" }}
  76. size="small"
  77. >
  78. <CheckCard
  79. title="未注册"
  80. description={
  81. <div>
  82. <div>✅经文阅读</div>
  83. <div>✅字典</div>
  84. <div>✅经文搜索</div>
  85. <div>❌课程</div>
  86. <div>❌翻译</div>
  87. </div>
  88. }
  89. value="B"
  90. disabled
  91. />
  92. <CheckCard
  93. title={intl.formatMessage({ id: "labels.software.edition.basic" })}
  94. description={
  95. <div>
  96. <div>✅逐词解析</div>
  97. <div>✅翻译</div>
  98. <div>✅参加课程</div>
  99. <div>❌公开发布译文和逐词解析</div>
  100. <div>❌公开发布用户字典和术语</div>
  101. <div>❌建立课程</div>
  102. <div>❌建立群组</div>
  103. </div>
  104. }
  105. value="A"
  106. />
  107. <CheckCard
  108. title={intl.formatMessage({ id: "labels.software.edition.pro" })}
  109. disabled
  110. description={
  111. <div>
  112. <div>✅逐词解析</div>
  113. <div>✅翻译</div>
  114. <div>✅参加课程</div>
  115. <div>✅公开发布译文和逐词解析</div>
  116. <div>✅公开发布用户字典和术语</div>
  117. <div>✅建立课程</div>
  118. <div>✅建立群组</div>
  119. </div>
  120. }
  121. value="C"
  122. />
  123. </CheckCard.Group>
  124. <ProFormCheckbox.Group
  125. name="checkbox"
  126. layout="horizontal"
  127. options={["我已经了解教育版的功能限制"]}
  128. fieldProps={{
  129. onChange(checkedValue) {
  130. if (checkedValue.includes("我已经了解教育版的功能限制")) {
  131. setAgree(true);
  132. } else {
  133. setAgree(false);
  134. }
  135. },
  136. }}
  137. />
  138. </StepsForm.StepForm>
  139. <StepsForm.StepForm<{
  140. checkbox: string;
  141. }>
  142. name="checkbox"
  143. title="邮箱验证"
  144. stepProps={{
  145. description: "填入您的注册邮箱",
  146. }}
  147. onFinish={async () => {
  148. const values = formRef.current?.getFieldsValue();
  149. const url = `/v2/invite`;
  150. const data: IInviteRequest = {
  151. email: values.email,
  152. lang: getUiLang(),
  153. studio: "",
  154. dashboard: dashboardBasePath(),
  155. };
  156. console.info("api request", values);
  157. try {
  158. const res = await post<IInviteRequest, IInviteResponse>(url, data);
  159. console.debug("api response", res);
  160. if (res.ok) {
  161. message.success(intl.formatMessage({ id: "flashes.success" }));
  162. } else {
  163. setError(intl.formatMessage({ id: `error.${res.message}` }));
  164. }
  165. return res.ok;
  166. } catch (error) {
  167. setError(error as string);
  168. return false;
  169. }
  170. }}
  171. >
  172. {error ? <Alert type="error" message={error} /> : undefined}
  173. <ProForm.Group>
  174. <ProFormText
  175. width="md"
  176. name="email"
  177. required
  178. label={intl.formatMessage({ id: "forms.fields.email.label" })}
  179. rules={[
  180. {
  181. required: true,
  182. type: "email",
  183. },
  184. ]}
  185. />
  186. </ProForm.Group>
  187. </StepsForm.StepForm>
  188. <StepsForm.StepForm name="finish" title="完成注册">
  189. <Result
  190. status="success"
  191. title="验证码已经成功发送"
  192. subTitle="验证邮件已经发送到您的邮箱。请查收邮件,根据提示完成注册。"
  193. />
  194. </StepsForm.StepForm>
  195. </StepsForm>
  196. );
  197. };
  198. export default SingUpWidget;