Skip to content

Commit 365a178

Browse files
authored
Merge pull request #1682 from rockingrohit9639/feature/bookings-sorting
feat(booking-sorting): add SortBy on bookings index
2 parents e525367 + 6f75f94 commit 365a178

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app/modules/booking/service.server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
User,
99
UserOrganization,
1010
} from "@prisma/client";
11+
import type { SortingDirection } from "~/components/list/filters/sort-by";
1112
import { db } from "~/database/db.server";
1213
import { bookingUpdatesTemplateString } from "~/emails/bookings-updates-template";
1314
import { sendEmail } from "~/emails/mail.server";
@@ -542,6 +543,8 @@ export async function getBookings(params: {
542543
extraInclude?: Prisma.BookingInclude;
543544
/** Controls whether entries should be paginated or not */
544545
takeAll?: boolean;
546+
orderBy?: string;
547+
orderDirection?: SortingDirection;
545548
}) {
546549
const {
547550
organizationId,
@@ -558,6 +561,8 @@ export async function getBookings(params: {
558561
userId,
559562
extraInclude,
560563
takeAll = false,
564+
orderBy = "from",
565+
orderDirection = "asc",
561566
} = params;
562567

563568
try {
@@ -692,7 +697,7 @@ export async function getBookings(params: {
692697
},
693698
...(extraInclude || undefined),
694699
},
695-
orderBy: { from: "asc" },
700+
orderBy: { [orderBy]: orderDirection },
696701
}),
697702
db.booking.count({ where }),
698703
]);

app/routes/_layout+/bookings.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import LineBreakText from "~/components/layout/line-break-text";
1919
import { List } from "~/components/list";
2020
import { ListContentWrapper } from "~/components/list/content-wrapper";
2121
import { Filters } from "~/components/list/filters";
22+
import type { SortingDirection } from "~/components/list/filters/sort-by";
23+
import { SortBy } from "~/components/list/filters/sort-by";
2224
import { Badge } from "~/components/shared/badge";
2325
import { Button } from "~/components/shared/button";
2426
import { Td, Th } from "~/components/table";
@@ -80,6 +82,10 @@ export async function loader({ context, request }: LoaderFunctionArgs) {
8082
const cookie = await updateCookieWithPerPage(request, perPageParam);
8183
const { perPage } = cookie;
8284

85+
const orderBy = searchParams.get("orderBy") ?? "from";
86+
const orderDirection = (searchParams.get("orderDirection") ??
87+
"asc") as SortingDirection;
88+
8389
/**
8490
* For self service and base users, we need to get the teamMember to be able to filter by it as well.
8591
* This is to handle a case when a booking was assigned when there wasn't a user attached to a team member but they were later on linked.
@@ -127,6 +133,8 @@ export async function loader({ context, request }: LoaderFunctionArgs) {
127133
}),
128134
custodianTeamMemberIds: teamMemberIds,
129135
...selfServiceData,
136+
orderBy,
137+
orderDirection,
130138
}),
131139

132140
// team members/custodian
@@ -222,6 +230,12 @@ export const shouldRevalidate: ShouldRevalidateFunction = ({
222230
return defaultShouldRevalidate;
223231
};
224232

233+
const BOOKING_SORTING_OPTIONS = {
234+
from: "From Date",
235+
to: "To Date",
236+
name: "Name",
237+
} as const;
238+
225239
export default function BookingsIndexPage({
226240
className,
227241
disableBulkActions = false,
@@ -292,6 +306,13 @@ export default function BookingsIndexPage({
292306
<Filters
293307
slots={{
294308
"left-of-search": <StatusFilter statusItems={BookingStatus} />,
309+
"right-of-search": (
310+
<SortBy
311+
sortingOptions={BOOKING_SORTING_OPTIONS}
312+
defaultSortingBy="from"
313+
defaultSortingDirection="asc"
314+
/>
315+
),
295316
}}
296317
>
297318
<When

0 commit comments

Comments
 (0)