createPlaylist.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import React, {useState} from 'react';
  2. import { sendForm } from './SendForm';
  3. import Modal from 'react-bootstrap/Modal';
  4. import Button from 'react-bootstrap/Button';
  5. import { actionUsersPlaylists } from '../store/promiseReducer';
  6. import { store } from '../store/store';
  7. import { history } from '../App';
  8. import {Form} from "react-bootstrap";
  9. export const CreatePlaylist = (props) => {
  10. const [name, setName] = useState('');
  11. const [description, setDescription] = useState('');
  12. const [privat, setPrivat] = useState(0);
  13. const [image, setImage] = useState(null);
  14. const PostCreatePlaylist = async(event) =>{
  15. event.preventDefault();
  16. const data = new FormData();
  17. data.append("name", name);
  18. description && data.append("description", description);
  19. data.append("private", privat);
  20. image && data.append("photo", image, image.name);
  21. let result = await sendForm('playlists/create', data);
  22. console.log(result);
  23. store.dispatch(actionUsersPlaylists(store.getState().auth?.user?.id));
  24. history.push(`/playlist/${result.playlist.id}`)
  25. }
  26. const PreViewImage = (image) => {
  27. if (image && typeof (image) !== "string") {
  28. return <div className="d-flex justify-content-center">
  29. <label htmlFor="fileImage" className='me-4 playlist-img-box rounded-5 cursor-pointer' style={{backgroundImage: `url(${URL.createObjectURL(image)})`, backgroundSize: "cover", backgroundRepeat: "no-repeat", backgroundPosition: "center"}}></label>
  30. </div>
  31. }
  32. }
  33. return <>
  34. <Modal
  35. {...props}
  36. aria-labelledby="contained-modal-title-vcenter"
  37. centered
  38. >
  39. <Modal.Header closeButton>
  40. <Modal.Title id="contained-modal-title-vcenter">Create new Playlist</Modal.Title>
  41. </Modal.Header>
  42. <Modal.Body >
  43. <Form onSubmit={PostCreatePlaylist} id='loadTracksForm'>
  44. {PreViewImage(image)}
  45. <Form.Group className="mb-3" >
  46. <Form.Label>Image</Form.Label>
  47. <Form.Control type="file" name="picture" accept="image/*" id="fileImage" className='form-control mb-3' onChange={(e) => {
  48. setImage(e.target.files[0]);
  49. }} multiple={false} />
  50. </Form.Group>
  51. <Form.Group className="mb-3" >
  52. <Form.Label>Name</Form.Label>
  53. <Form.Control type="text" id="username" className='input form-control mb-3' onChange={e => setName(e.target.value)} />
  54. </Form.Group>
  55. <Form.Group className="mb-3" >
  56. <Form.Label>Description</Form.Label>
  57. <Form.Control as="textarea" name="description" rows={3} id="description" className='form-control mb-3' onChange={e => setDescription(e.target.value)} />
  58. </Form.Group>
  59. <Form.Check
  60. type="switch"
  61. id="flexCheckIndeterminate"
  62. label="Private?"
  63. checked={privat}
  64. onChange={e => setPrivat(e.target.checked? 1 : 0)}
  65. />
  66. </Form>
  67. </Modal.Body>
  68. <Modal.Footer>
  69. <Button variant="outline-success w-100" type='submit' form='loadTracksForm' onClick={props.onHide}>Save</Button>
  70. </Modal.Footer>
  71. </Modal>
  72. </>
  73. }