Skip to content

Commit

Permalink
Add type Timestamp to lastItem instead of any, change the field from
Browse files Browse the repository at this point in the history
calendarEventId to requestedAt and change the order to be descending.
  • Loading branch information
nimanns committed Nov 24, 2024
1 parent ce90a0d commit 65820b4
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export default function useExistingBooking() {
setBookingCalendarInfo,
setFormData,
} = useContext(BookingContext);
const { bookings, roomSettings } = useContext(DatabaseContext);
const { futureBookings, roomSettings } = useContext(DatabaseContext);

const findBooking = (calendarEventId: string) =>
bookings.filter(
futureBookings.filter(
(booking) => booking.calendarEventId === calendarEventId
)[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { useAuth } from "@/components/src/client/routes/components/AuthProvider";
import { fetchAllBookings, fetchAllFutureBooking } from "@/components/src/server/db";
import { clientFetchAllDataFromCollection } from "@/lib/firebase/firebase";
import { Timestamp } from "firebase-admin/firestore";

export interface DatabaseContextType {
adminUsers: AdminUser[];
Expand Down Expand Up @@ -103,7 +104,7 @@ export const DatabaseProvider = ({
const [userApiData, setUserApiData] = useState<UserApiData | undefined>(
undefined
);
const [lastItem, setLastItem] = useState<any>(null);
const [lastItem, setLastItem] = useState<Timestamp>(null);
const LIMIT = 3;

const { user } = useAuth();
Expand Down Expand Up @@ -190,7 +191,7 @@ export const DatabaseProvider = ({
const fetchBookings = async () => {
try{
const bookingsResponse : Booking[] = await fetchAllBookings(LIMIT, lastItem);
setLastItem(bookingsResponse[bookingsResponse.length - 1].calendarEventId);
setLastItem(bookingsResponse[bookingsResponse.length - 1].requestedAt);
setAllBookings((oldBookings) => [...oldBookings, ...bookingsResponse]);
} catch (error) {
console.error("Error fetching data:", error);
Expand Down
2 changes: 1 addition & 1 deletion booking-app/components/src/server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const fetchAllFutureBooking = async <Booking>(): Promise<Booking[]> => {
};

export const fetchAllBookings = async <Booking>(limit:number, last:any) : Promise<Booking[]> =>{
return getPaginatedData<Booking>(TableNames.BOOKING, limit, "calendarEventId", last);
return getPaginatedData<Booking>(TableNames.BOOKING, limit, "requestedAt", last);
}


Expand Down
8 changes: 4 additions & 4 deletions booking-app/lib/firebase/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const clientFetchAllDataFromCollectionWithLimitAndOffset = async <T>(
export const getPaginatedData = async<T> (
collectionName,
itemsPerPage = 10,
orderByField = 'calendarEventId',
orderByField = 'requestedAt',
lastVisible = null
) : Promise<T[]> => {
try {
Expand All @@ -98,7 +98,7 @@ export const getPaginatedData = async<T> (
// Build query
let q = query(
colRef,
orderBy(orderByField),
orderBy(orderByField, 'desc'),
limit(itemsPerPage)
);

Expand All @@ -107,7 +107,7 @@ export const getPaginatedData = async<T> (
console.log(lastVisible);
q = query(
colRef,
orderBy(orderByField),
orderBy(orderByField, 'desc'),
startAfter(lastVisible),
limit(itemsPerPage)
);
Expand All @@ -121,7 +121,7 @@ export const getPaginatedData = async<T> (
id: doc.id,
...doc.data() as unknown as T
}));

console.log(items)
return items;
} catch (error) {
console.error('Error getting paginated data:', error);
Expand Down
71 changes: 71 additions & 0 deletions booking-app/playwright-report/index.html

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions booking-app/test-results/.last-run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status": "passed",
"failedTests": []
}

0 comments on commit 65820b4

Please sign in to comment.