header-build.js 3.7 KB

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