index.js 7.9 KB

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