index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import * as action from '../../actions'
  2. import { useState, useEffect, useCallback } from 'react';
  3. import { connect } from 'react-redux';
  4. import { Link } from 'react-router-dom';
  5. import { history, backendURL } from '../../App';
  6. import { useDropzone } from 'react-dropzone'
  7. const LogoutBtn = ({ onLogout }) =>
  8. <div
  9. style={{ border: '1px solid black', backgroundColor: 'red', color: 'white' }}
  10. onClick={() => { onLogout(); history.push('/login') }}
  11. >log-out[X]</div>
  12. export const LogoutBtnConnect = connect(null, { onLogout: action.actionAuthLogout })(LogoutBtn)
  13. const ProfileWindow = ({ user }) => {
  14. let [userInfo, setUserInfo] = useState(user.payload)
  15. useEffect(() => {
  16. setUserInfo(user.payload)
  17. }, [user, userInfo])
  18. return (
  19. <section>
  20. <h3>{userInfo?.login || 'user'}</h3>
  21. <img
  22. width={150}
  23. height={'auto'}
  24. style={{ border: '1px solid black', display: 'block', margin: '5% auto', marginBottom: '2px' }}
  25. src={userInfo?.avatar?.url ? backendURL + '/' + userInfo?.avatar?.url : ''}
  26. alt='avatar'
  27. />
  28. </section>
  29. )
  30. }
  31. export const ProfileWindowConnect = connect(state => ({ user: state.promise.userData || {} }))(ProfileWindow)
  32. const ProfileWindowDropzone = ({ onLoad }) => {
  33. const onDrop = useCallback(acceptedFiles => {
  34. onLoad(acceptedFiles[0])
  35. }, [onLoad])
  36. const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop })
  37. return (
  38. <div {...getRootProps()} style={isDragActive ? { border: '1px solid mediumseagreen' } : { border: '1px solid black' }}>
  39. <input {...getInputProps()} />
  40. <ProfileWindowConnect />
  41. {isDragActive ? <small>drag here...</small> : <small>change avatar</small>}
  42. </div>
  43. )
  44. }
  45. export const ProfileWindowDropzoneConnect = connect(null, { onLoad: action.actionUploadAvatar })(ProfileWindowDropzone)
  46. const UserTracksBtn = ({ userId }) => {
  47. let [_id, setId] = useState()
  48. useEffect(() => {
  49. console.log('CHENG', userId)
  50. setId(userId)
  51. }, [userId])
  52. return (<Link to={`/player/tracks/:${_id}`} className='sidebar__link highlight'>MY UPLOADS</Link>)
  53. }
  54. export const UserTracksBtnConnect = connect(state => ({ userId: state.promise.userData?.payload?._id || '' }))(UserTracksBtn)
  55. const PlaylistAdd = ({ addPlaylist }) => {
  56. let [clicked, setClicked] = useState(false)
  57. let [name, setName] = useState()
  58. return (
  59. <div>
  60. {
  61. !clicked ?
  62. <button
  63. className='sidebar__button'
  64. onClick={() => setClicked(true)}
  65. >NEW PLAYLIST</button>
  66. :
  67. <div style={{ width: '95%', margin: '0 auto' }}>
  68. <input
  69. style={{ width: '72%', padding: '5px' }}
  70. placeholder='Playlist name'
  71. value={name}
  72. onChange={(e) => setName(e.target.value)}
  73. />
  74. <button
  75. disabled={!name}
  76. style={{ padding: '5px', backgroundColor: 'mediumseagreen' }}
  77. onClick={() => { addPlaylist(name); setClicked(false); setName(''); }}
  78. >+</button>
  79. <button
  80. style={{ padding: '5px', backgroundColor: 'red' }}
  81. onClick={() => { setClicked(false); setName('') }}
  82. >X</button>
  83. </div>
  84. }
  85. </div>
  86. )
  87. }
  88. export const PlaylistAddConnect = connect(null, { addPlaylist: action.actionAddPlaylist })(PlaylistAdd)
  89. const Playlists = ({ playlists }) => {
  90. return (
  91. <div>
  92. {
  93. playlists?.payload ? playlists.payload.map(item => {
  94. return (
  95. <Link className='sidebar__link' to={`/player/playlist/:${item._id}`}>{item.name}</Link>
  96. )
  97. }).reverse() : ''
  98. }
  99. </div>
  100. )
  101. }
  102. export const PlaylistsConnect = connect(state => ({ playlists: state.promise.userPlaylists || {} }))(Playlists)