EditProfile.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. export async function sendForm (url, data) {
  10. let error = await fetch(`http://player-api/api/${url}`, {
  11. method: 'POST',
  12. headers: {
  13. ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {})
  14. },
  15. body: data
  16. }).then(res => res.json())
  17. .then(data => {
  18. if(data.user) {
  19. store.dispatch(actionAuthLogin(store.getState().auth?.token, data.user));
  20. return data
  21. } else if(data.login){
  22. return data.login[0];
  23. } else{
  24. return data;
  25. }
  26. })
  27. return error;
  28. }
  29. export function EditProfile (props) {
  30. const [login, setLogin] = useState(props.user?.login);
  31. const [name, setName] = useState(props.user?.name);
  32. const [avatar, setAvatar] = useState((props.user?.avatar || image));
  33. const [passwordConfirm, setPasswordConfirm] = useState('');
  34. const [password, setPassword] = useState('');
  35. const [textModal, setTextModal] = useState('');
  36. const postForm = async (event) =>{
  37. event.preventDefault();
  38. const data = new FormData();
  39. login && login!==props.user?.login && data.append("login", login);
  40. name && name!==props.user?.name && data.append("name", name);
  41. avatar.name && data.append("avatar", avatar, avatar.name);
  42. setTextModal(( typeof(await sendForm('profile/edit', data))==='string')? (await sendForm('profile/edit', data)) : '');
  43. }
  44. const postEditPassword = async (event) =>{
  45. event.preventDefault();
  46. const data = new FormData();
  47. password && data.append("password", password);
  48. setTextModal(( typeof(await sendForm('profile/edit', data))==='string')? (await sendForm('profile/edit', data)) : '');
  49. }
  50. return (<>
  51. <div className='d-flex container align-items-center justify-content-center'>
  52. <div className=''>
  53. <img className='m-4' alt='...' src={props.user?.avatar || image} width='150px'/>
  54. </div>
  55. <div className=''>
  56. <h5>Name: {store.getState().auth?.user?.name}</h5>
  57. <p>Login: {store.getState().auth?.user?.login}</p>
  58. </div>
  59. </div>
  60. <Form onSubmit={postForm} className='d-flex justify-content-center align-items-center'>
  61. <div>
  62. <Form.Group className="mb-3" controlId="formBasicLogin">
  63. <Form.Label>Change Login:</Form.Label>
  64. <div className='d-flex'>
  65. <Form.Control type="text" className="me-1" placeholder={login}
  66. value={login} onChange={e => setLogin(e.target.value)} />
  67. <Button variant="outline-danger" type="submit">Save</Button>
  68. </div>
  69. <Form.Text className="text-muted">{textModal || "*login must be unique"}</Form.Text>
  70. </Form.Group>
  71. <Form.Group className="mb-3" controlId="formBasicName">
  72. <Form.Label>Change Name:</Form.Label>
  73. <div className='d-flex'>
  74. <Form.Control type="text" className="me-1" placeholder="Anastasiia"
  75. value={name} onChange={e => setName(e.target.value)} />
  76. <Button variant="outline-danger" type="submit">Save</Button>
  77. </div>
  78. </Form.Group>
  79. <Form.Group className="mb-3" controlId="formBasicAvatar">
  80. <Form.Label>Change Avatar:</Form.Label>
  81. <div className='d-flex'>
  82. <Form.Control type="file" className="me-1" name="picture" accept="image/*" onChange={e => setAvatar(e.target.files[0])} multiple={false} />
  83. <Button variant="outline-danger" type="submit">Save</Button>
  84. </div>
  85. </Form.Group>
  86. </div>
  87. </Form>
  88. <h5 className='d-flex justify-content-center'> Change Password:</h5>
  89. <Form onSubmit={postEditPassword} className='d-flex justify-content-center align-items-center'>
  90. <div>
  91. <Form.Group className="mb-3" controlId="formBasicNewPassword">
  92. <Form.Label>New Password:</Form.Label>
  93. <Form.Control type="password" className="" placeholder="new password"
  94. value={password} onChange={e => setPassword(e.target.value)} />
  95. <Form.Text className="text-muted">* Minimum 8 symbols.</Form.Text>
  96. </Form.Group>
  97. <Form.Group className="mb-3" controlId="formBasicConfirmPassword">
  98. <Form.Label>Confirm New Password:</Form.Label>
  99. <Form.Control type="password" placeholder="confirm new password"
  100. value={passwordConfirm} onChange={e => setPasswordConfirm(e.target.value)} />
  101. </Form.Group>
  102. <div className='d-flex justify-content-between'>
  103. <Button variant="outline-danger" type="submit" disabled={ password.length < 8 || password !== passwordConfirm}>
  104. Change Password
  105. </Button>
  106. <Link to={'/user'}>Back to home Page</Link>
  107. </div>
  108. </div>
  109. </Form>
  110. </>)
  111. }
  112. export const CEditProfile = connect(state => ({ user: state.auth?.user }))(EditProfile);