Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add breakfast #56

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/features/check-in-out/CheckinBooking.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useMoveBack } from '@/hooks/useMoveBack';
import { useBooking } from '../bookings/useBooking';
import { formatCurrency } from '@/utils/helpers';
import { useCheckin } from './useCheckin';
import { useSettings } from '../settings/useSettings';

const Box = styled.div`
/* Box */
Expand All @@ -22,15 +23,18 @@ const Box = styled.div`

function CheckinBooking() {
const [confirmPaid, setConfirmPaid] = useState(false);
const [addBreakfast, setAddBreakfast] = useState(false);
const { booking, isLoading } = useBooking();
const moveBack = useMoveBack();
const { checkin, isCheckingIn } = useCheckin();
const { settings, isLoading: isLoadingSettings } = useSettings();

console.log('Why this is not working?: ', booking);

useEffect(() => {
setConfirmPaid(booking?.is_paid ?? false);
}, [booking]);

if (isLoading) return <Spinner />;
if (isLoading & isLoadingSettings) return <Spinner />;
const {
id: bookingId,
guests,
Expand All @@ -40,13 +44,32 @@ function CheckinBooking() {
num_nights: numNights,
} = booking;

// calculate breakfast price
const optionBreakfastPrice = settings?.breakfastPrice * numGuests * numNights;

const totalPriceWithBreakfast = formatCurrency(totalPrice + optionBreakfastPrice);
const totalPriceBreadDown =
formatCurrency(totalPrice) + ' + ' + formatCurrency(optionBreakfastPrice);

function handleCheckin() {
setConfirmPaid((confirm) => !confirm);
}

function handleClick() {
if (!confirmPaid) return;
checkin(bookingId);
// why snakecase? because the API expects snakecase. see the table structure in the database.
if (addBreakfast) {
checkin({
bookingId,
breakfast: {
has_breakfast: true,
extras_price: optionBreakfastPrice,
total_price: totalPrice + optionBreakfastPrice,
},
});
} else {
checkin({ bookingId, breakfast: {} });
}
}

return (
Expand All @@ -57,6 +80,22 @@ function CheckinBooking() {
</Row>

<BookingDataBox booking={booking} />

{!hasBreakfast && (
<Box>
<Checkbox
checked={addBreakfast}
onChange={() => {
setAddBreakfast((add) => !add);
setConfirmPaid(false);
}}
id='addBreakfast'
>
Add breakfast for {numGuests} guests for {numNights} nights for a total of{' '}
{formatCurrency(optionBreakfastPrice)}.
</Checkbox>
</Box>
)}
{/* TODO: on confirm check box, it needs to disabled when payment is received when check in is commited. */}
{/* the query will be refetched and this checkbox will be disabled. at the moment we will live with this bug. */}
<Box>
Expand All @@ -67,7 +106,10 @@ function CheckinBooking() {
disabled={confirmPaid || isCheckingIn}
>
I confirm that {guests.full_name} has paid the total amount of{' '}
{formatCurrency(totalPrice)}.
{!addBreakfast
? formatCurrency(totalPrice)
: `${totalPriceWithBreakfast} including breakfast ( ${totalPriceBreadDown} ) `}
.
</Checkbox>
</Box>
<ButtonGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/features/check-in-out/useCheckin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export function useCheckin() {
const queryClient = useQueryClient();
const navigate = useNavigate();
const { mutate: checkin, isLoading: isCheckingIn } = useMutation({
mutationFn: (bookingId) => updateBooking(bookingId, { status: 'checked-in', is_paid: true }),
mutationFn: ({ bookingId, breakfast }) => {
updateBooking(bookingId, { status: 'checked-in', is_paid: true, ...breakfast });
},
// onSuccess is a callback that will be called when the mutation is successful
// it will receive the data returned by the mutationFn
onSuccess: (data) => {
console.log(data);
toast.success(`Booking ${data.id} checked in successfully.`);
queryClient.invalidateQueries({ active: true }); // refetch the active bookings
navigate('/'); // navigate to the home page once successful
Expand Down
2 changes: 0 additions & 2 deletions src/ui/ActionControls/Menus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ function Menus({ children }) {
const [parentRef, setParentRef] = useState(null); // this will parent element.

const close = () => setOpenId('');
console.log('openId', openId);
const open = (openId) => setOpenId(openId);
return (
<MenusContext.Provider
Expand All @@ -103,7 +102,6 @@ function Toggle({ id }) {
});

setParentRef(e.currentTarget.parentEelement);
console.log(boundingRect);
openId === id ? close() : open(id);
};
return (
Expand Down
Loading