EditPlaylistModal.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Button from 'react-bootstrap/Button';
  2. import Modal from 'react-bootstrap/Modal';
  3. import React, {useState} from 'react';
  4. import { actionPlaylistById } from '../store/promiseReducer';
  5. import { store } from '../store/store';
  6. import { sendForm } from './SendForm';
  7. import {Form} from "react-bootstrap";
  8. export function EditPlaylistModal (props) {
  9. const [name, setName] = useState(props.playlist?.name);
  10. const [description, setDescription] = useState(props.playlist?.description);
  11. //const [privat, setPrivat] = useState(0);
  12. const [image, setImage] = useState(props.playlist?.photo);
  13. const PostEditPlaylist = async(event) =>{
  14. event.preventDefault();
  15. const data = new FormData();
  16. data.append("name", name || props.playlist?.name);
  17. (description !== props.playlist?.description) && data.append("description", description);
  18. data.append("private", props.playlist?.private);
  19. image.name && data.append("photo", image, image.name);
  20. sendForm('playlists/' + props.playlist?.id + '/edit', data);
  21. setTimeout(() => store.dispatch(actionPlaylistById(props.playlist?.id)), 100) ;
  22. }
  23. const PreViewImage = (image) => {
  24. if (image && typeof (image) !== "string") {
  25. return <div className="d-flex justify-content-center">
  26. <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>
  27. </div>
  28. }
  29. }
  30. return(
  31. <Modal
  32. {...props}
  33. aria-labelledby="contained-modal-title-vcenter"
  34. centered
  35. >
  36. <Modal.Header closeButton>
  37. <Modal.Title id="contained-modal-title-vcenter">Edit Playlist</Modal.Title>
  38. </Modal.Header>
  39. <Modal.Body >
  40. <Form onSubmit={PostEditPlaylist} id='loadTracksForm'>
  41. {PreViewImage(image)}
  42. <Form.Group className="mb-3" >
  43. <Form.Label>Image</Form.Label>
  44. <Form.Control type="file" name="picture" accept="image/*" id="fileImage" className='form-control mb-3' onChange={(e) => {
  45. setImage(e.target.files[0]);
  46. }} multiple={false} />
  47. </Form.Group>
  48. <Form.Group className="mb-3" >
  49. <Form.Label>Name</Form.Label>
  50. <Form.Control type="text" id="username" className='input form-control mb-3' value={name} onChange={e => setName(e.target.value)} />
  51. </Form.Group>
  52. <Form.Group className="mb-3" >
  53. <Form.Label>Description</Form.Label>
  54. <Form.Control as="textarea" name="description" rows={3} id="description" className='form-control mb-3' value={description || ''} onChange={e => setDescription(e.target.value)} />
  55. </Form.Group>
  56. </Form>
  57. </Modal.Body>
  58. <Modal.Footer>
  59. <Button variant="outline-danger" type='submit' form='loadTracksForm' onClick={props.onHide}>Save</Button>
  60. </Modal.Footer>
  61. </Modal>)
  62. }