App.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import './App.css';
  2. import * as action from './actions'
  3. import * as reducer from './reducers'
  4. import * as Logcomp from './components/Login'
  5. import * as Sidebar from './components/Sidebar'
  6. import * as Page from './components/Page'
  7. import thunk from 'redux-thunk';
  8. import { useEffect, useState } from 'react';
  9. import { createStore, combineReducers, applyMiddleware } from 'redux';
  10. import { Provider, connect } from 'react-redux';
  11. import { Link, Route, Router, Switch } from 'react-router-dom';
  12. import createHistory from 'history/createBrowserHistory'
  13. export function jwtDecode(token) {
  14. try {
  15. let decoded = token.split('.')
  16. decoded = decoded[1]
  17. decoded = atob(decoded)
  18. decoded = JSON.parse(decoded)
  19. return decoded
  20. }
  21. catch (e) {
  22. return;
  23. }
  24. }
  25. export const getGQL = url =>
  26. (query, variables = {}) =>
  27. fetch(url, {
  28. method: 'POST',
  29. headers: {
  30. "Content-Type": "application/json",
  31. ...(localStorage.authToken ? { "Authorization": "Bearer " + localStorage.authToken } : {})
  32. },
  33. body: JSON.stringify({ query, variables })
  34. })
  35. .then(res => res.json())
  36. .then(data => {
  37. if (data.errors && !data.data) throw new Error(JSON.stringify(data.errors))
  38. return data.data[Object.keys(data.data)[0]]
  39. })
  40. export const history = createHistory()
  41. const backendURL = "http://player.asmer.fs.a-level.com.ua"
  42. export const gql = getGQL(backendURL + '/graphql')
  43. const store = createStore(
  44. combineReducers(
  45. {
  46. promise: reducer.promiseReducer,
  47. auth: reducer.authReducer,
  48. //local: localStoreReducer(promiseReducer, 'locale')
  49. }
  50. ), applyMiddleware(thunk)
  51. )
  52. store.subscribe(() => console.log(store.getState()))
  53. //works only once on start of page
  54. if(store.getState().auth?.token) {
  55. history.push('/player')
  56. store.dispatch(action.actionGetUserData())
  57. store.dispatch(action.actionGetUserPlaylists())
  58. } else {
  59. history.push('/login')
  60. }
  61. const ProfileWindow = ({user, onLogout}) => {
  62. let [userInfo, setUserInfo] = useState(user.payload)
  63. useEffect(()=> {
  64. setUserInfo(user.payload)
  65. },[user, userInfo])
  66. return(
  67. <section>
  68. <div
  69. style={{ border: '1px solid black', backgroundColor: 'red', color: 'white' }}
  70. onClick={() => { onLogout(); history.push('/login') }}
  71. >log-out[X]</div>
  72. <div style={{border:'1px solid chartreuse'}}>
  73. <h3>{userInfo?.login || 'user'}</h3>
  74. <img
  75. width={100}
  76. height={100}
  77. style={{ border: '1px solid black', display:'block', margin:'5% auto', marginBottom:'2px'}}
  78. src={ userInfo?.avatar?.url ? backendURL + '/' + userInfo?.avatar?.url : ''}
  79. alt='avatar'
  80. />
  81. <small>change avavtar</small>
  82. </div>
  83. <Link
  84. to={`/player/tracks/:${userInfo?._id}`}
  85. style={{
  86. display:'block',
  87. backgroundColor: 'purple',
  88. color: 'white',
  89. margin: '5px',
  90. padding:'5px'
  91. }}
  92. >My tracks</Link>
  93. </section>
  94. )
  95. }
  96. const ProfileWindowConnect = connect(state => ({ user: state.promise.userData || {} }),{ onLogout: action.actionAuthLogout })(ProfileWindow)
  97. const Player = () =>
  98. <>
  99. <header>Player</header>
  100. <div style={{ display: 'flex' }}>
  101. <aside style={{ border: '1px solid black', width: '30%' }}>
  102. <ProfileWindowConnect />
  103. <Sidebar.PlaylistsConnect />
  104. </aside>
  105. <main style={{ border: '1px solid black', width: '80%' }}>
  106. <Switch>
  107. <Route path="/player/playlist/:_id" component={Page.PlaylistPageConnect} exact/>
  108. <Route path="/player/tracks/:_id" component={Page.UserTracksPageConnect} exact/>
  109. <>
  110. <h2>Welcome to online Player!</h2>
  111. <div style={{width:'50%',margin: '0 auto'}}>
  112. <ul style={{textAlign:'start'}}>
  113. <li><strong>To create playlist: </strong>click "NEW PLAYLIST"</li>
  114. <li><strong>To upload track: </strong>drag and drop it to playlist area</li>
  115. <li><strong>To see list of all your tracks: </strong>click "My tracks"</li>
  116. </ul>
  117. </div>
  118. </>
  119. </Switch>
  120. </main>
  121. </div>
  122. <footer> back stop forw</footer>
  123. </>
  124. // const PlayerConnect = connect(
  125. // state => ({ user: state.promise.userData || {} }),{ onLogout: action.actionAuthLogout}
  126. // )(Player)
  127. function App() {
  128. return (
  129. <Router history={history}>
  130. <Provider store={store}>
  131. <div className="App">
  132. <Switch>
  133. <Route path="/login" component={Logcomp.LoginFormConnect} exact />
  134. <Route path="/registration" component={Logcomp.RegisterFormConnect} exact />
  135. <Route path='/player' component={Player} />
  136. </Switch>
  137. </div>
  138. </Provider>
  139. </Router>
  140. );
  141. }
  142. export default App;