App.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import './App.scss';
  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 { 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 GreetPg = () =>
  63. <>
  64. <h2>Welcome to online Player!</h2>
  65. <div className='startpage__ulwrapper'>
  66. <ul>
  67. <li>
  68. <strong className='highlightYellow'>Click "UPLOADS" - </strong>
  69. <small>To see all of your uploaded tracks.</small>
  70. </li>
  71. <li>
  72. <strong className='highlightGreen'>Click "NEW PLAYLIST" - </strong>
  73. <small>To create new playlist.</small>
  74. </li>
  75. <li>
  76. <strong>Drag 'n' drop track to playlist area - </strong>
  77. <small>To upload the track and add it to current playlist.</small>
  78. </li>
  79. <li>
  80. <strong>Drag a track within playlist - </strong>
  81. <small>To chage the order of playlist tracks.</small>
  82. </li>
  83. </ul>
  84. </div>
  85. </>
  86. const Player = () =>
  87. <div>
  88. <div className='page'>
  89. <aside className='sidebar' >
  90. <Sidebar.LogoutBtnConnect />
  91. <Sidebar.ProfileWindowDropzoneConnect />
  92. <Sidebar.UserTracksBtnConnect />
  93. <Sidebar.PlaylistAddConnect />
  94. <Sidebar.PlaylistsConnect />
  95. </aside>
  96. <main className='page__window'>
  97. <Switch>
  98. <Route path="/player/playlist/:_id" component={Page.PlaylistPageConnect} exact />
  99. <Route path="/player/tracks/:_id" component={Page.UserTracksPageConnect} exact />
  100. <GreetPg />
  101. </Switch>
  102. </main>
  103. </div>
  104. <PlayerbarConnect />
  105. </div>
  106. function App() {
  107. return (
  108. <Router history={history}>
  109. <Provider store={store}>
  110. <div className="App">
  111. <Switch>
  112. <Route path="/login" component={Logcomp.LoginFormConnect} exact />
  113. <Route path="/registration" component={Logcomp.RegisterFormConnect} exact />
  114. <Route path="/player" component={Player} />
  115. </Switch>
  116. </div>
  117. </Provider>
  118. </Router>
  119. );
  120. }
  121. export default App;