GetCaptcha.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, {
  21. useMemo,
  22. useCallback,
  23. useState,
  24. ReactElement,
  25. ReactEventHandler
  26. } from 'react'
  27. import { Form, Input, Radio, Button } from 'antd'
  28. import { throttle } from 'lodash'
  29. import { FormComponentProps } from 'antd/lib/form/Form'
  30. import {
  31. GetPassWordType,
  32. getPassWordTypeLocale,
  33. IGetgetCaptchaParams,
  34. IOperateStates,
  35. FindPwStep
  36. } from './types'
  37. import { getCaptchaforResetPassword } from 'containers/App/actions'
  38. import { useDispatch } from 'react-redux'
  39. const styles = require('./index.less')
  40. const GetCaptcha: React.FC<IOperateStates & FormComponentProps> = React.memo(
  41. ({
  42. form,
  43. ticket,
  44. token,
  45. type,
  46. setTicket,
  47. setToken,
  48. setType,
  49. setStep,
  50. step
  51. }) => {
  52. const [submitStatus, setSubmitStatus] = useState<boolean>(false)
  53. const dispatch = useDispatch()
  54. const formItemLayout = useMemo(
  55. () => ({
  56. labelCol: { span: 0 },
  57. wrapperCol: { span: 24 }
  58. }),
  59. ['nf']
  60. )
  61. const buttonItemLayout = useMemo(
  62. () => ({
  63. wrapperCol: { span: 24, offset: 0 }
  64. }),
  65. ['nf']
  66. )
  67. const handleFormLayoutChange = useCallback(
  68. (event) => {
  69. const typeValue: GetPassWordType = event.target.value
  70. setType(typeValue)
  71. form.setFieldsValue({ ticket: '' })
  72. },
  73. [type, setType]
  74. )
  75. const handleSubmit = useCallback(
  76. (e: React.MouseEvent<HTMLButtonElement>) => {
  77. e.preventDefault()
  78. form.validateFieldsAndScroll((err, values) => {
  79. if (!err) {
  80. const { type, ticket } = values
  81. setSubmitStatus(true)
  82. const params: IGetgetCaptchaParams = {
  83. type,
  84. ticket,
  85. resolve: (payload) => {
  86. if (payload && payload.length) {
  87. setToken(payload)
  88. setTicket(ticket)
  89. setSubmitStatus(false)
  90. }
  91. }
  92. }
  93. onGetCaptchaforResetPassword(params)
  94. }
  95. })
  96. },
  97. ['nf']
  98. )
  99. const goNext = useCallback(() => {
  100. if (token && token.length) {
  101. setStep(FindPwStep.RESET)
  102. }
  103. }, [step, setStep, token])
  104. const Buttons: ReactElement = useMemo(() => {
  105. return token && token.length ? (
  106. <Button
  107. type="primary"
  108. size="large"
  109. onClick={goNext}
  110. className={styles.next}
  111. >
  112. 下一步
  113. </Button>
  114. ) : (
  115. <Button
  116. type="primary"
  117. size="large"
  118. onClick={handleSubmit}
  119. disabled={submitStatus}
  120. loading={submitStatus}
  121. className={styles.submit}
  122. >
  123. 确定
  124. </Button>
  125. )
  126. }, [token, submitStatus])
  127. const Tips: ReactElement = useMemo(
  128. () =>
  129. token && token.length ? (
  130. <>
  131. 一封确认信已经发到
  132. {type === GetPassWordType.USERNAME ? (
  133. <>
  134. <b>{ticket}</b>
  135. <span> 所关联的邮箱</span>
  136. </>
  137. ) : (
  138. <b>{ticket}</b>
  139. )}
  140. ,请前往该邮箱获取验证码,然后点击下一步重置密码。
  141. </>
  142. ) : (
  143. <></>
  144. ),
  145. [ticket, type, token]
  146. )
  147. const matchTypeInput: ReactElement = useMemo(() => {
  148. const { getFieldDecorator } = form
  149. return type === GetPassWordType.USERNAME ? (
  150. <Form.Item label="" {...formItemLayout}>
  151. {getFieldDecorator('ticket', {
  152. rules: [
  153. {
  154. required: true,
  155. message: '请输入用户名'
  156. }
  157. ]
  158. })(<Input placeholder="请输入用户名" size="large" />)}
  159. </Form.Item>
  160. ) : (
  161. <Form.Item label="" {...formItemLayout}>
  162. {getFieldDecorator('ticket', {
  163. rules: [
  164. {
  165. type: 'email',
  166. message: '邮箱格式不正确'
  167. },
  168. {
  169. required: true,
  170. message: '请输入邮箱'
  171. }
  172. ]
  173. })(<Input placeholder="请输入邮箱" size="large" />)}
  174. </Form.Item>
  175. )
  176. }, [type, form])
  177. const onGetCaptchaforResetPassword = useCallback(
  178. (params: IGetgetCaptchaParams) => {
  179. dispatch(getCaptchaforResetPassword(params))
  180. },
  181. []
  182. )
  183. return (
  184. <Form className={styles.form}>
  185. <div className={styles.top}>
  186. <Form.Item label="" {...formItemLayout}>
  187. {form.getFieldDecorator('type', {
  188. initialValue: type,
  189. rules: [
  190. {
  191. required: true,
  192. message: '请选择找回方式'
  193. }
  194. ]
  195. })(
  196. <Radio.Group onChange={handleFormLayoutChange} size="large">
  197. <Radio value={`${GetPassWordType.EMAIL}`}>
  198. {getPassWordTypeLocale[GetPassWordType.EMAIL]}
  199. </Radio>
  200. <Radio value={`${GetPassWordType.USERNAME}`}>
  201. {getPassWordTypeLocale[GetPassWordType.USERNAME]}
  202. </Radio>
  203. </Radio.Group>
  204. )}
  205. </Form.Item>
  206. {matchTypeInput}
  207. </div>
  208. <div className={styles.bottom}>
  209. <p className={styles.tip}>{Tips}</p>
  210. <Form.Item {...buttonItemLayout}>{Buttons}</Form.Item>
  211. </div>
  212. </Form>
  213. )
  214. }
  215. )
  216. export default Form.create<FormComponentProps & IOperateStates>()(GetCaptcha)