NewParcelComponent.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, {useEffect, useState} from "react";
  2. import {InputText} from "primereact/inputtext";
  3. import {Button} from "primereact/button";
  4. import {SPRING_SERVER} from "../config";
  5. export default function (props) {
  6. const userDataUrl = SPRING_SERVER + '/api/users/' + props.username;
  7. const [name,setName] = useState("");
  8. const [address, setAddress] = useState("");
  9. function addNewParcel() {
  10. fetch(userDataUrl, {
  11. method: 'PUT',
  12. headers: {
  13. "Authorization": "Bearer " + localStorage.getItem("access_token"),
  14. 'Content-Type': 'application/json',
  15. },
  16. body: JSON.stringify({
  17. deliveryAddress: address,
  18. recipientName: "Adam Nowak",
  19. deliveryTime: [
  20. 2024,
  21. 1,
  22. 25,
  23. 21,
  24. 34,
  25. 55
  26. ],
  27. packets: [
  28. {
  29. description: "Packet description",
  30. name: name
  31. }
  32. ]
  33. })
  34. })
  35. .then(response => response.json())
  36. .then(data => {
  37. props.callback()
  38. })
  39. }
  40. return (
  41. <div className="w-11 my-2 p-2 surface-200 border-round shadow-3 flex flex-column">
  42. <div>
  43. <div>Parcel name:</div>
  44. <InputText type="text" className="w-full p-inputtext-sm surface-300 mt-1" onChange={(e) => setName(e.target.value)}/>
  45. </div>
  46. <div className="mt-3">
  47. <div>Address:</div>
  48. <InputText type="text" className="w-full p-inputtext-sm surface-300 mt-1" onChange={(e) => setAddress(e.target.value)}/>
  49. </div>
  50. <Button onClick={() => {
  51. addNewParcel()
  52. }} className="align-self-end mt-2"><i
  53. className="pi pi-check text-green-500 text-xl font-bold"></i> Add</Button>
  54. </div>
  55. )
  56. }