Skip to content

Commit

Permalink
can now calculate money from booking dates
Browse files Browse the repository at this point in the history
  • Loading branch information
kemboi590 committed Jul 22, 2024
1 parent d4b0f62 commit 4193e7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@stripe/stripe-js": "^4.1.0",
"axios": "^1.7.2",
"date-fns": "^3.6.0",
"dayjs": "^1.11.12",
"flowbite": "^2.4.1",
"flowbite-react": "^0.10.1",
"lucide-react": "^0.400.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

38 changes: 27 additions & 11 deletions src/pages/dashboard/main/Bookings/BookingForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { useForm, SubmitHandler, useWatch } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup";
import * as yup from "yup";
import { Toaster, toast } from 'sonner';
Expand All @@ -10,7 +10,7 @@ import { bookingVehicleAPI } from "../../../../features/booking/bookingAPI";
import { vehiclesAPI } from "../../../../features/vehicles/Vehicles";
import { ArrowLeft } from 'lucide-react';
import { useNavigate } from "react-router-dom";

import dayjs from 'dayjs';

type BookingFormData = {
booking_date: Date;
Expand All @@ -19,8 +19,11 @@ type BookingFormData = {
};

const schema = yup.object().shape({
booking_date: yup.date().required("Booking date is required"),
return_date: yup.date().required("Return date is required"),
booking_date: yup.date().required("Booking date is required").min(new Date(), "Booking date cannot be in the past"),
return_date: yup.date().required("Return date is required").when('booking_date', {
is: (booking_date: Date) => !!booking_date,
then: (schema) => schema.min(yup.ref('booking_date'), "Return date cannot be before booking date"),
})
});

const BookingForm = () => {
Expand All @@ -30,7 +33,6 @@ const BookingForm = () => {
const parsedVehicleId = vehicle_id ? parseInt(vehicle_id, 10) : NaN;

if (isNaN(parsedVehicleId)) {
console.error("Invalid vehicle ID");
return <div>Invalid vehicle ID</div>;
}

Expand All @@ -41,29 +43,42 @@ const BookingForm = () => {
const externalData = {
user_id: user.user?.userID,
vehicle_id: parsedVehicleId,
total_amount: vehicleData?.rental_rate
};

const { register, handleSubmit, formState: { errors } } = useForm<BookingFormData>({ resolver: yupResolver(schema) });
const { register, handleSubmit, formState: { errors }, control } = useForm<BookingFormData>({ resolver: yupResolver(schema) });

const booking_date = useWatch({ control, name: "booking_date" });
const return_date = useWatch({ control, name: "return_date" });

const calculateTotalAmount = () => {
if (booking_date && return_date) {
const bookingDate = dayjs(booking_date);
const returnDate = dayjs(return_date);
const numberOfDays = returnDate.diff(bookingDate, 'day') + 1;
return numberOfDays * Number(vehicleData?.rental_rate);
}
return '😋🤑';
};

const totalAmount = calculateTotalAmount();

const onSubmit: SubmitHandler<BookingFormData> = async (formData) => {
const dataToSubmit = {
...externalData,
...formData
...formData,
total_amount: totalAmount.toString(),
};

try {
setIsSubmitting(true);
const response = await bookingVehicle(dataToSubmit).unwrap();
console.log("Booking created successfully", response);
await bookingVehicle(dataToSubmit).unwrap();
toast.success("Booking created successfully");

setTimeout(() => {
navigate('/dashboard/payments');
}, 1000);

} catch (err) {
console.error("Error creating booking", err);
toast.error("Error creating booking");
} finally {
setIsSubmitting(false);
Expand Down Expand Up @@ -110,6 +125,7 @@ const BookingForm = () => {
<p className="text-lg"><strong>Seating Capacity:</strong> {vehicleData.vehicle_specifications.seating_capacity}</p>
<p className="text-lg"><strong>Transmission:</strong> {vehicleData.vehicle_specifications.transmission}</p>
<p className="text-base col-span-1 sm:col-span-2"><strong>Features:</strong> {vehicleData.vehicle_specifications.features}</p>
<p className="text-lg text-blue-600"><strong>You will pay:</strong> {totalAmount}</p>
</div>
</div>
</div>
Expand Down

0 comments on commit 4193e7b

Please sign in to comment.