index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import React, { useState } from 'react'
  2. import { CEditAvatar } from '../../components/EditAvatar'
  3. import { CustomInput } from '../../components/Input'
  4. import { message, Button } from 'antd'
  5. import { connect } from 'react-redux'
  6. import { actionChangePassword } from '../../actions/query/aboutMeQuery'
  7. import { actionClearPromiseForName } from '../../actions/types/promiseTypes'
  8. import { Basic, ConstructorModal } from '../../helpers'
  9. import { SpoilerButton } from '../../components/comment/SpoilerButton'
  10. import { actionUserUpdateTypeSaga } from '../../actions/typeSaga/myDataTypesSaga'
  11. import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons'
  12. export const EditAccount = ({ showModalEdit }) => {
  13. return (
  14. <Button
  15. type="primary"
  16. style={{
  17. fontWeight: '600',
  18. fontSize: '15px',
  19. transition: '.3s',
  20. boxShadow: '0 5px 15px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22)',
  21. }}
  22. onClick={showModalEdit}
  23. >
  24. Edit Setting
  25. </Button>
  26. )
  27. }
  28. const EditSetting = ({
  29. info,
  30. myId,
  31. onSaveUserUpsert,
  32. onSaveNewPassword,
  33. changePassword,
  34. }) => {
  35. const [state, setState] = useState(info)
  36. const [changePass, setChangePass] = useState(changePassword)
  37. const [isModalVisibleEdit, setIsModalVisibleEdit] = useState(false)
  38. const [checked1, setChecked1] = useState(true)
  39. const [checked2, setChecked2] = useState(true)
  40. const showModalEdit = () => {
  41. setIsModalVisibleEdit(true)
  42. }
  43. const handleCancelEdit = () => {
  44. setIsModalVisibleEdit(false)
  45. }
  46. const onChangeLogin = (event) =>
  47. setState({
  48. ...state,
  49. login: event.target.value,
  50. })
  51. const onChangeNick = (event) =>
  52. setState({
  53. ...state,
  54. nick: event.target.value,
  55. })
  56. const onChangePassLogin = (event) =>
  57. setChangePass({
  58. ...changePass,
  59. login: event.target.value,
  60. })
  61. const onChangePassPassword = (event) =>
  62. setChangePass({
  63. ...changePass,
  64. password: event.target.value,
  65. })
  66. const onChangePassNew = (event) =>
  67. setChangePass({
  68. ...changePass,
  69. newPassword: event.target.value,
  70. })
  71. const saveChange = () => {
  72. onSaveUserUpsert(state, myId) &&
  73. message.success(`Successfully saved changed new login!`) &&
  74. setIsModalVisibleEdit(false)
  75. }
  76. const saveNewPassword = () => {
  77. onSaveNewPassword(
  78. changePass.login,
  79. changePass.password,
  80. changePass.newPassword,
  81. )
  82. if (changePassword?.payload == null && changePass.login != info?.login) {
  83. message.error(`You entered wrong login/password! Try again!`)
  84. } else {
  85. message.success(`Successfully saved changed new password!`) &&
  86. setIsModalVisibleEdit(false)
  87. }
  88. }
  89. console.log('save pass', changePass?.login != info?.login)
  90. return (
  91. <div>
  92. <EditAccount showModalEdit={showModalEdit} />
  93. <ConstructorModal
  94. title={'Edit setting'}
  95. isModalVisible={isModalVisibleEdit}
  96. setIsModalVisible={setIsModalVisibleEdit}
  97. handleCancel={handleCancelEdit}
  98. >
  99. <h3> Edit avatar </h3>
  100. <SpoilerButton text={'Change avatar'} style={{ width: '100%' }}>
  101. <CEditAvatar setIsModalVisibleEdit={setIsModalVisibleEdit} />
  102. </SpoilerButton>
  103. <h3> Edit login </h3>
  104. <SpoilerButton text={'Change login'} style={{ width: '100%' }}>
  105. <div
  106. style={{
  107. display: 'flex',
  108. alignItems: 'center',
  109. flexDirection: 'row',
  110. }}
  111. >
  112. <CustomInput
  113. state={state?.login || ''}
  114. onChangeText={onChangeLogin}
  115. />
  116. <Button
  117. size="large"
  118. style={{ margin: '10px' }}
  119. onClick={saveChange}
  120. disabled={state?.login && state?.login.length >= 5 ? false : true}
  121. type="primary"
  122. >
  123. {' '}
  124. Save login{' '}
  125. </Button>
  126. </div>
  127. </SpoilerButton>
  128. <h3> Edit nick </h3>
  129. <SpoilerButton text={'Change nick'} style={{ width: '100%' }}>
  130. <div
  131. style={{
  132. display: 'flex',
  133. alignItems: 'center',
  134. flexDirection: 'row',
  135. }}
  136. >
  137. <CustomInput
  138. state={state?.nick || ''}
  139. onChangeText={onChangeNick}
  140. />
  141. <Button
  142. style={{ margin: '10px' }}
  143. disabled={state?.nick && state?.nick.length >= 5 ? false : true}
  144. onClick={saveChange}
  145. size="large"
  146. type="primary"
  147. >
  148. Save nick
  149. </Button>
  150. </div>
  151. </SpoilerButton>
  152. <h3> Edit password </h3>
  153. <SpoilerButton text={'Change password'} style={{ width: '100%' }}>
  154. <h3> Login</h3>
  155. <div
  156. style={{
  157. display: 'flex',
  158. alignItems: 'center',
  159. flexDirection: 'row',
  160. }}
  161. >
  162. <CustomInput
  163. state={changePass?.login}
  164. onChangeText={onChangePassLogin}
  165. type={true}
  166. />
  167. </div>
  168. <h3> Old password</h3>
  169. <div
  170. style={{
  171. display: 'flex',
  172. alignItems: 'center',
  173. flexDirection: 'row',
  174. }}
  175. >
  176. <CustomInput
  177. state={changePass?.password}
  178. onChangeText={onChangePassPassword}
  179. checked={checked1}
  180. />
  181. {checked1 ? (
  182. <EyeInvisibleOutlined
  183. onClick={() => setChecked1(!checked1)}
  184. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  185. />
  186. ) : (
  187. <EyeOutlined
  188. onClick={() => setChecked1(!checked1)}
  189. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  190. />
  191. )}
  192. </div>
  193. <h3> New password</h3>
  194. <div
  195. style={{
  196. display: 'flex',
  197. alignItems: 'center',
  198. flexDirection: 'row',
  199. }}
  200. >
  201. <CustomInput
  202. state={changePass?.newPassword || ''}
  203. checked={checked2}
  204. onChangeText={onChangePassNew}
  205. />
  206. {checked2 ? (
  207. <EyeInvisibleOutlined
  208. onClick={() => setChecked2(!checked2)}
  209. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  210. />
  211. ) : (
  212. <EyeOutlined
  213. onClick={() => setChecked2(!checked2)}
  214. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  215. />
  216. )}
  217. </div>
  218. <div
  219. style={{
  220. display: 'flex',
  221. alignItems: 'center',
  222. flexDirection: 'column',
  223. }}
  224. >
  225. <Button
  226. style={{ margin: '10px' }}
  227. disabled={
  228. changePass?.login &&
  229. changePass?.password &&
  230. /(?=.*[0-9])(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z])[0-9!@#$%^&*a-zA-Z]{8,}/g.test(
  231. changePass?.newPassword,
  232. )
  233. ? false
  234. : true
  235. }
  236. onClick={saveNewPassword}
  237. size="x-large"
  238. type="primary"
  239. >
  240. Save new password
  241. </Button>
  242. </div>
  243. </SpoilerButton>
  244. </ConstructorModal>
  245. </div>
  246. )
  247. }
  248. export const CEditSetting = connect(
  249. (state) => ({
  250. myId: state?.auth.payload.sub?.id,
  251. info: state?.myData?.aboutMe,
  252. changePassword: state.promise?.newPassword,
  253. }),
  254. {
  255. onSaveUserUpsert: actionUserUpdateTypeSaga,
  256. onSaveNewPassword: actionChangePassword,
  257. onClearPromise: actionClearPromiseForName,
  258. },
  259. )(EditSetting)