createPlaylist.js 3.3 KB

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