Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve map popup component #1002

Merged
merged 6 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 17 additions & 43 deletions frontend/src/components/Map/DetailsMap/PointsSecondary.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import React, { FC, useMemo } from 'react';
import { useMemo } from 'react';
import Image from 'next/image';
import { Tooltip } from 'react-leaflet';
import { Signage } from 'components/Icons/Signage';
import { renderToStaticMarkup } from 'react-dom/server';
import { SignageDictionary } from 'modules/signage/interface';
import styled, { css } from 'styled-components';
import { desktopOnly, getSpacing } from 'stylesheet';
import { RawCoordinate2D } from 'modules/interface';
import { InfrastructureDictionary } from 'modules/infrastructure/interface';
import { FormattedMessage } from 'react-intl';
import { HoverableMarker } from '../components/HoverableMarker';

export type PointsSecondaryProps = {
dictionary?: SignageDictionary | InfrastructureDictionary | null;
icon?: FC;
icon?: React.FC;
};

type Locations = {
Expand Down Expand Up @@ -57,14 +56,23 @@ export const PointsSecondary: React.FC<PointsSecondaryProps> = ({
pictogramUri={location.pictogramUri}
type={null}
>
<StyledTooltip>
<Tooltip className="!p-0 !border-0 !rounded-xl !overflow-hidden w-55 desktop:w-70 !whitespace-normal">
<div className="flex flex-col">
{location.imageUrl !== null && <CoverImage src={location.imageUrl} alt="" />}
{location.imageUrl !== null && (
<Image
loading="lazy"
className="h-40 w-auto desktop:h-30 object-cover"
width={300}
height={130}
src={location.imageUrl}
alt=""
/>
)}
<div className="p-4">
<div className="text-P2 mb-1 text-greyDarkColored">{location.type}</div>
<Name className="text-Mobile-C1 text-primary1 font-bold desktop:text-H4 line-clamp-2">
<h3 className="text-Mobile-C1 text-primary1 font-bold desktop:text-H4 line-clamp-2">
{location.name}
Bo-Duke marked this conversation as resolved.
Show resolved Hide resolved
</Name>
</h3>
{Boolean(location.description) && (
<p
className="text-P2 my-2"
Expand All @@ -83,43 +91,9 @@ export const PointsSecondary: React.FC<PointsSecondaryProps> = ({
)}
</div>
</div>
</StyledTooltip>
</Tooltip>
</HoverableMarker>
))}
</>
);
};

const desktopWidth = 288;
const desktopImgHeight = 122;
const mobileWidth = 215;
const mobileImgHeight = 133;

const StyledTooltip = styled(Tooltip)`
padding: 0;
border: 0px !important;
border-radius: ${getSpacing(4)} !important;
overflow: hidden;
white-space: initial !important;
width: ${mobileWidth}px;
${desktopOnly(css`
width: ${desktopWidth}px;
`)};
`;

const Name = styled.span`
${desktopOnly(css`
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
display: block;
`)}
`;

const CoverImage = styled.img`
height: ${mobileImgHeight}px;
${desktopOnly(css`
height: ${desktopImgHeight}px;
`)}
object-fit: cover;
`;
163 changes: 75 additions & 88 deletions frontend/src/components/Map/components/Popup/index.tsx
Original file line number Diff line number Diff line change
@@ -1,64 +1,99 @@
import React, { useState } from 'react';
import { useState } from 'react';
import Image from 'next/image';
import { routes } from 'services/routes';
import styled, { css } from 'styled-components';
import { Popup as LeafletPopup, Tooltip as LeafletTooltip } from 'react-leaflet';
import { FormattedMessage } from 'react-intl';
import Loader from 'components/Loader';

import { desktopOnly, getSpacing } from 'stylesheet';
import { Button } from 'components/Button';
import { generateResultDetailsUrl } from 'components/pages/search/utils';

import Link from 'components/Link';
import { PopupResult } from 'modules/trekResult/interface';
import { usePopupResult } from '../../hooks/usePopupResult';

