index.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { makeStyles } from '@material-ui/core'
  2. import { useState,useEffect } from 'react';
  3. import { useSelector,useDispatch } from 'react-redux';
  4. import ToolBar from './ToolBar'
  5. import EditList from './EditList'
  6. import SubmitBtn from './SubmitBtn';
  7. import { getState } from '../../../../redux/authorization/selector'
  8. import { asyncCurrentUser } from '../../../../redux/authorization/operations';
  9. import { updateCredentials,updateUserAvatar } from '../../../../api-data';
  10. const useStyles = makeStyles({
  11. containerAbsolute: {
  12. position: 'absolute',
  13. top: 58,
  14. width: 506,
  15. maxHeight: '905px',
  16. minHeight: '905px',
  17. overflow: 'hidden',
  18. backgroundColor: '#f3f2f2',
  19. }
  20. })
  21. interface IEditBar {
  22. setSelectedIndex: React.Dispatch<React.SetStateAction<number | null>>
  23. }
  24. const EditBar= ({setSelectedIndex}:IEditBar) => {
  25. const classes = useStyles()
  26. const dispatch = useDispatch()
  27. const user = useSelector(getState)
  28. const [name, setName] = useState<string>('')
  29. const [lastName, setLastName] = useState<string>('')
  30. const [openBtn, setOpenBtn] = useState<boolean>(false)
  31. const [file,setFile] = useState<any>(null)
  32. const {name:Name,lastName:LastName} = user
  33. const handleSubmit = async () => {
  34. if (name !== Name || lastName !== LastName){
  35. await updateCredentials({ name, lastName, originalName: name, originalLastName: lastName })
  36. }
  37. if (file) {
  38. const formData: any = new FormData()
  39. formData.append("avatar", file);
  40. await updateUserAvatar(formData)
  41. }
  42. file && setFile(null)
  43. openBtn && setOpenBtn(false)
  44. dispatch(asyncCurrentUser())
  45. }
  46. useEffect(() => {
  47. Name&&setName(Name)
  48. LastName && setLastName(LastName)
  49. }, [Name, LastName])
  50. return (
  51. <div>
  52. <ToolBar setSelectedIndex={setSelectedIndex}/>
  53. <div className={classes.containerAbsolute}>
  54. <EditList user={user} name={name} setName={setName}
  55. lastName={lastName} setLastName={setLastName}
  56. openBtn={openBtn} setOpenBtn={setOpenBtn}
  57. file={file} setFile={setFile}/>
  58. {openBtn&&<SubmitBtn handleSubmit={handleSubmit}/>}
  59. </div>
  60. </div>
  61. )
  62. }
  63. export default EditBar