Skip to content

Commit

Permalink
feat: add map view (#1060)
Browse files Browse the repository at this point in the history
* feat: add map view

* refactor: fix types

* refactor: changes

* refactor: update query-client and map

* refactor: fix tests
  • Loading branch information
pyphilia authored Mar 11, 2024
1 parent b8f7011 commit 95df7a5
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 188 deletions.
4 changes: 2 additions & 2 deletions cypress/e2e/item/duplicate/duplicateItem.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SAMPLE_ITEMS } from '../../../fixtures/items';
import duplicateItem from '../../../support/actionsUtils';

describe('duplicate Item in Home', () => {
Object.values(ItemLayoutMode).forEach((view) => {
Object.values([ItemLayoutMode.Grid, ItemLayoutMode.List]).forEach((view) => {
it(`duplicate item on Home in ${view} view`, () => {
cy.setUpApi(SAMPLE_ITEMS);
cy.visit(HOME_PATH);
Expand All @@ -25,7 +25,7 @@ describe('duplicate Item in Home', () => {
});
});
describe('duplicate Item in item', () => {
Object.values(ItemLayoutMode).forEach((view) => {
Object.values([ItemLayoutMode.Grid, ItemLayoutMode.List]).forEach((view) => {
it(`duplicate item in item in ${view} view`, () => {
cy.setUpApi(SAMPLE_ITEMS);
const { id, path } = SAMPLE_ITEMS.items[0];
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
"@emotion/react": "11.11.4",
"@emotion/styled": "11.11.0",
"@graasp/chatbox": "3.1.0",
"@graasp/query-client": "2.8.0",
"@graasp/map": "1.4.0",
"@graasp/query-client": "2.9.0",
"@graasp/sdk": "4.1.0",
"@graasp/translations": "1.25.2",
"@graasp/ui": "4.9.0",
"@graasp/ui": "4.9.1",
"@mui/icons-material": "5.15.11",
"@mui/lab": "5.0.0-alpha.166",
"@mui/material": "5.15.11",
Expand Down
45 changes: 45 additions & 0 deletions src/components/item/MapView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Typography } from '@mui/material';

import { Map } from '@graasp/map';
import { type DiscriminatedItem, redirect } from '@graasp/sdk';

import { buildGraaspPlayerView } from '@/config/externalPaths';
import { hooks, mutations } from '@/config/queryClient';
import { buildPlayerTabName } from '@/config/selectors';

type Props = {
parentId?: DiscriminatedItem['id'];
title: string;
};

const MapView = ({ parentId, title }: Props): JSX.Element => {
const { data: currentMember } = hooks.useCurrentMember();

return (
<>
<Typography variant="h4" sx={{ wordWrap: 'break-word' }}>
{title}
</Typography>
<div style={{ width: '100%', height: '80vh' }}>
<Map
useDeleteItemGeolocation={mutations.useDeleteItemGeolocation}
usePostItem={mutations.usePostItem}
useRecycleItems={mutations.useRecycleItems}
useAddressFromGeolocation={hooks.useAddressFromGeolocation}
useSuggestionsForAddress={hooks.useSuggestionsForAddress}
useItemsInMap={hooks.useItemsInMap}
viewItem={(item) => {
redirect(window, buildGraaspPlayerView(item.id), {
name: buildPlayerTabName(item.id),
openInNewTab: true,
});
}}
currentMember={currentMember}
itemId={parentId}
/>
</div>
</>
);
};

export default MapView;
6 changes: 3 additions & 3 deletions src/components/item/header/ModeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
List as ListIcon,
ViewModule as ViewModuleIcon,
} from '@mui/icons-material';
// import MapIcon from '@mui/icons-material/Map';
import MapIcon from '@mui/icons-material/Map';
import { IconButton } from '@mui/material';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
Expand All @@ -16,8 +16,8 @@ import { useLayoutContext } from '../../context/LayoutContext';

const modeToIcon = (mode: ItemLayoutMode) => {
switch (mode) {
// case ItemLayoutMode.Map:
// return <MapIcon color="primary" />;
case ItemLayoutMode.Map:
return <MapIcon color="primary" />;
case ItemLayoutMode.Grid:
return <ViewModuleIcon color="primary" />;
case ItemLayoutMode.List:
Expand Down
113 changes: 27 additions & 86 deletions src/components/item/settings/GeolocationPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import { ChangeEventHandler } from 'react';

import Clear from '@mui/icons-material/Clear';
import {
Box,
IconButton,
InputAdornment,
LinearProgress,
List,
ListItemButton,
OutlinedInput,
Stack,
Tooltip,
Typography,
} from '@mui/material';
import { IconButton, Stack, Tooltip, Typography } from '@mui/material';

import {
type GeolocationPickerProps,
GeolocationPicker as MapGeolocationPicker,
} from '@graasp/map';
import { DiscriminatedItem } from '@graasp/sdk';

import { useBuilderTranslation } from '@/config/i18n';
import { hooks, mutations } from '@/config/queryClient';
import { BUILDER } from '@/langs/constants';

import { OpenStreetMapResult, useSearchAddress } from './hooks';

const GeolocationPicker = ({
item,
}: {
Expand All @@ -32,42 +21,26 @@ const GeolocationPicker = ({
const { mutate: putGeoloc } = mutations.usePutItemGeolocation();
const { mutate: deleteGeoloc } = mutations.useDeleteItemGeolocation();

const {
query,
setQuery,
isDebounced,
setResults,
results,
loading,
clearQuery,
} = useSearchAddress({
lang: item.lang,
geoloc,
});

const onChange: ChangeEventHandler<HTMLInputElement> = (e) => {
setQuery(e.target.value);
};

const onChangeOption = (option: OpenStreetMapResult): void => {
const {
raw: { lat, lon: lng },
label,
} = option;
const onChangeOption: GeolocationPickerProps['onChangeOption'] = (option: {
addressLabel: string;
lat: number;
lng: number;
country?: string;
}): void => {
const { addressLabel, lat, lng, country } = option;
putGeoloc({
itemId: item.id,
geolocation: {
lng: parseFloat(lng),
lat: parseFloat(lat),
addressLabel: label,
addressLabel,
lat,
lng,
country,
},
});
setResults([]);
};

const clearGeoloc = () => {
deleteGeoloc({ itemId: item.id });
clearQuery();
};

// the input is disabled if the geoloc is defined in parent
Expand All @@ -86,57 +59,25 @@ const GeolocationPicker = ({
</Stack>
<Stack direction="row" alignItems="center">
<Stack flexGrow={1}>
<OutlinedInput
disabled={isDisabled}
fullWidth
multiline
placeholder={t(BUILDER.ITEM_SETTINGS_GEOLOCATION_PLACEHOLDER)}
onChange={onChange}
value={query}
endAdornment={
query && !isDisabled ? (
<InputAdornment position="end">
<Tooltip title={t(BUILDER.ITEM_SETTINGS_CLEAR_GEOLOCATION)}>
<IconButton onClick={clearGeoloc}>
<Clear />
</IconButton>
</Tooltip>
</InputAdornment>
) : undefined
}
<MapGeolocationPicker
onChangeOption={onChangeOption}
initialValue={geoloc?.addressLabel ?? undefined}
useSuggestionsForAddress={hooks.useSuggestionsForAddress}
/>
{loading && (
<Box sx={{ width: '100%' }}>
<LinearProgress />
</Box>
)}
</Stack>
<Stack>
<Tooltip title={t(BUILDER.ITEM_SETTINGS_CLEAR_GEOLOCATION)}>
<IconButton onClick={clearGeoloc}>
<Clear />
</IconButton>
</Tooltip>
</Stack>
</Stack>
{isDisabled && (
<Typography variant="caption">
{t(BUILDER.ITEM_SETTINGS_GEOLOCATION_INHERITED_EXPLANATION)}
</Typography>
)}
<Stack>
{results && (
<List>
{results.map((r) => (
<ListItemButton
key={r.raw.osm_id}
onClick={() => onChangeOption(r)}
>
{r.label}
</ListItemButton>
))}
{!results.length &&
query &&
query !== geoloc?.addressLabel &&
!loading &&
!isDebounced &&
t(BUILDER.ITEM_SETTINGS_GEOLOCATION_NO_ADDRESS)}
</List>
)}
</Stack>
</Stack>
);
};
Expand Down
79 changes: 0 additions & 79 deletions src/components/item/settings/hooks.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/components/main/Items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ShowOnlyMeChangeType } from '@/config/types';
import { hooks } from '../../config/queryClient';
import { ItemLayoutMode } from '../../enums';
import { useLayoutContext } from '../context/LayoutContext';
import MapView from '../item/MapView';
import { useItemsStatuses } from '../table/BadgesCellRenderer';
import ItemsGrid from './ItemsGrid';
import ItemsTable from './ItemsTable';
Expand Down Expand Up @@ -75,6 +76,8 @@ const Items = ({
itemsTags,
});
switch (mode) {
case ItemLayoutMode.Map:
return <MapView title={title} parentId={parentId} />;
case ItemLayoutMode.Grid:
return (
<ItemsGrid
Expand Down
5 changes: 2 additions & 3 deletions src/enums/itemLayoutMode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
enum ItemLayoutMode {
Grid = 'grid',
List = 'list',
// todo: enable when adding the map
// Map = 'map',
Grid = 'grid',
Map = 'map',
}

export const DEFAULT_ITEM_LAYOUT_MODE = ItemLayoutMode.List;
Expand Down
Loading

0 comments on commit 95df7a5

Please sign in to comment.