Skip to content

Commit

Permalink
Remove layers from legend
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarrenechea committed Nov 28, 2023
1 parent e849400 commit d381afe
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
2 changes: 2 additions & 0 deletions client/src/components/map/legend/item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const LegendItem: React.FC<PropsWithChildren & LegendItemProps> = ({
// events
onChangeOpacity,
onChangeVisibility,
onRemove,
}) => {
const validChildren = useMemo(() => {
const chldn = Children.map(children, (Child) => {
Expand Down Expand Up @@ -78,6 +79,7 @@ export const LegendItem: React.FC<PropsWithChildren & LegendItemProps> = ({
settingsManager={settingsManager}
onChangeOpacity={onChangeOpacity}
onChangeVisibility={onChangeVisibility}
onRemove={onRemove}
InfoContent={InfoContent}
/>
</header>
Expand Down
28 changes: 27 additions & 1 deletion client/src/components/map/legend/item/toolbar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";

import { PopoverArrow } from "@radix-ui/react-popover";
import { LuEye, LuEyeOff, LuDroplet, LuInfo } from "react-icons/lu";
import { LuEye, LuEyeOff, LuDroplet, LuInfo, LuX } from "react-icons/lu";

import { cn } from "@/lib/classnames";

Expand All @@ -17,6 +17,7 @@ export const LegendItemToolbar: React.FC<LegendItemToolbarProps> = ({
InfoContent,
settings,
settingsManager,
onRemove,
onChangeOpacity,
onChangeVisibility,
}: LegendItemToolbarProps) => {
Expand Down Expand Up @@ -134,6 +135,31 @@ export const LegendItemToolbar: React.FC<LegendItemToolbarProps> = ({
</Dialog>
</div>
)}

{onRemove && (
<div className="flex items-start">
<Tooltip delayDuration={500}>
<TooltipTrigger
type="button"
aria-label="Show info"
className={cn({
"pointer-events-none": popoverOpen,
})}
onClick={() => {
if (onRemove) onRemove();
}}
>
<LegendItemButton Icon={LuX} />
</TooltipTrigger>

<TooltipContent side="top" align="end" alignOffset={-10}>
<div className="text-xxs">Remove layer</div>

<TooltipArrow className="fill-white" width={10} height={5} />
</TooltipContent>
</Tooltip>
</div>
)}
</div>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/map/legend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type OnChangeOrder = (id: string[]) => void;
type OnChangeOpacity = (opacity: number) => void;
type OnChangeVisibility = (visibility: boolean) => void;
type OnChangeColumn = (column: string) => void;
type OnRemove = () => void;

export type Settings = Record<string, unknown> & {
opacity?: number;
Expand All @@ -31,6 +32,7 @@ export type LegendItemEvents = {
onChangeOpacity?: OnChangeOpacity;
onChangeVisibility?: OnChangeVisibility;
onChangeColumn?: OnChangeColumn;
onRemove?: OnRemove;
};
/*
* Legend
Expand Down
24 changes: 22 additions & 2 deletions client/src/containers/map/legend/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useGetLayersId } from "@/types/generated/layer";
import { LayerTyped, LegendConfig } from "@/types/layers";
import { LegendType } from "@/types/legend";

import { useSyncLayersSettings } from "@/app/store";
import { useSyncDatasets, useSyncLayers, useSyncLayersSettings } from "@/app/store";

import LegendItem from "@/components/map/legend/item";
import {
Expand Down Expand Up @@ -45,10 +45,12 @@ const getSettingsManager = (data: LayerTyped = {} as LayerTyped): SettingsManage
};

const MapLegendItem = ({ id, ...props }: MapLegendItemProps) => {
const [, setLayers] = useSyncLayers();
const [, setDatasets] = useSyncDatasets();
const [layersSettings] = useSyncLayersSettings();

const { data, isError, isFetched, isFetching, isPlaceholderData } = useGetLayersId(id, {
populate: "metadata",
populate: "metadata,dataset",
});

const attributes = data?.data?.attributes as LayerTyped;
Expand Down Expand Up @@ -92,6 +94,24 @@ const MapLegendItem = ({ id, ...props }: MapLegendItemProps) => {
name={attributes?.name}
settingsManager={settingsManager}
{...props}
onRemove={() => {
setDatasets((prev) => {
const current = [...prev];
const index = current.indexOf(attributes?.dataset?.data?.id as number);
if (index > -1) {
current.splice(index, 1);
}
return current;
});
setLayers((prev) => {
const current = [...prev];
const index = current.indexOf(id);
if (index > -1) {
current.splice(index, 1);
}
return current;
});
}}
// InfoContent={!!metadata && <Metadata {...attributes} />}
>
{LEGEND_COMPONENT}
Expand Down

0 comments on commit d381afe

Please sign in to comment.