EditProfile.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import Button from 'react-bootstrap/Button';
  2. import {connect} from 'react-redux';
  3. import React, {useState} from 'react';
  4. import { store } from '../store/store';
  5. import image from '../images/card.png';
  6. import { actionAuthLogin } from '../store/authReducer';
  7. import {Link} from 'react-router-dom';
  8. import Form from 'react-bootstrap/Form';
  9. import {Accordion} from "react-bootstrap";
  10. import {RunToast} from "./Toast";
  11. export async function sendForm (url, data) {
  12. let error = await fetch(`http://player-api/api/${url}`, {
  13. method: 'POST',
  14. headers: {
  15. ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {})
  16. },
  17. body: data
  18. }).then(res => res.json())
  19. .then(data => {
  20. if(data.user) {
  21. RunToast('bg-success','Success', 'Profile updated')
  22. store.dispatch(actionAuthLogin(store.getState().auth?.token, data.user));
  23. return data
  24. } else if(data.login){
  25. RunToast('bg-danger','Error', 'Login should be unique')
  26. return data.login[0];
  27. } else{
  28. return data;
  29. }
  30. })
  31. return error;
  32. }
  33. export function EditProfile (props) {
  34. const [login, setLogin] = useState(props.user?.login);
  35. const [name, setName] = useState(props.user?.name);
  36. const [avatar, setAvatar] = useState((props.user?.avatar || image));
  37. const [description, setDescription] = useState(props.user?.description);
  38. const [passwordConfirm, setPasswordConfirm] = useState('');
  39. const [password, setPassword] = useState('');
  40. const [textModal, setTextModal] = useState('');
  41. const postForm = async (event) =>{
  42. event.preventDefault();
  43. const data = new FormData();
  44. login && login!==props.user?.login && data.append("login", login);
  45. name && name!==props.user?.name && data.append("name", name);
  46. data.append("description", description);
  47. avatar.name && data.append("avatar", avatar, avatar.name);
  48. setTextModal(( typeof(await sendForm('profile/edit', data))==='string')? (await sendForm('profile/edit', data)) : '');
  49. }
  50. const postEditPassword = async (event) =>{
  51. event.preventDefault();
  52. const data = new FormData();
  53. password && data.append("password", password);
  54. setTextModal(( typeof(await sendForm('profile/edit', data))==='string')? (await sendForm('profile/edit', data)) : '');
  55. }
  56. return (<>
  57. <div className='d-flex w-100'>
  58. <div className="me-4">
  59. <div className='mb-3 playlist-img-box rounded-5' style={{backgroundImage: `url(${props.user?.avatar || image})`, backgroundSize: "cover", backgroundRepeat: "no-repeat", backgroundPosition: "center"}}></div>
  60. <Form.Group className="mb-3" controlId="formBasicAvatar">
  61. <div className='d-flex'>
  62. <Form.Control type="file" className="me-1" name="picture" accept="image/*" onChange={e => setAvatar(e.target.files[0])} multiple={false} />
  63. </div>
  64. </Form.Group>
  65. </div>
  66. <div className='w-100'>
  67. <div className="d-flex flex-column justify-content-between h-100">
  68. <div className="w-100 row">
  69. <Form onSubmit={postForm}>
  70. <Form.Group className="mb-3" controlId="formBasicAvatar">
  71. <Form.Label>Change Login:</Form.Label>
  72. <Form.Control type="text" className="me-1" placeholder={login}
  73. value={login} onChange={e => setLogin(e.target.value)} />
  74. </Form.Group>
  75. <Form.Group className="mb-3" controlId="formBasicAvatar">
  76. <Form.Label>Change Name:</Form.Label>
  77. <Form.Control type="text" className="me-1" placeholder="Anastasiia"
  78. value={name} onChange={e => setName(e.target.value)} />
  79. </Form.Group>
  80. <Form.Group className="mb-3" controlId="formBasicAvatar">
  81. <Form.Label>Change About Me:</Form.Label>
  82. <Form.Control as="textarea" rows="3" className="me-1"
  83. value={description !== 'null' ? description : ''} onChange={e => setDescription(e.target.value)} />
  84. </Form.Group>
  85. <div className="d-flex justify-content-between align-items-center mb-3">
  86. <Link to={'/user'}>Back to home Page</Link>
  87. <Button variant="outline-success" type="submit">Save</Button>
  88. </div>
  89. </Form>
  90. <Accordion className="border-0">
  91. <Accordion.Item eventKey="0" className="border-0">
  92. <Accordion.Header className="btn-dark">Change Password</Accordion.Header>
  93. <Accordion.Body className="bg-dark">
  94. <Form onSubmit={postEditPassword} className=''>
  95. <div className="d-flex m-0">
  96. <Form.Group className="col-6 px-3" controlId="formBasicNewPassword">
  97. <Form.Label>New Password:</Form.Label>
  98. <Form.Control type="password" className="" placeholder="new password"
  99. value={password} onChange={e => setPassword(e.target.value)} />
  100. <Form.Text className="text-muted m-0 p-0">* Minimum 8 symbols.</Form.Text>
  101. </Form.Group>
  102. <Form.Group className="col-6 px-3" controlId="formBasicConfirmPassword">
  103. <Form.Label>Confirm New Password:</Form.Label>
  104. <Form.Control type="password" placeholder="confirm new password"
  105. value={passwordConfirm} onChange={e => setPasswordConfirm(e.target.value)} />
  106. </Form.Group>
  107. </div>
  108. <div className='text-end'>
  109. <Button variant="outline-success" type="submit" disabled={ password.length < 8 || password !== passwordConfirm}>
  110. Change Password
  111. </Button>
  112. </div>
  113. </Form>
  114. </Accordion.Body>
  115. </Accordion.Item>
  116. </Accordion>
  117. </div>
  118. </div>
  119. </div>
  120. </div>
  121. </>)
  122. }
  123. export const CEditProfile = connect(state => ({ user: state.auth?.user }))(EditProfile);