Skip to content

Commit

Permalink
Fix updates to count
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Nov 13, 2024
1 parent 56be1f1 commit f038b08
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
21 changes: 15 additions & 6 deletions packages/client/src/formElements/FilterInputContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ export function FilterInputServiceContextProvider({
updatingCount: boolean;
count: number;
fullCellCount: number;
filterString: string;
}>({
loading: false,
updatingCount: true,
count: 0,
fullCellCount: 0,
filterString: "",
});

const mapContext = useContext(MapContext);
Expand All @@ -107,13 +109,16 @@ export function FilterInputServiceContextProvider({
.then((res) => res.json())
.then((metadata: FilterServiceMetadata) => {
setState((prev) => {
const count =
// @ts-ignore
metadata.attributes.find((a) => a.attribute === "id")?.count || 0;
return {
...prev,
metadata,
loading: false,
fullCellCount:
// @ts-ignore
metadata.attributes.find((a) => a.attribute === "id")?.count || 0,
fullCellCount: count,
count,
updatingCount: false,
};
});
})
Expand Down Expand Up @@ -158,17 +163,21 @@ export function FilterInputServiceContextProvider({
)
);
filterLayerManager.updateFilter(filterString);
if (filterString.length === 0) {
if (filterString === state.filterString) {
// do nothing
} else if (filterString.length === 0) {
setState((prev) => ({
...prev,
updatingCount: false,
count: prev.fullCellCount,
filterString: "",
}));
} else if (serviceLocation && state.fullCellCount === 0) {
} else if (serviceLocation) {
// TODO: update count from service
setState((prev) => ({
...prev,
updatingCount: true,
filterString,
}));
fetch(
`${serviceLocation.replace(/\/$/, "")}/count?filter=${filterString}`
Expand Down Expand Up @@ -299,7 +308,7 @@ export function filterStateToSearchString(filters: FilterState) {
} = {};
for (const attr in filters) {
const filter = filters[attr];
if (filter.selected) {
if (filter && filter.selected) {
if ("numberState" in filter) {
state[attr] = filter.numberState!;
} else if ("stringState" in filter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Spinner from "../../components/Spinner";

export default function FilteredPlanningUnitCountHeader() {
const context = useContext(FilterInputServiceContext);
console.log("context", context);
return (
<div
className={`w-full flex items-center p-2 px-4 border-b bg-gray-50 text-sm ${
Expand Down

0 comments on commit f038b08

Please sign in to comment.