updateForm.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { useState, useRef, useEffect } from 'react';
  2. import {BrowserRouter as Router, Route, Link, Switch, Redirect, useHistory} from 'react-router-dom';
  3. import {gql, urlUpload, actionPromise, actionGoodCard, getGoods, updateImgAction, actionUserUpdate} from "../reducers";
  4. import {createStore, combineReducers, applyMiddleware, bindActionCreators} from 'redux';
  5. import { connect } from 'react-redux';
  6. const UpdateForm = ({updateImg, updateUser, id}) => {
  7. const history = useHistory();
  8. const formRef = useRef(null);
  9. const [nick, changeNick] = useState(null);
  10. const [login, changeLogin] = useState(null);
  11. const [password, changePassword] = useState(null);
  12. const [password2, changePassword2] = useState(null);
  13. const [isUpdateImg, changeImg] = useState(false);
  14. const [isUpdatePage, changePage] = useState(false);
  15. useEffect(() => isUpdatePage && window.location.reload()[isUpdatePage])
  16. console.log(formRef);
  17. return(
  18. <>
  19. <div className = "updateWrapper">
  20. <h4>Редагування</h4>
  21. <input onChange = {(e) => changeNick(e.target.value)} placeholder = "Новий нікнейм"/>
  22. <input onChange = {(e) => changeLogin(e.target.value)} placeholder = "Новий логін"/>
  23. <input onChange = {(e) => changePassword(e.target.value)} placeholder = "Новий пароль"/>
  24. <input onChange = {(e) => changePassword2(e.target.value)} placeholder = "Повторіть пароль"/>
  25. <form className = "updateImg" action="/upload" method="post" enctype="multipart/form-data" id='form' ref={formRef}
  26. onChange = {
  27. () => {
  28. updateImg(formRef);
  29. changeImg(true);
  30. }}>
  31. <input type="file" name="photo" id='photo'/>
  32. </form>
  33. <button onClick = {() => {
  34. let newUser = {}
  35. newUser._id = id;
  36. if(nick){
  37. newUser.nick = nick;
  38. }
  39. if(login) {
  40. newUser.login = login;
  41. }
  42. if (password && password == password2) {
  43. newUser.password = password
  44. } else if (password !== password2) {
  45. history.push("/loginError");
  46. return
  47. }
  48. if(isUpdateImg) {
  49. newUser.avatar = {
  50. _id:123,
  51. url:"est"
  52. }
  53. }
  54. updateUser(newUser);
  55. changePage(true);
  56. }}>Зберегти</button>
  57. </div>
  58. {/* <img src = {`${urlUpload}/images/cbd7145dbc78ced471951c70f571f871`}/> */}
  59. </>
  60. )
  61. }
  62. const mapStateToProps = state => ({
  63. state: state,
  64. // data: state.promiseRed && state.promiseRed.user &&
  65. // state.promiseRed.user.payload && state.promiseRed.user.payload.data &&
  66. // state.promiseRed.user.payload.data.UserFindOne
  67. // basket: state.basket,
  68. // GoodsArr: arrFromObj(ObjFilter(state.basket, "price")),
  69. // order: orderArr(arrFromObj(ObjFilter(state.basket, "price"))),
  70. // orderDone: state.promiseRed && state.promiseRed.order &&
  71. // state.promiseRed.order.payload &&
  72. // state.promiseRed.order.payload.data.OrderUpsert
  73. });
  74. const mapDispatchToProps = dispatch => bindActionCreators({
  75. updateImg: updateImgAction,
  76. updateUser: actionUserUpdate,
  77. // onDel: actionCartDelete,
  78. // onClear: actionCartClear,
  79. // onOrder: actionOrder
  80. }, dispatch);
  81. const CUpdateForm = connect(mapStateToProps, mapDispatchToProps)(UpdateForm)
  82. export default CUpdateForm;