createPlaylist.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React, {useState} from 'react';
  2. import { sendForm } from './SendForm';
  3. // import {Link} from 'react-router-dom';
  4. // import { actionAuthLogin } from '../store/authReducer';
  5. // import { store } from '../store/store';
  6. // function sendForm (url, data) {
  7. // fetch(`http://player-api/api/${url}`, {
  8. // method: 'POST',
  9. // body: data,
  10. // headers: {
  11. // ...(localStorage.authToken ? {"Authorization": "Bearer " + localStorage.authToken} : {})
  12. // },
  13. // }).then(res => res.json())
  14. // .then(data => {
  15. // if(data.token) {
  16. // console.log(data)
  17. // return data
  18. // } else {
  19. // //console.log(data.login[0]);
  20. // }
  21. // })
  22. // }
  23. export const CreatePlaylist = (props) => {
  24. const [name, setName] = useState('');
  25. const [description, setDescription] = useState('');
  26. const [privat, setPrivat] = useState(0);
  27. const [image, setImage] = useState(null);
  28. const PostCreatePlaylist = async(event) =>{
  29. event.preventDefault();
  30. const data = new FormData();
  31. data.append("name", name);
  32. data.append("description", description);
  33. data.append("private", privat);
  34. data.append("photo", image, image.name);
  35. sendForm('playlists/create', data);
  36. }
  37. return <>
  38. <form onSubmit={PostCreatePlaylist} className="authorization center align-items-center justify-content-center d-flex">
  39. <div className="border p-3 col-9">
  40. <h4 className="w-100 text-center">Create Playlist</h4>
  41. <hr/>
  42. <div className="d-flex justify-content-between">
  43. <div className="w-auto">
  44. <label className="form-label">Image</label>
  45. <input type="file" name="picture" accept="image/*" id="file" className='form-control mb-3' onChange={(e) => setImage(e.target.files[0])} multiple={false}/>
  46. <input className="form-check-input me-3" type="checkbox" id="flexCheckIndeterminate" checked={privat} onChange={e => setPrivat(e.target.checked? 1 : 0)}/>
  47. <label className="form-check-label" >Private?</label>
  48. </div>
  49. <div className="w-50">
  50. <label className="form-label">Name</label><br/>
  51. <input type="text" id="username" className='input form-control mb-3' value={name} onChange={e => setName(e.target.value)}/>
  52. <label className="form-label">Description</label>
  53. <textarea type="password" id="password" className='form-control mb-3' value={description} onChange={e => setDescription(e.target.value)}/>
  54. </div>
  55. </div>
  56. <button type='submit' className="btn btn-outline-danger" onClick={props.onHide} >Create</button>
  57. </div>
  58. </form>
  59. </>
  60. }