Skip to content

Commit

Permalink
[TM-1466] sort list of polygons
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarLima1 committed Dec 16, 2024
1 parent 3c54477 commit b55f742
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,8 @@ const DataCard = ({
const basename = useBasename();
const mapFunctions = useMap();
const { record } = useShowContext();
const { polygonsIndicator, treeCoverLossData, treeCoverLossFiresData, isLoadingIndicator } = useMonitoredData(
type!,
record.uuid
);
const { polygonsIndicator, treeCoverLossData, treeCoverLossFiresData, isLoadingIndicator, polygonOptions } =
useMonitoredData(type!, record.uuid);
const filteredPolygonsIndicator =
selectedPolygonUuid !== "0"
? polygonsIndicator?.filter((polygon: any) => polygon.poly_id === selectedPolygonUuid)
Expand Down Expand Up @@ -428,14 +426,6 @@ const DataCard = ({
const [topHeaderSecondTable, setTopHeaderSecondTable] = useState("70px");
const totalElemIndicator = filteredPolygonsIndicator?.length ? filteredPolygonsIndicator?.length - 1 : null;

const polygonsList = [
{ title: "All Polygons", value: "0" },
...(polygonsIndicator ?? []).map((item: any) => ({
title: item.poly_name,
value: item.poly_id
}))
];

useEffect(() => {
if (typeof window !== "undefined") {
const width = window.innerWidth;
Expand Down Expand Up @@ -661,7 +651,7 @@ const DataCard = ({
})}
optionsClassName="!w-max right-0"
className="w-max"
options={polygonsList}
options={polygonOptions}
defaultValue={["0"]}
onChange={option => setSelectedPolygonUuid(option[0])}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useT } from "@transifex/react";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";

import { ModalId } from "@/components/extensive/Modal/ModalConst";
import { useModalContext } from "@/context/modal.provider";
Expand Down Expand Up @@ -82,13 +82,19 @@ type InterfaceIndicatorPolygonsStatus = {
approved: number;
};

interface PolygonOption {
title: string;
value: string;
}

export const useMonitoredData = (entity?: EntityName, entity_uuid?: string) => {
const t = useT();
const { searchTerm, indicatorSlug, setLoadingAnalysis, setIndicatorSlugAnalysis } = useMonitoredDataContext();
const { modalOpened } = useModalContext();
const [isLoadingVerify, setIsLoadingVerify] = useState<boolean>(false);
const { openNotification } = useNotificationContext();
const [treeCoverLossData, setTreeCoverLossData] = useState<Indicators[]>([]);
const [polygonOptions, setPolygonOptions] = useState<PolygonOption[]>([{ title: "All Polygons", value: "0" }]);
const [treeCoverLossFiresData, setTreeCoverLossFiresData] = useState<Indicators[]>([]);
const [analysisToSlug, setAnalysisToSlug] = useState<any>({
treeCoverLoss: [],
Expand Down Expand Up @@ -172,11 +178,33 @@ export const useMonitoredData = (entity?: EntityName, entity_uuid?: string) => {
}
);

const filteredPolygons = indicatorData?.filter(
(polygon: Indicators) =>
polygon?.poly_name?.toLowerCase().includes(searchTerm?.toLowerCase()) ||
polygon?.site_name?.toLowerCase().includes(searchTerm?.toLowerCase())
);
const filteredPolygons = useMemo(() => {
if (!indicatorData) return [];

return indicatorData
.filter(
(polygon: Indicators) =>
polygon?.poly_name?.toLowerCase().includes(searchTerm?.toLowerCase()) ||
polygon?.site_name?.toLowerCase().includes(searchTerm?.toLowerCase())
)
.sort((a, b) => (a.poly_name || "").localeCompare(b.poly_name || ""));
}, [indicatorData, searchTerm]);

useEffect(() => {
if (!indicatorData) return;

const options = [
{ title: "All Polygons", value: "0" },
...indicatorData
.map((item: any) => ({
title: item.poly_name || "",
value: item.poly_id || ""
}))
.sort((a, b) => a.title.localeCompare(b.title))
];

setPolygonOptions(options);
}, [indicatorData]);

const headerBarPolygonStatus = dataPolygonOverview.map(status => {
const key = status.status_key as keyof InterfaceIndicatorPolygonsStatus;
Expand Down Expand Up @@ -251,6 +279,7 @@ export const useMonitoredData = (entity?: EntityName, entity_uuid?: string) => {

return {
polygonsIndicator: filteredPolygons,
polygonOptions,
indicatorPolygonsStatus,
headerBarPolygonStatus,
totalPolygonsStatus: totalPolygonsApproved,
Expand Down

0 comments on commit b55f742

Please sign in to comment.