interface Props {
id: number;
id: number | string;
parentId?: number;
handleOpen?: () => void;
handleClose?: () => void;
type: 'TREK' | 'TOURISTIC_CONTENT' | 'OUTDOOR_SITE' | 'TOURISTIC_EVENT';
type: 'TREK' | 'TOURISTIC_CONTENT' | 'OUTDOOR_SITE' | 'TOURISTIC_EVENT' | null;
content?: PopupResult;
}

interface PropsPC {
showButton: boolean;
id: number;
type: 'TREK' | 'TOURISTIC_CONTENT' | 'OUTDOOR_SITE' | 'TOURISTIC_EVENT';
id: number | string;
type: 'TREK' | 'TOURISTIC_CONTENT' | 'OUTDOOR_SITE' | 'TOURISTIC_EVENT' | null;
parentId?: number;
content?: PopupResult;
}

const getRoute = (type: string) => {
const getRoute = (type: PropsPC['type']) => {
if (type === 'TOURISTIC_CONTENT') return routes.TOURISTIC_CONTENT;
if (type === 'OUTDOOR_SITE') return routes.OUTDOOR_SITE;
if (type === 'TOURISTIC_EVENT') return routes.TOURISTIC_EVENT;
if (type === 'TREK') return routes.TREK;
return routes.TREK;
};

dtrucs marked this conversation as resolved.
Show resolved Hide resolved
const PopupContent: React.FC<PropsPC> = ({ showButton, id, type, parentId }) => {
const { isLoading, trekPopupResult } = usePopupResult(id.toString(), true, type);
const PopupContent: React.FC<PropsPC> = ({ showButton, id, type, parentId, content }) => {
let isLoading = false;
let trekPopupResult = null;

if (!content && type) {
const popupResult = usePopupResult(id.toString(), true, type);
isLoading = popupResult.isLoading;
Bo-Duke marked this conversation as resolved.
Show resolved Hide resolved
trekPopupResult = popupResult.trekPopupResult;
} else {
trekPopupResult = content;
}

return (
<Loader className="absolute inset-0" loaded={!isLoading}>
{trekPopupResult && (
<div className="flex flex-col">
<CoverImage src={trekPopupResult.imgUrl} />
<Image
loading="lazy"
className="h-40 w-auto desktop:h-30 object-cover"
width={300}
height={130}
src={trekPopupResult.imgUrl}
alt=""
/>
<div className="p-4">
<span className="text-P2 mb-1 text-greyDarkColored hidden desktop:inline">
{trekPopupResult.place}
</span>
<h3 className="text-Mobile-C1 text-primary1 font-bold desktop:text-H4 line-clamp-1">
{trekPopupResult.place && (
<span className="text-P2 mb-1 text-greyDarkColored hidden desktop:inline">
{trekPopupResult.place}
</span>
)}
<h3 className="text-Mobile-C1 text-primary1 font-bold desktop:text-H4 line-clamp-2">
{trekPopupResult.title}
</h3>
{showButton && (
<Link
href={generateResultDetailsUrl(id, trekPopupResult.title, getRoute(type), parentId)}
>
<Button className="mt-4 w-full">
<span className="w-full text-center">
<FormattedMessage id="search.map.seeResult" />
</span>
</Button>
</Link>
<>
{trekPopupResult?.button?.onClick ? (
<Button className="mt-4 w-full" {...trekPopupResult.button}>
dtrucs marked this conversation as resolved.
Show resolved Hide resolved
<span className="w-full">
<FormattedMessage id={trekPopupResult.button.label} />
</span>
</Button>
) : (
<Link
className="mt-4 flex gap-1 items-center py-2 px-4 h-12 border border-solid border-primary1 rounded-lg text-sm text-primary1 bg-white font-semibold transition transition-color hover:bg-primary2 focus:bg-primary2"
href={generateResultDetailsUrl(
id,
trekPopupResult.title,
getRoute(type ?? null),
parentId,
)}
>
<span className="text-center w-full">
<FormattedMessage id="search.map.seeResult" />
</span>
</Link>
)}
</>
)}
</div>
</div>
Expand All @@ -67,85 +102,37 @@ const PopupContent: React.FC<PropsPC> = ({ showButton, id, type, parentId }) =>
);
};

export const Popup: React.FC<Props> = ({ id, parentId, handleOpen, handleClose, type }) => {
export const Popup: React.FC<Props> = ({
id,
parentId,
handleOpen,
handleClose,
type,
content,
}) => {
const [hideTooltip, setHideTooltip] = useState<boolean>(false);

return (
<>
{!hideTooltip && (
<StyledTooltip>
<PopupContent type={type} id={id} showButton={false} />
</StyledTooltip>
<LeafletTooltip className="!p-0 !border-0 !rounded-xl !overflow-hidden w-55 desktop:w-70 !whitespace-normal">
<PopupContent type={type} id={id} showButton={false} content={content} />
</LeafletTooltip>
)}
<StyledPopup
<LeafletPopup
closeButton={false}
onOpen={() => {
setHideTooltip(true);
handleOpen && handleOpen();
handleOpen?.();
}}
onClose={() => {
setHideTooltip(false);
handleClose && handleClose();
handleClose?.();
}}
offset={[0, -12]}
>
<PopupContent type={type} id={id} showButton={true} parentId={parentId} />
</StyledPopup>
<PopupContent type={type} id={id} showButton={true} parentId={parentId} content={content} />
</LeafletPopup>
</>
);
};

const desktopWidth = 288;
const desktopImgHeight = 122;
const mobileWidth = 215;
const mobileImgHeight = 133;

const StyledTooltip = styled(LeafletTooltip)`
padding: 0;
border: 0px !important;
border-radius: ${getSpacing(4)} !important;
overflow: hidden;
white-space: initial !important;
width: ${mobileWidth}px;
${desktopOnly(css`
width: ${desktopWidth}px;
`)};
`;

const StyledPopup = styled(LeafletPopup)`
.leaflet-popup-content {
margin: 0;

// Show the loader properly
position: relative;
min-height: 120px;
min-width: 120px;
}

.leaflet-popup-content-wrapper {
padding: 0;

border-radius: ${getSpacing(4)};
overflow: hidden;

width: ${mobileWidth}px;
${desktopOnly(css`
width: ${desktopWidth}px;
`)};
}

// Removes native leaflet popup triangle below the content
// https://stackoverflow.com/a/51457598/14707543
.leaflet-popup-tip {
background: rgba(0, 0, 0, 0) !important;
box-shadow: none !important;
}
`;

const CoverImage = styled.img`
height: ${mobileImgHeight}px;
${desktopOnly(css`
height: ${desktopImgHeight}px;
`)}
object-fit: cover;
`;
4 changes: 4 additions & 0 deletions frontend/src/modules/trekResult/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export interface PopupResult {
title: string;
place: string;
imgUrl: string;
button?: {
label: string;
onClick?: () => void;
};
}
22 changes: 22 additions & 0 deletions frontend/src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
border-hardKO
;
}

.textarea {
@apply
bg-white
Expand All @@ -50,6 +51,27 @@
;
}

/* Leaflet */
.leaflet-popup .leaflet-popup-content {
@apply m-0;
/* Show the loader properly */
@apply relative;
min-height: 120px;
min-width: 120px;
}

.leaflet-popup .leaflet-popup-content-wrapper {
@apply p-0 rounded-xl overflow-hidden w-55 desktop:w-70;
}

.leaflet-popup .leaflet-popup-tip {
/* Removes native leaflet popup triangle below the content
https://stackoverflow.com/a/51457598/14707543
*/
background: rgba(0, 0, 0, 0) !important;
box-shadow: none !important;
}

.hasDrawer .leaflet-bottom {
@apply mb-12;
@apply desktop:mb-0;
Expand Down
1 change: 1 addition & 0 deletions frontend/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = {
30: `${SPACING_UNIT * 30}px`,
40: `${SPACING_UNIT * 40}px`,
50: `${SPACING_UNIT * 50}px`,
55: `${SPACING_UNIT * 55}px`,
60: `${SPACING_UNIT * 60}px`,
70: `${SPACING_UNIT * 70}px`,
90: `${SPACING_UNIT * 90}px`,
Expand Down
Loading