Tracks.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {connect} from 'react-redux';
  2. import {actionFullSetTrack, actionFullPlay, actionFullSetPlaylist , actionAddTrackToQueue} from '../store/playerReducer';
  3. import {actionPlaylistById, actionArtistById} from '../store/promiseReducer';
  4. import { store } from '../store/store';
  5. import Button from 'react-bootstrap/Button';
  6. import Modal from 'react-bootstrap/Modal';
  7. import { sendForm } from './SendForm';
  8. import React, {useState} from 'react';
  9. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  10. import {faCheck, faPause, faPlay} from "@fortawesome/free-solid-svg-icons";
  11. import { Link } from 'react-router-dom';
  12. import { Dropdown , Form} from 'react-bootstrap';
  13. import { RunToast } from './Toast';
  14. export let audio = new Audio();
  15. const PlaylistOption = () => {
  16. const playlists = store.getState().promise?.usersPlaylists?.payload?.playlists;
  17. if (playlists) {
  18. console.log(playlists);
  19. return (playlists.map((playlistone, i) => <option value={playlistone.id} className="bg-dark" key={i}>{playlistone.name}</option>))
  20. }
  21. }
  22. const ButtonDeleteTrack = (track) => {
  23. const [deletePllstModal, setDeletePllstModal] = useState(false);
  24. return (
  25. <>
  26. <Dropdown.Item onClick={() => {console.log(track); setDeletePllstModal(true)}}>Delete</Dropdown.Item>
  27. <Modal
  28. show={deletePllstModal} onHide={() => setDeletePllstModal(false)}
  29. backdrop="static" keyboard={false} track={track}>
  30. <Modal.Header closeButton>
  31. <Modal.Title className='w-100 text-center'>Delete Track?</Modal.Title>
  32. </Modal.Header>
  33. <Modal.Body className='text-center'>
  34. Are you really want to delete track <span className='text-danger'><i>"{track.track.name}" - {track.track?.id3?.artist}</i></span>?
  35. </Modal.Body>
  36. <Modal.Footer className='d-flex justify-content-center'>
  37. <Button className='mx-2' variant="secondary" onClick={() => setDeletePllstModal(false)}>
  38. Close
  39. </Button>
  40. <Button track={track} className='mx-2' variant="danger" onClick={async() => {
  41. const data = new FormData();
  42. data.append('playlistId', track.track?.pivot?.playlist_id);
  43. data.append('trackId', track.track?.pivot?.track_id);
  44. await sendForm(`playlists/remove-track`, data);
  45. setDeletePllstModal(false);
  46. setTimeout(() => store.dispatch(actionPlaylistById(track.track?.pivot?.playlist_id)), 100)
  47. }}>Delete</Button>
  48. </Modal.Footer>
  49. </Modal>
  50. </>
  51. )
  52. }
  53. const Track = ({track, playlist, isPlaying}) => {
  54. const PostLoadTracks = async(form) =>{
  55. console.log(form.target)
  56. const data = new FormData(form.target);
  57. sendForm('playlists/add-track', data);
  58. RunToast('bg-success','Success', `Track add to playlist`)
  59. }
  60. return(
  61. <tr className={`disactive-track track-play-button-${track?.id}`}>
  62. <td scope="row" width={30} data-id={track.id}>
  63. <div className="col">
  64. <Button variant="outline-light" className={`rounded-5`} title='Play' onClick={async () => {
  65. // console.log(playlist.tracks, playlist?.tracks[playlist?.tracks.indexOf(track)]);
  66. playlist.tracks && store.dispatch(actionFullSetPlaylist(playlist?.tracks));
  67. playlist.tracks ? store.dispatch(actionFullSetTrack(playlist?.tracks[playlist?.tracks.indexOf(track)])) : store.dispatch(actionFullSetTrack(track))
  68. store.dispatch(actionFullPlay());
  69. }}>
  70. <FontAwesomeIcon className={`track-play-icon-${track?.id}`} icon={faPlay}/>
  71. </Button>
  72. </div>
  73. </td>
  74. <td>
  75. <Link className="link-light" to='#'> {track.name}</Link>
  76. </td>
  77. <td onClick={() => store.dispatch(actionArtistById(track.artist_id))}>
  78. <Link className="link-light" to={`/artists/${track.artist_id}`}> {track.id3.artist}</Link>
  79. </td>
  80. <td>
  81. <Link className="link-light" to={`/albums/${track?.album?.id}`}> {track.id3.getAlbum}</Link>
  82. </td>
  83. <td align={"right"}>
  84. <Dropdown align={"end"}>
  85. <Dropdown.Toggle variant="outline-light" id="dropdown-basic"></Dropdown.Toggle>
  86. <Dropdown.Menu variant={"dark"}>
  87. {playlist?.user_id === store.getState().auth.user.id ? <ButtonDeleteTrack track={track} /> :
  88. <Form className="input-group d-flex" onSubmit={(e) => {
  89. e.preventDefault();
  90. PostLoadTracks(e);
  91. }}>
  92. <input type={"hidden"} name="trackId" value={track?.id}></input>
  93. <select className="dropdown-item btn btn-outline-secondary w-75" name="playlistId" id="inputGroupSelect04"
  94. aria-label="Example select with button addon">
  95. <PlaylistOption/>
  96. </select>
  97. <button type={"submit"} className="btn btn-outline-secondary w-auto"><FontAwesomeIcon icon={faCheck}/></button>
  98. </Form>
  99. }
  100. <Dropdown.Item onClick={() => {
  101. store.dispatch(actionAddTrackToQueue(track))
  102. }}>Add to Queue</Dropdown.Item>
  103. </Dropdown.Menu>
  104. </Dropdown>
  105. </td>
  106. </tr>)
  107. }
  108. export const TracksAll = ({tracks, playlist, isPlaying}) =>
  109. <table className="table table-dark table-hover align-middle">
  110. <thead>
  111. <tr>
  112. <th scope="col" width={30}></th>
  113. <th scope="col">Track name</th>
  114. <th scope="col">Artist</th>
  115. <th scope="col">Album</th>
  116. <th scope='col'>Action</th>
  117. </tr>
  118. </thead>
  119. <tbody>
  120. {tracks.map((tracks, i) => <Track key={i} track={tracks} playlist={playlist} isPlaying={isPlaying}/>)}
  121. </tbody>
  122. </table>
  123. export const СAllTracks = connect(state => ({playlist: state.promise.plstById?.payload || {},
  124. tracks: state.promise?.plstById?.payload?.tracks || [],
  125. isPlaying: state.player?.isPlaying || false,} ), )(TracksAll);