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

feat: add view item in Builder #124

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/components/Map.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
DiscriminatedItem,
MemberFactory,
PackedFolderItemFactory,
PermissionLevel,
Expand Down Expand Up @@ -470,6 +471,10 @@ export const MapRead = {
args: {
item: PackedFolderItemFactory({}, { permission: PermissionLevel.Read }),
viewItem: () => ({}) as any,
viewItemInBuilder: (it: DiscriminatedItem) => {
// eslint-disable-next-line no-console
console.log(it.id);
},
currentMember: MemberFactory({ extra: { lang: 'fr' } }),
useDeleteItemGeolocation: () => ({}) as any,
useItemsInMap: () =>
Expand Down
14 changes: 8 additions & 6 deletions src/components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Map = ({
useDeleteItemGeolocation,
handleAddOnClick,
currentPosition,
viewItemInBuilder,
}: Props): JSX.Element => {
const [showMap, setShowMap] = useState<boolean>(false);

Expand All @@ -44,17 +45,18 @@ const Map = ({

return (
<QueryClientContextProvider
item={item}
useSuggestionsForAddress={useSuggestionsForAddress}
currentMember={currentMember}
currentPosition={currentPosition}
handleAddOnClick={handleAddOnClick}
item={item}
useAddressFromGeolocation={useAddressFromGeolocation}
useDeleteItemGeolocation={useDeleteItemGeolocation}
useItemsInMap={useItemsInMap}
useRecycleItems={useRecycleItems}
usePostItem={usePostItem}
useRecycleItems={useRecycleItems}
useSuggestionsForAddress={useSuggestionsForAddress}
viewItem={viewItem}
handleAddOnClick={handleAddOnClick}
useDeleteItemGeolocation={useDeleteItemGeolocation}
currentPosition={currentPosition}
viewItemInBuilder={viewItemInBuilder}
>
<div
style={{
Expand Down
5 changes: 5 additions & 0 deletions src/components/context/QueryClientContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface QueryClientContextInterface {
useDeleteItemGeolocation: QueryClientMutations['useDeleteItemGeolocation'];
useSuggestionsForAddress: QueryClientHooks['useSuggestionsForAddress'];
viewItem: (item: DiscriminatedItem) => void;
viewItemInBuilder: (item: DiscriminatedItem) => void;
handleAddOnClick?: ({
location,
}: {
Expand All @@ -44,6 +45,7 @@ export const QueryClientContext = createContext<QueryClientContextInterface>({
usePostItem: () => ({}) as any,
useDeleteItemGeolocation: () => ({}) as any,
viewItem: () => ({}) as any,
viewItemInBuilder: () => ({}) as any,
});

export const QueryClientContextProvider = ({
Expand All @@ -59,6 +61,7 @@ export const QueryClientContextProvider = ({
item,
currentPosition,
handleAddOnClick,
viewItemInBuilder,
}: QueryClientContextInterface & { children: JSX.Element }): JSX.Element => {
const value = useMemo(
() => ({
Expand All @@ -73,6 +76,7 @@ export const QueryClientContextProvider = ({
useSuggestionsForAddress,
currentPosition,
handleAddOnClick,
viewItemInBuilder,
}),
[
currentMember,
Expand All @@ -86,6 +90,7 @@ export const QueryClientContextProvider = ({
item,
currentPosition,
handleAddOnClick,
viewItemInBuilder,
],
);

Expand Down
18 changes: 16 additions & 2 deletions src/components/map/ItemsMarkers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRef } from 'react';
import { FeatureGroup, Marker } from 'react-leaflet';
import { useEffect, useRef, useState } from 'react';
import { FeatureGroup, Marker, useMap } from 'react-leaflet';
import MarkerClusterGroup from 'react-leaflet-cluster';

import { useQueryClientContext } from '../context/QueryClientContext';
Expand All @@ -19,12 +19,26 @@ const ItemsMarkers = ({
};
}): JSX.Element | JSX.Element[] | undefined => {
const groupRef = useRef<any>(null);
const map = useMap();
const { useItemsInMap, item } = useQueryClientContext();
const { data: itemGeolocations } = useItemsInMap({
...bounds,
parentItemId: item?.id,
keywords: tags,
});
const [prevState, setPrevState] = useState(itemGeolocations);

useEffect(() => {
if (JSON.stringify(itemGeolocations) !== JSON.stringify(prevState)) {
// on positive search, focus on items
if (itemGeolocations?.length !== prevState?.length && tags.length) {
map.fitBounds(groupRef.current.getBounds());
}

setPrevState(itemGeolocations);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [tags, itemGeolocations]);

// color of clusters is defined by number of markers grouped together
return (
Expand Down
27 changes: 23 additions & 4 deletions src/components/map/MarkerPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Popup } from 'react-leaflet';

import { Box, Chip, Stack, Typography } from '@mui/material';
import { Box, Chip, Link, Stack, Tooltip, Typography } from '@mui/material';

import {
ItemGeolocation,
PermissionLevel,
PermissionLevelCompare,
} from '@graasp/sdk';

import { useMapTranslation } from '@/config/i18n';

import { useQueryClientContext } from '../context/QueryClientContext';
import DeleteItemButton from './DeleteItemButton';
import DeleteLocationButton from './DeleteLocationButton';
import ViewButton from './ViewButton';
Expand All @@ -18,10 +21,26 @@ const MarkerPopup = ({
geolocation: ItemGeolocation;
}): JSX.Element => {
const { item } = geolocation;
const { viewItemInBuilder } = useQueryClientContext();
const { t } = useMapTranslation();

return (
<Popup autoPan={false}>
<Typography variant="h5">{item.name}</Typography>
<Tooltip title={t('VIEW_ITEM_BUILDER_TOOLTIP')}>
{/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
<Link
component={Typography}
onClick={() => viewItemInBuilder(item)}
variant="h5"
sx={{
'&:hover': {
cursor: 'pointer',
},
}}
>
{item.name}
</Link>
</Tooltip>
{/* TODO: slice and show html */}
{item.description && (
<Typography
Expand All @@ -35,9 +54,9 @@ const MarkerPopup = ({
{geolocation.helperLabel}
</Typography>
<Box>
{item.settings.tags?.map((t: string, idx) => (
{item.settings.tags?.map((tag: string, idx) => (
// eslint-disable-next-line react/no-array-index-key
<Chip key={idx} label={t} sx={{ mx: 0.5 }} />
<Chip key={idx} label={tag} sx={{ mx: 0.5 }} />
))}
</Box>
<Stack direction="row">
Expand Down
5 changes: 4 additions & 1 deletion src/components/map/ViewButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { IconButton, Tooltip } from '@mui/material';

import { DiscriminatedItem } from '@graasp/sdk';

import { useMapTranslation } from '@/config/i18n';

import { useQueryClientContext } from '../context/QueryClientContext';

type Props = {
Expand All @@ -11,9 +13,10 @@ type Props = {

const ViewButton = ({ item }: Props): JSX.Element => {
const { viewItem } = useQueryClientContext();
const { t } = useMapTranslation();

return (
<Tooltip title="View item in Graasp Player">
<Tooltip title={t('VIEW_ITEM_PLAYER_TOOLTIP')}>
<IconButton
onClick={() => {
viewItem(item);
Expand Down
2 changes: 1 addition & 1 deletion src/components/topbar/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Search = ({
<Autocomplete
multiple
freeSolo
aria-label="keywords"
aria-label={t('Keywords')}
value={tags}
options={[]}
renderInput={(params) => (
Expand Down
4 changes: 3 additions & 1 deletion src/langs/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@
"Name": "Name",
"Add a new folder at this location": "Add a new folder at this location",
"This location does not match a specific address.": "This location does not match a specific address.",
"Select a country": "Select a country"
"Select a country": "Select a country",
"VIEW_ITEM_BUILDER_TOOLTIP": "View item in Graasp Builder",
"VIEW_ITEM_PLAYER_TOOLTIP": "View item in Graasp Player"
}