SelectedBike.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {Badge} from "primereact/badge";
  2. import Moment from "react-moment";
  3. import {Button} from "primereact/button";
  4. import {SPRING_SERVER} from "../config";
  5. import {useEffect, useState} from "react";
  6. import authorizedFetch from "../Utils";
  7. import {useTranslation} from "react-i18next";
  8. export default function (props) {
  9. const [pdfGeneratorVisible, setPdfGeneratorVisible] = useState(false);
  10. const [payVisible, setPayVisible] = useState(false);
  11. const { t } = useTranslation(); // Initialize the hook
  12. useEffect(() => {
  13. console.log("rerender")
  14. setPayVisible(false)
  15. setPdfGeneratorVisible(false)
  16. }, [props.selectedBike.id]);
  17. function rentMethod() {
  18. const url = SPRING_SERVER + '/api/rentABike/' + props.user + "/" + props.selectedBike.id;
  19. fetch(url, {
  20. method: "POST", headers: {
  21. 'Accept': 'application/json',
  22. 'Content-Type': 'application/json',
  23. "Authorization": "Bearer " + localStorage.getItem("access_token"),
  24. }, body: JSON.stringify({
  25. "rentedFrom": props.selectedDate[0],
  26. "rentedTo": props.selectedDate[1],
  27. "price": props.selectedBike.price
  28. })
  29. }).then(() => {
  30. props.callback()
  31. }).then(() => {
  32. setPayVisible(true)
  33. })
  34. }
  35. function generateInvoice() {
  36. const pdfEndpoint = SPRING_SERVER + "/api/download-pdf/" + props.selectedBike.id; // Replace with your PDF endpoint URL
  37. fetch(pdfEndpoint, {
  38. method: 'GET', headers: {
  39. "Authorization": "Bearer " + localStorage.getItem("access_token"),
  40. }
  41. })
  42. .then(response => {
  43. if (!response.ok) {
  44. throw new Error('Network response was not ok');
  45. }
  46. return response.blob();
  47. })
  48. .then(blob => {
  49. // Create a temporary URL for the blob
  50. const url = URL.createObjectURL(blob);
  51. // Create an anchor element to trigger the download
  52. const a = document.createElement('a');
  53. a.href = url;
  54. a.download = 'document.pdf'; // Change the filename if needed
  55. // Programmatically trigger the download
  56. document.body.appendChild(a);
  57. a.click();
  58. // Clean up: remove the anchor and revoke the URL
  59. document.body.removeChild(a);
  60. URL.revokeObjectURL(url);
  61. }).then(() => {
  62. setPdfGeneratorVisible(false)
  63. })
  64. .catch(error => {
  65. console.error('Fetch error:', error);
  66. });
  67. }
  68. function payForABike() {
  69. authorizedFetch(SPRING_SERVER + "/api/payForABike/" + props.selectedBike.id)
  70. .then((boolean) => {
  71. if (boolean) setPdfGeneratorVisible(true)
  72. else {
  73. setPayVisible(false)
  74. //show notification that it's too late for payment
  75. }
  76. })
  77. }
  78. return (
  79. <div className="p-2 surface-200 shadow-8 w-12 flex flex-column" style={{border: "1px solid #cfcfcf"}}>
  80. <div className="flex flex-row"><Badge value={props.selectedBike.id} severity="info"/></div>
  81. <div>{t("days")}: {Math.ceil((props.selectedDate[1] - props.selectedDate[0]) / (1000 * 3600 * 24))}</div>
  82. <div>{t("price")}: {Math.ceil((props.selectedDate[1] - props.selectedDate[0]) / (1000 * 3600 * 24)) * props.selectedBike.price}$</div>
  83. <div className={"flex flex-row"}><Button onClick={() => {
  84. rentMethod()
  85. }} className="mt-2 flex-grow-1">{t('rent')}</Button>{payVisible ? <Button severity={"danger"} onClick={() => {
  86. payForABike()
  87. }} className="mt-2 ml-2">{t('pay')}</Button> : <div></div>}</div>
  88. {pdfGeneratorVisible ? <Button severity={"warning"} onClick={() => {
  89. generateInvoice()
  90. }} className="mt-2">{t('invoice')}</Button> : <div></div>}
  91. </div>
  92. )
  93. }