Skip to content

Commit

Permalink
Merge pull request #78 from Lor3wp/dbAddReservation
Browse files Browse the repository at this point in the history
Db add reservation
  • Loading branch information
Lor3wp authored Dec 7, 2023
2 parents 47b33fd + 839c089 commit adfd438
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 41 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_API_BASE_URL=https://hsytrailerapi.azurewebsites.net/api/
27 changes: 26 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"axios": "^1.6.2",
"bootstrap": "^5.3.2",
"bootstrap-icons": "^1.11.1",
"dotenv": "^16.3.1",
"geolib": "^3.3.4",
"i18next": "^23.7.6",
"i18next-browser-languagedetector": "^7.2.0",
Expand All @@ -35,7 +36,8 @@
"react-i18next": "^13.5.0",
"react-router-dom": "^6.16.0",
"react-simple-star-rating": "^5.1.7",
"react-toastify": "^9.1.3"
"react-toastify": "^9.1.3",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/react": "^18.2.15",
Expand Down
1 change: 1 addition & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { StepperProvider } from './context/StepperContext';
import { useTranslation } from 'react-i18next';
import { useState } from 'react';


function App() {
const [itemReturned, setItemReturned] = useState(false);

Expand Down
33 changes: 28 additions & 5 deletions src/components/BankButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,41 @@ import { useState } from 'react';
import PopUpWarningModal from '../components/PopUpWarningModal';
import { useNavigate } from 'react-router-dom';
import { Trans, useTranslation } from 'react-i18next';

const BankButton = ({ logo, bankName, rentId }) => {
import useApi from '../hooks/useApi';
import { useStepper } from '../hooks/useStepper';
const BankButton = ({ logo, bankName, randomUUID }) => {
const [showWarningModal, setShowWarningModal] = useState(false);

const navigate = useNavigate();

const { t } = useTranslation();
const { userData } = useStepper();
const { postRequest } = useApi();

const handleClick = (bankName) => {
const handleClick = async (bankName) => {
if (bankName === 'HSY') {
handleOpenWarningModal();
} else {
navigate(`/rent-successful/${rentId}`);
try {
const bodyData = {
customerInfo: {
name: userData.firstName,
lastName: userData.lastName,
phoneNumber: userData.phoneNumber,
email: userData.emailAddress,
address: userData.streetName,
zipCode: userData.postalCode,
city: userData.cityName,
},
uuid: randomUUID,
};

const responce = await postRequest('add-reservation', bodyData);
console.log('BankButton.jsx 36', responce.updatedReservation._id);
navigate(`/rent-successful/${responce.updatedReservation._id}`);
} catch (error) {
console.log(error);
}
}
};

Expand Down Expand Up @@ -68,7 +91,7 @@ const BankButton = ({ logo, bankName, rentId }) => {
BankButton.propTypes = {
logo: PropTypes.any.isRequired,
bankName: PropTypes.string,
rentId: PropTypes.string.isRequired,
randomUUID: PropTypes.string.isRequired,
};

export default BankButton;
11 changes: 10 additions & 1 deletion src/components/BankType.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import PropTypes from 'prop-types';
import BankButton from './BankButton';
import styles from '../css/BankButton.module.css';

const BankType = ({ gridName, title, arrayName, paymentName, rentId }) => {
const BankType = ({
gridName,
title,
arrayName,
paymentName,
rentId,
randomUUID,
}) => {
return (
<div className={styles.titleButtonContainer}>
<h3>{title}</h3>
Expand All @@ -14,6 +21,7 @@ const BankType = ({ gridName, title, arrayName, paymentName, rentId }) => {
bankName={item.bankName}
key={item.bankName}
rentId={rentId}
randomUUID={randomUUID}
></BankButton>
</div>
))}
Expand All @@ -28,5 +36,6 @@ BankType.propTypes = {
arrayName: PropTypes.array,
paymentName: PropTypes.string,
rentId: PropTypes.string.isRequired,
randomUUID: PropTypes.string.isRequired,
};
export default BankType;
2 changes: 1 addition & 1 deletion src/components/PopUpWarningModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PopUpWarningModal.propTypes = {
show: PropTypes.bool,
onHide: PropTypes.func,
title: PropTypes.string,
body: PropTypes.obj,
body: PropTypes.string,
backButton: PropTypes.string,
acceptButton: PropTypes.string,
acceptButtonVariant: PropTypes.string,
Expand Down
7 changes: 5 additions & 2 deletions src/components/SelectTime.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function SelectTime({
setSelectedStationAndTime,
stationName,
timeSlots,
randomUUID,
}) {
const handleClick = (stationName, timeSlot) => {
setSelectedStationAndTime({
Expand All @@ -16,6 +17,7 @@ function SelectTime({

const itemElements = timeSlots.map((item, index) => (
<TimePeriodButton
randomUUID={randomUUID}
buttonText={item}
setSelectedTime={handleClick}
key={index}
Expand All @@ -35,12 +37,13 @@ function SelectTime({
}

SelectTime.propTypes = {
selectedStationAndTime: PropTypes.obj,
selectedStationAndTime: PropTypes.object,
setSelectedStationAndTime: PropTypes.func,
setSelectedStation: PropTypes.func,
stationName: PropTypes.string.isRequired, // Expect a string prop, and it's required
selectedTime: PropTypes.string,
timeSlots: PropTypes.array.isRequired,
randomUUID: PropTypes.string,
};


export default SelectTime;
46 changes: 32 additions & 14 deletions src/components/TimeForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import RentCalendar from '../components/Calendar';
import SelectTime from '../components/SelectTime';
import timeStyle from '../css/SelectTime.module.css';
Expand All @@ -12,12 +12,17 @@ import { ChevronCompactLeft } from 'react-bootstrap-icons';
import { ChevronCompactRight } from 'react-bootstrap-icons';
import { useStepper } from '../hooks/useStepper';
import { useTranslation } from 'react-i18next';

import useApi from '../hooks/useApi';
// create random uuid for user identification in temporary reservation
// fill with Date values to disable those dates from caledar
const futureDates = [];
const ProductAndTime = ({
onProductAndTimeSelected,
onPrevStep,
handleWarningModal,
randomUUID,
}) => {
const { deleteRequest } = useApi();
const [showWarningModal, setShowWarningModal] = useState(false);

const { t } = useTranslation();
Expand All @@ -31,20 +36,8 @@ const ProductAndTime = ({
selectedProduct,
} = useStepper();

// values will be used in future
const currentDate = new Date();
const currentMonth = currentDate.getMonth();
const currentYear = currentDate.getFullYear();
const currentDay = currentDate.getDate();
const futureDates = [];

const navigate = useNavigate();

for (let i = 1; i < 4; i++) {
const randomDate = new Date(currentYear, currentMonth, currentDay + i);
futureDates.push(randomDate);
}

// handling the button click for selecting a time and date
const handleSubmit = () => {
if (
Expand All @@ -66,6 +59,29 @@ const ProductAndTime = ({
setShowWarningModal(true);
};

// TODO add before unload delete temp reservation with uuid
useEffect(() => {
const sendDeleteRequestOnUnload = async () => {
window.addEventListener('beforeunload', async () => {
try {
const responce = await deleteRequest(
'delete-temp-reservation/',
randomUUID,
);
console.log('TimeForm.jsx 72 ', responce);
} catch (error) {
console.error('Error sending DELETE request:', error);
}
});
};

sendDeleteRequestOnUnload();

// Cleanup the event listener when the component unmounts
return () => {
window.removeEventListener('beforeunload', sendDeleteRequestOnUnload);
};
}, []);
return (
<>
<PopUpWarningModal
Expand Down Expand Up @@ -100,6 +116,7 @@ const ProductAndTime = ({
if (station.selected) {
return (
<SelectTime
randomUUID={randomUUID}
timeSlots={station.timeSlots}
setSelectedStationAndTime={setSelectedStationAndTime}
selectedStationAndTime={selectedStationAndTime}
Expand Down Expand Up @@ -134,6 +151,7 @@ ProductAndTime.propTypes = {
onProductAndTimeSelected: PropTypes.func.isRequired,
onPrevStep: PropTypes.func.isRequired,
handleWarningModal: PropTypes.func,
randomUUID: PropTypes.string.isRequired,
};

export default ProductAndTime;
26 changes: 25 additions & 1 deletion src/components/TimePeriodButton.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
import Button from 'react-bootstrap/Button';
import PropTypes from 'prop-types';
import style from '../css/TimeButton.module.css';
import useApi from '../hooks/useApi';
import { useStepper } from '../hooks/useStepper';

function TimePeriodButton({
buttonText,
setSelectedTime,
selectedTime,
stationName,
randomUUID,
}) {
const handleClick = () => {
const { postRequest } = useApi();
const { selectedDate, selectedProduct, selectAdaptor } = useStepper();

const handleClick = async () => {
setSelectedTime(stationName, buttonText);
try {
const bodyData = {
uuid: randomUUID,
station: stationName,
timeSlot: buttonText,
product: selectedProduct,
date: selectedDate,
isAdapter: selectAdaptor,
};
const isEmptyField = Object.values(bodyData).some((value) => !value);
if (isEmptyField) {
const response = await postRequest('add-temp-reservation', bodyData);
console.log('TimePeriodButton.jsx 30 ', response);
}
} catch (error) {
console.error('api error: ', error);
}
};

return (
Expand All @@ -35,6 +58,7 @@ TimePeriodButton.propTypes = {
stationName: PropTypes.string.isRequired,
setSelectedTime: PropTypes.func.isRequired,
setSelectedStation: PropTypes.func,
randomUUID: PropTypes.string,
};

export default TimePeriodButton;
1 change: 0 additions & 1 deletion src/components/UserForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import PopUpWarningModal from '../components/PopUpWarningModal';
import hsyLogo from '../assets/hsy_logo_dark.png';
import { useStepper } from '../hooks/useStepper';
import { useTranslation } from 'react-i18next';

const UserForm = ({ onSubmit, onPrevStep }) => {
const [validated, setValidated] = useState(false);
const [showInfoModal, setShowInfoModal] = useState(false);
Expand Down
Loading

0 comments on commit adfd438

Please sign in to comment.