header-build.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {Link, Route, Switch} from "react-router-dom";
  2. import {connect} from "react-redux";
  3. import {actionAuthLogin, actionAuthLogout, actionPromise} from "../../reducers";
  4. import {CLoginForm} from "./login";
  5. import {CRegForm} from "./registration";
  6. import {useState} from "react";
  7. import {CAvatar,CAvatarDropZone} from "./avatar";
  8. import {Logo} from "./logo";
  9. import {history} from "../../App";
  10. import {actionFullLogin, gql} from "../../actions";
  11. import {takeEvery, select, call,put} from "redux-saga/effects";
  12. import {promiseWorker} from "../../reducers/promiseReducer";
  13. import {CTrackSearch} from "../search";
  14. export const actionSetUserPassword = (password) =>
  15. ({type:'SET_USER_PASSWORD', password})
  16. export function* setUserPasswordWorker (action) {
  17. let {password} = action
  18. let {auth} = yield select()
  19. let userId = auth.payload?.sub?.id
  20. console.log(userId, password)
  21. // yield put (actionAuthLogout())
  22. yield call(promiseWorker,actionPromise('register', gql(`mutation reg($user:UserInput) {
  23. UserUpsert(user:$user) {
  24. _id
  25. }
  26. }`, {user: {userId, password}})))
  27. // yield put (actionFullLogin)
  28. }
  29. export function* setUserPasswordWatcher() {
  30. yield takeEvery ('SET_USER_PASSWORD',setUserPasswordWorker)
  31. }
  32. const ChangePasswordForm = ({onChangePassword}) => {
  33. const [p, setP] = useState ('')
  34. return (
  35. <div>
  36. <input type='password' placeholder='Введите пароль' style={{backgroundColor:"skyblue"}} onChange={e => setP(e.target.value)}></input>
  37. <button onClick={() => {onChangePassword(p); history.push('/')}}>Сменить пароль</button>
  38. </div>
  39. )
  40. }
  41. export const CChangePasswordForm = connect(null,{onChangePassword:actionSetUserPassword})(ChangePasswordForm)
  42. export const Header = () =>
  43. <header className="Header">
  44. <Logo />
  45. <Link to={`/music`}><h2>Все песни</h2></Link>
  46. <Link to={`/mymusic`}> <h2>Моя музыка</h2></Link>
  47. <h2><CTrackSearch /></h2>
  48. <Switch>
  49. <Route path="/login" component={CLoginForm}/>
  50. <Route path="/registration" component={CRegForm}/>
  51. <Route path='*' component={CLoginButtons} />
  52. </Switch>
  53. </header>
  54. const Spoiler = ({ header, children, open=false }) => {
  55. const [openSpoiler, setOpenSpoiler] = useState(open)
  56. return (
  57. <>
  58. <section className="Spoiler" onClick={() => setOpenSpoiler(!openSpoiler)}>
  59. {header}
  60. </section>
  61. {openSpoiler && (
  62. <div className="SpoilerContent">
  63. {children}
  64. </div>
  65. )}
  66. </>
  67. );
  68. };
  69. const LoginButtons = ({onLogout, history, token}) => {
  70. return (
  71. <>
  72. {!token ?
  73. ( <div className='LoginButtons'>
  74. <Link to='/login'><button onClick={() => history.push('/')}>Вход</button></Link>
  75. <Link to='/registration'><button>Регистрация</button></Link></div>) :
  76. (<div className='LoginColumn'>
  77. <Spoiler header={<CAvatar/>}>
  78. <strong>{JSON.parse(atob(token.split(".")[1])).sub.login}</strong>
  79. <CAvatarDropZone />
  80. <CChangePasswordForm/>
  81. <button onClick={() => {onLogout(); history.push('/')}}>Выйти</button>
  82. </Spoiler>
  83. </div>)
  84. }
  85. </>
  86. )
  87. }
  88. export const CLoginButtons = connect(state => ({token: state.auth.token}), {onLogout: actionAuthLogout})(LoginButtons)