profilePage.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import {CUsersList, SearchUserInput, CUpdateAdminForm} from "./index";
  2. import {gql, urlUpload, actionPromise, actionGoodCard, getGoods,
  3. actionUserFindOne, updateImgAction, actionAuthLogin, actionAuthLogout,
  4. actionUserUpdate, actionOrdersFind, getData, actionLogin } from "../reducers";
  5. import {createStore, combineReducers, applyMiddleware, bindActionCreators} from 'redux';
  6. import { connect } from "react-redux";
  7. import { useEffect, useState, useRef } from "react";
  8. import {BrowserRouter as Router, Route, Link, Switch, Redirect, useHistory} from 'react-router-dom';
  9. import { NewAuth } from "../reducers/actionAuthLog";
  10. const YourProfile = ({state, orders, updateStatus, updatedUser = {}, newImg, token, onLogin, onLogout, updateImg,
  11. updateUser, isUserAdmin = {}, status, data = {}, getData,
  12. match:{params:{_id}}, isAdmin = [], isOwner = {}, newPicture = "clear"}) => {
  13. const history = useHistory();
  14. const [nick, changeNick] = useState(null);
  15. const [newLogin, changeLogin] = useState(null);
  16. const [nowPassword, superPassword] = useState(null);
  17. const [password, changePassword] = useState(null);
  18. const [password2, changePassword2] = useState(null);
  19. const [isUpdateImg, changeImg] = useState(false);
  20. // const [isUpdatePage, changePage] = useState(false);
  21. // const [newImage, getImage] = useState({});
  22. useEffect(() => {
  23. if(history.location.pathname.includes(`/profile/`)) {
  24. getData(`${isOwner.id}`);
  25. orders(`${isOwner.id}`) ;
  26. }
  27. else {
  28. getData(`${_id}`);
  29. orders(`${_id}`) ;
  30. }
  31. },[history.location.pathname])
  32. useEffect(
  33. async() => {
  34. if(updateStatus == "RESOLVED" && updatedUser && updatedUser.data && updatedUser.data.UserUpsert) {
  35. if(!isAdmin || (isAdmin && isOwner.id == data._id)){
  36. await onLogin(newLogin|| data.login, password || nowPassword);
  37. }
  38. window.location.reload()
  39. }
  40. }, [updateStatus])
  41. const date = new Date(data ? +data.createdAt : 0);
  42. const [showUpdate, onShow] = useState(false);
  43. const [searchInput, changeValue] = useState("Пошук");
  44. console.log(state);
  45. // console.log(data);
  46. return (
  47. <>
  48. { (status == "RESOLVED" && data) &&
  49. <>
  50. <div className = "userSearch">
  51. <input type = "text" placeholder = "Пошук юзера" onChange = {e => changeValue(e.target.value)} onKeyDown = {(e) => e.keyCode == 13 && history.push(`/searchUser/${searchInput}`)}/>
  52. <Link to = {"/searchUser/" + searchInput}>
  53. <button>Пошук</button>
  54. </Link>
  55. </div>
  56. <div className = "profilePage">
  57. {(isUserAdmin )&&
  58. <div className = "adminSmile">
  59. <svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" className="bi bi-emoji-sunglasses-fill" viewBox="0 0 16 16">
  60. <path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16zM2.31 5.243A1 1 0 0 1 3.28 4H6a1 1 0 0 1 1 1v.116A4.22 4.22 0 0 1 8 5c.35 0 .69.04 1 .116V5a1 1 0 0 1 1-1h2.72a1 1 0 0 1 .97 1.243l-.311 1.242A2 2 0 0 1 11.439 8H11a2 2 0 0 1-1.994-1.839A2.99 2.99 0 0 0 8 6c-.393 0-.74.064-1.006.161A2 2 0 0 1 5 8h-.438a2 2 0 0 1-1.94-1.515L2.31 5.243zM4.969 9.75A3.498 3.498 0 0 0 8 11.5a3.498 3.498 0 0 0 3.032-1.75.5.5 0 1 1 .866.5A4.498 4.498 0 0 1 8 12.5a4.498 4.498 0 0 1-3.898-2.25.5.5 0 0 1 .866-.5z"/>
  61. </svg>
  62. <span>admin</span>
  63. </div>
  64. }
  65. { (isOwner.id == data._id) ? <h2>Привіт, {data.nick || "людино"}!</h2> : <h2>{data.nick || data.login}</h2>}
  66. <img src={data.avatar ? `${urlUpload}/${data.avatar.url}`: 'https://pngicon.ru/file/uploads/picca-1.png'} />
  67. <span>
  68. Сторінку створено {`${date.getDate()}. ${date.getMonth() + 1}. ${date.getFullYear()} о ${date.getHours()}: ${date.getMinutes()}: ${date.getSeconds()} `}
  69. </span>
  70. {( (isOwner.id == data._id) || (isAdmin && !isUserAdmin)) &&
  71. <span>
  72. <button onClick = {() => onShow(!showUpdate)}>Редагувати</button>
  73. </span>
  74. }
  75. {showUpdate &&
  76. <div className = "updateWrapper">
  77. <h4>Редагування</h4>
  78. {updatedUser && updatedUser.errors && updatedUser.errors.length > 0 && <p>Введено невірні дані, або цей логін вже зайнято</p>}
  79. {(!isAdmin || (isAdmin && isOwner.id == data._id))&&
  80. <input type = "password" onChange = {(e) => superPassword(e.target.value)} placeholder = "Пароль"/>}
  81. <input onChange = {(e) => changeNick(e.target.value)} placeholder = "Новий нікнейм"/>
  82. <input onChange = {(e) => changeLogin(e.target.value)} placeholder = "Новий логін"/>
  83. <input type = "password" onChange = {(e) => changePassword(e.target.value)} placeholder = "Новий пароль"/>
  84. <input type = "password" onChange = {(e) => changePassword2(e.target.value)} placeholder = "Повторіть пароль"/>
  85. <input type="file" name="photo" id='photo'
  86. onChange={(ev) => {
  87. updateImg(ev.target.files[0])
  88. changeImg(true);
  89. }
  90. }/>
  91. <button disabled = {(newPicture == "PENDING") ||
  92. ( !isAdmin && !nowPassword) ||
  93. ((isAdmin && isOwner.id == data._id) && !nowPassword )}
  94. onClick = {
  95. () => {
  96. let newUser = {};
  97. newUser._id = data._id;
  98. if(nick){
  99. newUser.nick = nick;
  100. }
  101. if (newLogin){
  102. newUser.login = newLogin;
  103. }
  104. if (password && password == password2) {
  105. newUser.password = password;
  106. } else if (password !== password2) {
  107. history.push("/loginError");
  108. return
  109. }
  110. if(isUpdateImg) {
  111. newUser.avatar = {};
  112. newUser.avatar._id = newImg._id;
  113. newUser.avatar.url = newImg._url;
  114. }
  115. updateUser(newUser);
  116. }}>Зберегти</button>
  117. </div>
  118. }
  119. </div>
  120. </>
  121. }
  122. </>
  123. )
  124. }
  125. const mapStateToProps = state => ({
  126. state: state,
  127. status: state.promiseRed && state.promiseRed.user &&
  128. state.promiseRed.user.payload && state.promiseRed.user &&
  129. state.promiseRed.user.status,
  130. data: state.promiseRed && state.promiseRed.user &&
  131. state.promiseRed.user.payload && state.promiseRed.user.payload.data &&
  132. state.promiseRed.user.payload.data.UserFindOne,
  133. newPicture: state.promiseRed && state.promiseRed.photo &&
  134. state.promiseRed.photo.status,
  135. token: state.auth && state.auth.token,
  136. updatedUser: state.promiseRed && state.promiseRed.newUser &&
  137. state.promiseRed.newUser.payload && state.promiseRed.newUser.payload.data &&
  138. state.promiseRed.newUser.payload,
  139. updateStatus: state.promiseRed && state.promiseRed.newUser &&
  140. state.promiseRed.newUser.status,
  141. isUserAdmin: state.promiseRed && state.promiseRed.user &&
  142. state.promiseRed.user.payload && state.promiseRed.user.payload.data &&
  143. state.promiseRed.user.payload.data.UserFindOne &&
  144. state.promiseRed.user.payload.data.UserFindOne.acl &&
  145. state.promiseRed.user.payload.data.UserFindOne.acl.indexOf("admin") > -1,
  146. isOwner: state.auth && state.auth.payload && state.auth.payload.sub &&
  147. state.auth.payload.sub,
  148. newImg: state.promiseRed && state.promiseRed.photo &&
  149. state.promiseRed.photo.payload && state.promiseRed.photo.payload,
  150. isAdmin: state.auth && state.auth.payload && state.auth.payload.sub &&
  151. state.auth.payload.sub.acl && state.auth.payload.sub.acl.indexOf("admin") > -1,
  152. });
  153. const mapDispatchToProps = dispatch => bindActionCreators({
  154. getData: actionUserFindOne,
  155. updateImg: updateImgAction,
  156. updateUser: actionUserUpdate,
  157. onLogout: actionAuthLogout,
  158. onLogin: actionLogin,
  159. orders: actionOrdersFind
  160. }, dispatch);
  161. const CYourProfile = connect(mapStateToProps, mapDispatchToProps)
  162. (YourProfile)
  163. export default CYourProfile;