Skip to content

Commit

Permalink
feat(room-booking): scroll to a date from search param
Browse files Browse the repository at this point in the history
  • Loading branch information
evermake committed Nov 1, 2024
1 parent 6539754 commit f635d66
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/app/routes/_with_menu/room-booking.index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { RoomBookingPage } from "@/components/room-booking/timeline/RoomBookingP
import { createFileRoute } from "@tanstack/react-router";
import { Helmet } from "react-helmet-async";

type RoomBookingSearch = {
d?: number;
};

export const Route = createFileRoute("/_with_menu/room-booking/")({
component: () => (
<>
Expand All @@ -20,4 +24,16 @@ export const Route = createFileRoute("/_with_menu/room-booking/")({
<RoomBookingPage />
</>
),
validateSearch: (search): RoomBookingSearch => {
const unix =
typeof search.d === "number"
? search.d
: typeof search.d === "string"
? Number.parseInt(search.d)
: NaN;
if (!Number.isNaN(unix) && unix > Date.UTC(0)) {
return { d: unix };
}
return {};
},
});
13 changes: 4 additions & 9 deletions src/components/room-booking/timeline/BookingTimeline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import {
} from "@/lib/utils/dates.ts";
import { useMediaQuery, useNow } from "@vueuse/core";
import type { MaybeRef } from "vue";
import {
computed,
onMounted,
ref,
shallowRef,
unref,
watch,
} from "vue"; /* ========================================================================== */
import { computed, onMounted, ref, shallowRef, unref, watch } from "vue";
/* ========================================================================== */
/* ================================ Options ================================= */
Expand All @@ -40,6 +33,8 @@ const emit = defineEmits<{
bookingClick: [booking: Booking];
}>();
defineExpose({ scrollTo });
/* ========================================================================== */
/* =============================== Constants ================================ */
/* ========================================================================== */
Expand Down Expand Up @@ -959,7 +954,7 @@ function handleBookingClick(event: MouseEvent) {
/* =============================== Scrolling ================================ */
/* ========================================================================== */
type ScrollToOptions = {
export type ScrollToOptions = {
/** Date to scroll to. */
to: Date;
/** Behavior of scroll. */
Expand Down
33 changes: 31 additions & 2 deletions src/components/room-booking/timeline/RoomBookingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,45 @@ import { $roomBooking } from "@/api/room-booking";
import { AuthWall } from "@/components/common/AuthWall.tsx";
import { BookingModal } from "@/components/room-booking/timeline/BookingModal.tsx";
import { T } from "@/lib/utils/dates.ts";
import { lazy, Suspense, useState } from "react";
import type { Booking, Slot } from "./BookingTimeline.vue";
import { lazy, Suspense, useEffect, useRef, useState } from "react";
import type { Booking, ScrollToOptions, Slot } from "./BookingTimeline.vue";
import { getRouteApi } from "@tanstack/react-router";

const BookingTimeline = lazy(
() => import("@/components/room-booking/timeline/BookingTimeline.tsx"),
);

type TimelineRef = {
__veauryVueRef__: {
scrollTo: (options: ScrollToOptions) => void;
};
};

const routeApi = getRouteApi("/_with_menu/room-booking/");

export function RoomBookingPage() {
const search = routeApi.useSearch();
const [modalOpen, setModalOpen] = useState(false);
const [newBookingSlot, setNewBookingSlot] = useState<Slot>();
const [bookingDetails, setBookingDetails] = useState<Booking>();
const timelineRef = useRef<TimelineRef | null>(null);
const [timelineLoaded, setTimelineLoaded] = useState(false);

const setTimelineRef = (x: TimelineRef) => {
timelineRef.current = x;
setTimelineLoaded(true);
};

useEffect(() => {
if (timelineLoaded && search.d) {
timelineRef.current?.__veauryVueRef__.scrollTo({
to: new Date(search.d),
behavior: "smooth",
offsetMs: T.Min * 20,
position: "left",
});
}
}, [timelineLoaded, search.d]);

const { me } = useMe();

Expand Down Expand Up @@ -68,6 +96,7 @@ export function RoomBookingPage() {
setNewBookingSlot(undefined);
setModalOpen(true);
}}
ref={setTimelineRef}
/>
</Suspense>
</div>
Expand Down

0 comments on commit f635d66

Please sign in to comment.