Skip to content

Commit

Permalink
climbing: Tick fix (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvaclavik authored Apr 17, 2024
1 parent 75d7fbc commit 874e1ce
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 19 deletions.
27 changes: 14 additions & 13 deletions src/components/FeaturePanel/Climbing/RouteList/ExpandedRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
import DeleteIcon from '@material-ui/icons/Delete';
import ArrowForwardIcon from '@material-ui/icons/ArrowForward';
import AddIcon from '@material-ui/icons/Add';
import Router from 'next/router';
import { RouteDifficultySelect } from '../RouteDifficultySelect';
import { useClimbingContext } from '../contexts/ClimbingContext';
import { RouteInDifferentPhotos } from './RouteInDifferentPhotos';
Expand Down Expand Up @@ -68,9 +67,6 @@ export const ExpandedRow = ({
machine.execute('deleteRoute', { routeNumber });
hideDeleteDialog();
};
const onNodeDetailClick = () => {
Router.push(`${getOsmappLink(tempRoute.feature)}${window.location.hash}`);
};

return (
<>
Expand Down Expand Up @@ -175,15 +171,20 @@ export const ExpandedRow = ({
</ListItem>
)}
<ListItem>
<Button
onClick={onNodeDetailClick}
color="secondary"
size="small"
variant="text"
endIcon={<ArrowForwardIcon />}
>
Show route detail
</Button>
{tempRoute.feature ? (
<Button
color="secondary"
size="small"
variant="text"
endIcon={<ArrowForwardIcon />}
href={`${getOsmappLink(tempRoute.feature)}${
window.location.hash
}`}
component="a"
>
Show route detail
</Button>
) : null}
</ListItem>
<ListItem>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export const osmToClimbingRoutes = (feature: Feature): Array<ClimbingRoute> => {
return [];
}

const routes = feature.memberFeatures.filter(({ tags }) =>
['route', 'route_bottom'].includes(tags.climbing),
);
const routes = feature.memberFeatures;

return routes.map((route) => {
const { paths, photoToKeyMap } = getPathsByImage(route.tags);
Expand Down
19 changes: 19 additions & 0 deletions src/services/__tests__/ticks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getStorageIndex } from '../ticks';

const ticks = [
{
osmId: 'n3462908984',
date: '2024-04-17T15:59:42.163Z',
style: 'PP' as const,
},
{
osmId: 'n3462910811',
date: '2024-04-17T16:00:16.911Z',
style: 'FS' as const,
},
];

const osmId = 'n3462908984';
test('getStorageIndex', () => {
expect(getStorageIndex(ticks, osmId, 0)).toBe(0);
});
29 changes: 26 additions & 3 deletions src/services/ticks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ export const onTickAdd = ({ osmId }) => {
]);
};

export const getStorageIndex = (
ticks: Array<Tick>,
osmId: string,
routeIndex: number,
): number | null => {
const found = ticks?.reduce(
(acc, tick, storageIndex) => {
if (osmId === tick.osmId) {
const newRouteIndex = acc.routeIndex + 1;
return {
storageIndex:
newRouteIndex === routeIndex ? storageIndex : acc.storageIndex,
routeIndex: newRouteIndex,
};
}
return acc;
},
{ routeIndex: -1, storageIndex: null },
);
return found.storageIndex;
};

export const findTicks = (osmId: string): Array<Tick> => {
const ticks = getLocalStorageItem(KEY);

Expand All @@ -90,10 +112,11 @@ export const onTickUpdate = ({
index: number;
updatedObject: Partial<Tick>;
}) => {
const routeTicks = findTicks(osmId);
const ticks = getLocalStorageItem(KEY);
const storageIndex = getStorageIndex(ticks, osmId, index);
const updatedArray = updateElementOnIndex<Tick>(
routeTicks,
index,
ticks,
storageIndex,
(item) => ({ ...item, ...updatedObject }),
);
setLocalStorageItem(KEY, updatedArray);
Expand Down

0 comments on commit 874e1ce

Please sign in to comment.