index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 (
  53. <Link
  54. to={`/player/tracks/:${_id}`}
  55. style={{
  56. display: 'block',
  57. backgroundColor: 'purple',
  58. color: 'white',
  59. margin: '5px',
  60. padding: '5px'
  61. }}
  62. >MY UPLOADS</Link>
  63. )
  64. }
  65. export const UserTracksBtnConnect = connect(state => ({ userId: state.promise.userData?.payload?._id || '' }))(UserTracksBtn)
  66. const PlaylistAdd = ({ addPlaylist }) => {
  67. let [clicked, setClicked] = useState(false)
  68. let [name, setName] = useState()
  69. return (
  70. <div>
  71. {
  72. !clicked ?
  73. <button
  74. style={{ border: '1px solid black', backgroundColor: 'mediumseagreen', width: '95%', padding: '5px', margin: '5px' }}
  75. onClick={() => setClicked(true)}
  76. >NEW PLAYLIST</button>
  77. :
  78. <div style={{ width: '95%', margin: '0 auto' }}>
  79. <input
  80. style={{ width: '72%', padding: '5px' }}
  81. placeholder='Playlist name'
  82. value={name}
  83. onChange={(e) => setName(e.target.value)}
  84. />
  85. <button
  86. disabled={!name}
  87. style={{ padding: '5px', backgroundColor: 'mediumseagreen' }}
  88. onClick={() => { addPlaylist(name); setClicked(false); setName(''); }}
  89. >+</button>
  90. <button
  91. style={{ padding: '5px', backgroundColor: 'red' }}
  92. onClick={() => { setClicked(false); setName('') }}
  93. >X</button>
  94. </div>
  95. }
  96. </div>
  97. )
  98. }
  99. export const PlaylistAddConnect = connect(null, { addPlaylist: action.actionAddPlaylist })(PlaylistAdd)
  100. const Playlists = ({ playlists }) => {
  101. return (
  102. <div>
  103. {
  104. playlists?.payload ? playlists.payload.map(item => {
  105. return (
  106. <Link
  107. style={{ display: 'block', backgroundColor:'darkcyan', color: 'cyan', margin: '5px', padding: '5px' }}
  108. to={`/player/playlist/:${item._id}`}
  109. >{item.name}</Link>
  110. )
  111. }).reverse() : ''
  112. }
  113. </div>
  114. )
  115. }
  116. export const PlaylistsConnect = connect(state => ({ playlists: state.promise.userPlaylists || {} }))(Playlists)