MyPlaylists.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React, { useState, useEffect } from "react";
  2. import { connect } from "react-redux";
  3. import * as actions from "../constants/actions/index";
  4. import store from "../store";
  5. import Library from "./Library";
  6. import { Spin } from "antd";
  7. import Player from "./Player";
  8. import { findMyPlaylists, createMyPlaylist } from "../actions/playerActions";
  9. import Header from "./Header";
  10. import Footer from "./Footer";
  11. import "../styles/library.css";
  12. import "../styles/playlists.css";
  13. import "../styles/MyPlaylists.css";
  14. const MyPlaylists = ({
  15. createMyPlaylist,
  16. findMyPlaylists,
  17. showMyPlaylists = [],
  18. }) => {
  19. const [playlistName, setPlaylistName] = useState("");
  20. const [buttonClick, setButtonClick] = useState(false);
  21. console.log("showMyPlaylists in MyPlaylists: ", showMyPlaylists);
  22. // let owner = localStorage.user;
  23. // let id = localStorage.id;
  24. // uploadTrack.onsubmit = async (e) => {
  25. // e.preventDefault();
  26. // let response = await fetch('/article/formdata/post/user-avatar', {
  27. // method: 'POST',
  28. // body: new FormData(formElem)
  29. // });
  30. // let result = await response.json();
  31. // alert(result.message);
  32. // };
  33. useEffect(() => { findMyPlaylists() }, [buttonClick]);//вызывая функцию findMyPlaylists через useEffect можно получить ошибку при переходе из страницы мои плейлисты на страницу все плейлисты
  34. return (
  35. <>
  36. <Header />
  37. {!showMyPlaylists.length && (
  38. <div className="container">
  39. <Spin size="large" tip="loading..." />
  40. </div>
  41. )}
  42. <div className="main">
  43. <div className="my_playlists">
  44. <div className="my_playlists_list">
  45. <h2>My playlists</h2>
  46. <ul>
  47. {showMyPlaylists.map((playlist, item) => (
  48. <li>{playlist.name}</li>
  49. ))}
  50. </ul>
  51. </div>
  52. <input
  53. type="text"
  54. placeholder="Enter the name of playlist"
  55. onChange={(e) => setPlaylistName(e.target.value)}
  56. />
  57. <button
  58. onClick={() =>
  59. playlistName
  60. ? createMyPlaylist(playlistName) &&
  61. setButtonClick(()=>!buttonClick) &&
  62. console.log("onClick is run")
  63. : console.log("i dont know")
  64. }
  65. >
  66. Add new playlist
  67. </button>
  68. </div>
  69. <form id="formElem">
  70. <h2>Add track</h2>
  71. <input type="file" name="track" id="track" />
  72. </form>
  73. </div>
  74. <Footer />
  75. </>
  76. );
  77. };
  78. const mapStateToProps = (state) => ({
  79. showMyPlaylists: state && state.player && state.player.showMyPlaylists,
  80. });
  81. const mapDispatchToProps = (dispatch) => ({
  82. createMyPlaylist: (playlistName) => dispatch(createMyPlaylist(playlistName)),
  83. findMyPlaylists: () => dispatch(findMyPlaylists()),
  84. });
  85. export default connect(mapStateToProps, mapDispatchToProps)(MyPlaylists);