Skip to content

Commit

Permalink
Merge pull request #128 from developmentseed/enhance/compare-preselect
Browse files Browse the repository at this point in the history
Preselect compare map from project explore page
  • Loading branch information
LanesGood committed Mar 6, 2024
2 parents f39a00e + 60a70cb commit 57fa286
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
68 changes: 67 additions & 1 deletion app/assets/scripts/components/project/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { themeVal, glsp, media, truncated } from '@devseed-ui/theme-provider';
import { Heading } from '@devseed-ui/typography';
import { Form } from '@devseed-ui/form';
import { Button } from '@devseed-ui/button';
import { FormInput } from '@devseed-ui/form';
import { FormInput, FormSwitch } from '@devseed-ui/form';

import { ProjectMachineContext } from '../../../fsm/project';

Expand All @@ -28,9 +28,11 @@ import {
downloadShareGeotiff,
getShareLink,
} from '../../../utils/share-link';
import logger from '../../../utils/logger';
import { useAuth } from '../../../context/auth';
import selectors from '../../../fsm/project/selectors';
import toasts from '../../common/toasts';
import { StyledLink } from '../../../styles/links';

const Wrapper = styled.div`
flex: 1;
Expand Down Expand Up @@ -134,6 +136,7 @@ function ProjectPageHeader({ isMediumDown }) {
const { restApiClient, user, isAuthenticated } = useAuth();
const [editProjectNameMode, setEditProjectNameMode] = useState(false);
const [localProjectName, setLocalProjectName] = useState(null);
const [isPublished, setIsPublished] = useState(null);
const actorRef = ProjectMachineContext.useActorRef();
const displayProjectNameModal = ProjectMachineContext.useSelector(
selectors.displayProjectNameModal
Expand Down Expand Up @@ -197,6 +200,11 @@ function ProjectPageHeader({ isMediumDown }) {
}
}, [projectName]);

useEffect(() => {
if (!currentShare) return;
setIsPublished(currentShare.published);
}, [currentShare]);

// Delays focus
const [projectNameInputRef, setProjectNameInputFocus] = useFocus(0);

Expand Down Expand Up @@ -420,6 +428,64 @@ function ProjectPageHeader({ isMediumDown }) {
</DropdownItem>
)}
</li>
{currentShare && (
<>
<li>
<DropdownItem>
<FormSwitch
name='published'
title='Make share map public'
checked={isPublished}
onChange={async () => {
const newIsPublished = !isPublished;
setIsPublished(newIsPublished);

try {
await restApiClient.patch(
`share/${currentShare.uuid}`,
{
published: newIsPublished,
}
);
setIsPublished(newIsPublished);
} catch (err) {
setIsPublished(!newIsPublished);
logger(
'There was an unexpected error updating the exported map.',
err
);
toasts.error(
'There was an unexpected error updating the exported map.'
);
}
}}
textPlacement='right'
>
Publish Share
</FormSwitch>
</DropdownItem>
</li>
<li>
<DropdownItem
useIcon='resize-center-horizontal'
as={StyledLink}
nonhoverable={!isPublished}
muted={!isPublished}
disabled={!isPublished}
to={
isPublished
? {
pathname: '/public-maps',
state: { uuid: currentShare.uuid },
}
: null
}
>
Compare map
</DropdownItem>
</li>
</>
)}
</DropdownBody>
</>
</Dropdown>
Expand Down
18 changes: 15 additions & 3 deletions app/assets/scripts/components/public-maps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import toasts from '../common/toasts';
import { downloadShareGeotiff } from '../../utils/share-link';
import { StyledLink } from '../../styles/links';
import { composeMosaicName } from '../compare-map';
import { useLocation } from 'react-router-dom';

const ITEMS_PER_PAGE = 20;

Expand Down Expand Up @@ -179,20 +180,23 @@ function RenderRow(share, { restApiClient, compareMaps, setCompareMaps }) {
type='checkbox'
name={`select-compare-${share.uuid}`}
id={`select-compare-${share.uuid}`}
hideText
checked={
share.uuid &&
compareMaps.some((compareMap) => compareMap.uuid === share.uuid)
}
onChange={() => checkHandler(compareMaps, setCompareMaps, share)}
/>
>
Select for comparison
</FormCheckable>
</TableCell>
</TableRow>
);
}
function RenderSelectedRow(share, { compareMaps, setCompareMaps }) {
const { side, aoi, checkpoint, model, timeframe, mosaic } = share;
return (
<TableRow key={share.uuid}>
<TableRow key={share.uuid || share.side}>
<TableCell>
<TableRowHeader>{side}</TableRowHeader>
</TableCell>
Expand Down Expand Up @@ -223,7 +227,8 @@ function RenderSelectedRow(share, { compareMaps, setCompareMaps }) {

function ExportedMapsList() {
const { apiToken } = useAuth();

const location = useLocation();
const { uuid } = location.state || 'none';
const [page, setPage] = useState(0);
const [total, setTotal] = useState(null);
const [isLoading, setIsLoading] = useState(true);
Expand Down Expand Up @@ -255,6 +260,13 @@ function ExportedMapsList() {
}, [apiToken, page]);
const twoSharesSelected = compareMaps.filter((c) => c.uuid).length === 2;

let preSelectedShare = shares.find((s) => s.uuid === uuid);
useEffect(() => {
if (!preSelectedShare) return;
setCompareMaps([{ ...preSelectedShare, side: 'left' }, { side: 'right' }]);
window.history.replaceState({}, '');
}, [preSelectedShare]);

return (
<>
<Inpage>
Expand Down

0 comments on commit 57fa286

Please sign in to comment.