Skip to content

Commit df4ef13

Browse files
authored
Merge pull request #324 from maykinmedia/feature/#316-ordering-frontend
✨ - feat: ordering
2 parents f733956 + a845d0e commit df4ef13

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

frontend/src/lib/api/destructionLists.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isPrimitive } from "@maykin-ui/admin-ui";
2+
import { URLSearchParams } from "url";
23

34
import { Zaak } from "../../types";
45
import { User } from "./auth";
@@ -110,8 +111,10 @@ export async function getDestructionList(uuid: string) {
110111
/**
111112
* List destruction lists.
112113
*/
113-
export async function listDestructionLists() {
114-
const response = await request("GET", "/destruction-lists/");
114+
export async function listDestructionLists(
115+
params?: URLSearchParams | { ordering?: string },
116+
) {
117+
const response = await request("GET", "/destruction-lists/", params);
115118
const promise: Promise<DestructionList[]> = response.json();
116119
return promise;
117120
}

frontend/src/pages/landing/Landing.loader.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ export interface LandingContext {
1414
}
1515

1616
export const landingLoader = loginRequired(
17-
async (): Promise<LandingContext> => {
18-
const statusMap = await getStatusMap();
17+
async ({ request }): Promise<LandingContext> => {
18+
const url = new URL(request.url);
19+
const queryParams = url.searchParams;
20+
const orderQuery = queryParams.get("ordering");
21+
const statusMap = await getStatusMap(orderQuery);
1922
const user = await whoAmI();
2023

2124
return {
@@ -25,8 +28,10 @@ export const landingLoader = loginRequired(
2528
},
2629
);
2730

28-
export const getStatusMap = async () => {
29-
const lists = await listDestructionLists();
31+
export const getStatusMap = async (orderQuery: string | null) => {
32+
const lists = await listDestructionLists({
33+
ordering: orderQuery ?? "-created",
34+
});
3035
return STATUSES.reduce((acc, val) => {
3136
const status = val[0] || "";
3237
const destructionLists = lists.filter(

frontend/src/pages/landing/Landing.tsx

+31-3
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ export const Landing = () => {
8888
const { statusMap, user } = useLoaderData() as LandingContext;
8989
const navigate = useNavigate();
9090
const revalidator = useRevalidator();
91+
9192
usePoll(async () => {
92-
const _statusMap = await getStatusMap();
93+
const orderQuery = new URLSearchParams(window.location.search).get(
94+
"ordering",
95+
);
96+
const _statusMap = await getStatusMap(orderQuery);
9397
const equal = JSON.stringify(_statusMap) === JSON.stringify(statusMap);
9498
if (!equal) {
9599
revalidator.revalidate();
@@ -193,6 +197,25 @@ export const Landing = () => {
193197
};
194198
}),
195199
);
200+
201+
const sortOptions = [
202+
{ label: "Nieuwste eerst", value: "-created" },
203+
{ label: "Oudste eerst", value: "created" },
204+
];
205+
206+
const selectedSort =
207+
new URLSearchParams(window.location.search).get("ordering") || "-created";
208+
209+
const sortedOptions = sortOptions.map((option) => ({
210+
...option,
211+
selected: option.value === selectedSort,
212+
}));
213+
214+
const onChangeSort = (event: React.ChangeEvent<HTMLSelectElement>) => {
215+
// update the query string
216+
navigate(`?ordering=${event.target.value}`);
217+
};
218+
196219
return (
197220
<KanbanTemplate
198221
kanbanProps={{
@@ -201,10 +224,15 @@ export const Landing = () => {
201224
objectLists: objectLists,
202225
toolbarProps: {
203226
items: [
204-
// TODO: SORT
227+
{
228+
direction: "horizontal",
229+
label: "Sorteren",
230+
required: true,
231+
options: sortedOptions,
232+
onChange: onChangeSort,
233+
},
205234
"spacer",
206235
{
207-
// TODO: LINK
208236
children: (
209237
<>
210238
<Solid.DocumentPlusIcon />

0 commit comments

Comments
 (0)