Skip to content

Commit

Permalink
add search params to sort
Browse files Browse the repository at this point in the history
  • Loading branch information
RODO94 committed Jan 14, 2025
1 parent 20de7ef commit 9b7aa3f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions editor.planx.uk/src/ui/editor/SortFlowsSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PublishedFlowSummary,
} from "pages/FlowEditor/lib/store/editor";
import React, { useEffect, useState } from "react";
import { useNavigation } from "react-navi";

import SelectInput from "./SelectInput/SelectInput";

Expand Down Expand Up @@ -64,6 +65,32 @@ export const SortFlowsSelect = ({
}) => {
const [sortBy, setSortBy] = useState<SortObject>(sortArray[0]);
const [sortDirection, setSortDirection] = useState<SortDirection>("asc");

const navigation = useNavigation();

const addToSearchParams = (sortKey: SortTypes) => {
navigation.navigate(
{
pathname: window.location.pathname,
search: `?sort=${sortKey}`,
},
{
replace: true,
},
);
};

useEffect(() => {
const params = new URLSearchParams(window.location.search);
const urlSort = params.get("sort") as SortTypes;
const newSortObj = sortArray.find(
(sort) =>
sort.sortKey === urlSort ||
(sort.sortKey === "publishedFlows" && sort.nestedKey === urlSort),
);
newSortObj && setSortBy(newSortObj);
}, []);

useEffect(() => {
const { sortKey } = sortBy;

Expand All @@ -81,11 +108,13 @@ export const SortFlowsSelect = ({
return sortFlowList(aValue, bValue, sortDirection);
});
sortedFlows && setFlows([...sortedFlows]);
addToSearchParams(sortBy.nestedKey);
} else {
const sortedFlows = flows?.sort((a: FlowSummary, b: FlowSummary) =>
sortFlowList(a[sortKey], b[sortKey], sortDirection),
);
sortedFlows && setFlows([...sortedFlows]);
addToSearchParams(sortBy.sortKey);
}
}, [sortBy, sortDirection]);

Expand Down

0 comments on commit 9b7aa3f

Please sign in to comment.