App.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 {PlayerbarConnect} from './components/Playerbar'
  8. import thunk from 'redux-thunk';
  9. import { createStore, combineReducers, applyMiddleware } from 'redux';
  10. import { Provider } 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. export const backendURL = "http://player.asmer.fs.a-level.com.ua"
  42. export const gql = getGQL(backendURL + '/graphql')
  43. export const store = createStore(
  44. combineReducers(
  45. {
  46. promise: reducer.promiseReducer,
  47. auth: reducer.authReducer,
  48. player: reducer.playerReducer
  49. //local: localStoreReducer(promiseReducer, 'locale')
  50. }
  51. ), applyMiddleware(thunk)
  52. )
  53. store.subscribe(() => console.log(store.getState()))
  54. //works only once on start of page
  55. if (store.getState().auth?.token) {
  56. history.push('/player')
  57. store.dispatch(action.actionGetUserData())
  58. store.dispatch(action.actionGetUserPlaylists())
  59. } else {
  60. history.push('/login')
  61. }
  62. const Player = () =>
  63. <div>
  64. {/* <header><Link to="/player">Player</Link></header> */}
  65. <div style={{ border:'1px solid blue', display: 'flex', maxHeight: '89vh', minHeight:'30vh', overflow:'none'}}>
  66. <aside style={{width: '30%', overflow:'auto'}}>
  67. <Sidebar.LogoutBtnConnect />
  68. <Sidebar.ProfileWindowDropzoneConnect />
  69. <Sidebar.UserTracksBtnConnect />
  70. <Sidebar.PlaylistAddConnect />
  71. <Sidebar.PlaylistsConnect />
  72. </aside>
  73. <main style={{ border: '1px solid red', width: '80%', height:'inherit', overflow:'auto'}}>
  74. <Switch>
  75. <Route path="/player/playlist/:_id" component={Page.PlaylistPageConnect} exact />
  76. <Route path="/player/tracks/:_id" component={Page.UserTracksPageConnect} exact />
  77. <>
  78. <h2>Welcome to online Player!</h2>
  79. <div style={{ width: '50%', margin: '0 auto' }}>
  80. <ul style={{ textAlign: 'start' }}>
  81. <li><strong>Click "NEW PLAYLIST" - </strong><small>To create new playlist.</small></li>
  82. <li><strong>Drag 'n' drop track to playlist area - </strong><small>To upload the track and add it to current playlist.</small></li>
  83. <li><strong>Drag a track within playlist - </strong><small>To chage the order of playlist tracks.</small></li>
  84. <li><strong>Click "MY UPLOADS" - </strong><small>To see all of your uploaded tracks.</small></li>
  85. </ul>
  86. </div>
  87. </>
  88. </Switch>
  89. </main>
  90. </div>
  91. <PlayerbarConnect />
  92. {/* <PlayerBar /> */}
  93. </div>
  94. function App() {
  95. return (
  96. <Router history={history}>
  97. <Provider store={store}>
  98. <div className="App">
  99. <Switch>
  100. <Route path="/login" component={Logcomp.LoginFormConnect} exact />
  101. <Route path="/registration" component={Logcomp.RegisterFormConnect} exact />
  102. <Route path="/player" component={Player} />
  103. </Switch>
  104. </div>
  105. </Provider>
  106. </Router>
  107. );
  108. }
  109. export default App;