index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. <div className='ModalEdit'>
  100. <p> Edit avatar </p>
  101. <SpoilerButton text={'Change avatar'}
  102. style={{ width: '100%' }}>
  103. <CEditAvatar
  104. setIsModalVisibleEdit={setIsModalVisibleEdit} />
  105. </SpoilerButton>
  106. <p> Edit login </p>
  107. <SpoilerButton text={'Change login'}
  108. style={{ width: '100%' }}>
  109. <div
  110. style={{
  111. display: 'flex',
  112. alignItems: 'center',
  113. flexDirection: 'row',
  114. }}
  115. >
  116. <CustomInput
  117. state={state?.login || ''}
  118. onChangeText={onChangeLogin}
  119. />
  120. <Button
  121. size="large"
  122. style={{ margin: '10px' }}
  123. onClick={saveChange}
  124. disabled={state?.login && state?.login.length >= 5 ? false : true}
  125. type="primary"
  126. >
  127. {' '}
  128. Save login{' '}
  129. </Button>
  130. </div>
  131. </SpoilerButton>
  132. <p> Edit nick </p>
  133. <SpoilerButton text={'Change nick'} style={{ width: '100%' }}>
  134. <div
  135. style={{
  136. display: 'flex',
  137. alignItems: 'center',
  138. flexDirection: 'row',
  139. }}
  140. >
  141. <CustomInput
  142. state={state?.nick || ''}
  143. onChangeText={onChangeNick}
  144. />
  145. <Button
  146. style={{ margin: '10px' }}
  147. disabled={state?.nick && state?.nick.length >= 5 ? false : true}
  148. onClick={saveChange}
  149. size="large"
  150. type="primary"
  151. >
  152. Save nick
  153. </Button>
  154. </div>
  155. </SpoilerButton>
  156. <p> Edit password </p>
  157. <SpoilerButton text={'Change password'}
  158. style={{ width: '100%', marginBottom:'10px' }}>
  159. <p> Login</p>
  160. <div
  161. style={{
  162. display: 'flex',
  163. alignItems: 'center',
  164. flexDirection: 'row',
  165. }}
  166. >
  167. <CustomInput
  168. state={changePass?.login}
  169. onChangeText={onChangePassLogin}
  170. type={true}
  171. />
  172. </div>
  173. <p> Old password</p>
  174. <div
  175. style={{
  176. display: 'flex',
  177. alignItems: 'center',
  178. flexDirection: 'row',
  179. }}
  180. >
  181. <CustomInput
  182. state={changePass?.password}
  183. onChangeText={onChangePassPassword}
  184. checked={checked1}
  185. />
  186. {checked1 ? (
  187. <EyeInvisibleOutlined
  188. onClick={() => setChecked1(!checked1)}
  189. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  190. />
  191. ) : (
  192. <EyeOutlined
  193. onClick={() => setChecked1(!checked1)}
  194. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  195. />
  196. )}
  197. </div>
  198. <p> New password</p>
  199. <div
  200. style={{
  201. display: 'flex',
  202. alignItems: 'center',
  203. flexDirection: 'row',
  204. }}
  205. >
  206. <CustomInput
  207. state={changePass?.newPassword || ''}
  208. checked={checked2}
  209. onChangeText={onChangePassNew}
  210. />
  211. {checked2 ? (
  212. <EyeInvisibleOutlined
  213. onClick={() => setChecked2(!checked2)}
  214. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  215. />
  216. ) : (
  217. <EyeOutlined
  218. onClick={() => setChecked2(!checked2)}
  219. style={{ marginLeft: '5px', fontSize: 'xx-large' }}
  220. />
  221. )}
  222. </div>
  223. <div
  224. style={{
  225. display: 'flex',
  226. alignItems: 'center',
  227. flexDirection: 'column',
  228. }}
  229. >
  230. <Button
  231. style={{ margin: '10px' }}
  232. disabled={
  233. changePass?.login &&
  234. changePass?.password &&
  235. /(?=.*[0-9])(?=.*[!@#$%^&*])(?=.*[a-z])(?=.*[A-Z])[0-9!@#$%^&*a-zA-Z]{8,}/g.test(
  236. changePass?.newPassword,
  237. )
  238. ? false
  239. : true
  240. }
  241. onClick={saveNewPassword}
  242. size="x-large"
  243. type="primary"
  244. >
  245. Save new password
  246. </Button>
  247. </div>
  248. </SpoilerButton>
  249. </div>
  250. </ConstructorModal>
  251. </div>
  252. )
  253. }
  254. export const CEditSetting = connect(
  255. (state) => ({
  256. myId: state?.auth.payload.sub?.id,
  257. info: state?.myData?.aboutMe,
  258. changePassword: state.promise?.newPassword,
  259. }),
  260. {
  261. onSaveUserUpsert: actionUserUpdateTypeSaga,
  262. onSaveNewPassword: actionChangePassword,
  263. onClearPromise: actionClearPromiseForName,
  264. },
  265. )(EditSetting)