Skip to content

Commit

Permalink
feat(map): ✨ Edit Claims
Browse files Browse the repository at this point in the history
  • Loading branch information
Nudelsuppe42 committed Oct 14, 2023
1 parent c9c954a commit 6e56689
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@mantine/rte": "^5.10.5",
"@mantine/spotlight": "^7.0.0",
"@mantine/tiptap": "^7.0.0",
"@mapbox/mapbox-gl-draw": "^1.4.3",
"@react-three/drei": "^9.56.10",
"@react-three/fiber": "^8.10.1",
"@tabler/icons": "^1.119.0",
Expand All @@ -43,6 +44,7 @@
"@tiptap/pm": "^2.0.2",
"@tiptap/react": "^2.0.2",
"@tiptap/starter-kit": "^2.0.2",
"@types/mapbox__mapbox-gl-draw": "^1.4.4",
"@types/sanitize-html": "^2.9.0",
"axios": "^1.3.4",
"dayjs": "^1.11.5",
Expand Down
2 changes: 1 addition & 1 deletion src/components/map/ClaimDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function ClaimDrawer(props: ClaimDrawerProps) {
Copy Coordinates
</Button>
{data.owner?.id == user?.id && (
<Button component="a" variant="outline" leftSection={<IconPencil />} href={`/claims/${props.id}`}>
<Button component="a" variant="outline" leftSection={<IconPencil />} href={`/me/claims/${props.id}`}>
Edit Claim
</Button>
)}
Expand Down
3 changes: 1 addition & 2 deletions src/components/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { LoadingOverlay, useMantineColorScheme, useMantineTheme } from '@mantine
import { MapboxStyleDefinition, MapboxStyleSwitcherControl } from 'mapbox-gl-style-switcher';

import { IconCheck } from '@tabler/icons';
import { MapboxMap } from 'react-map-gl';
import ReactDOM from 'react-dom';
import mapboxgl from 'mapbox-gl';
import { showNotification } from '@mantine/notifications';
Expand Down Expand Up @@ -186,7 +185,7 @@ export function mapCopyCoordinates(map: any, clipboard: any) {
// Map Load Helper Functions

export async function mapLoadGeoJson(
map: MapboxMap,
map: mapboxgl.Map,
url: string,
layer: string,
layerType: any,
Expand Down
1 change: 1 addition & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import '@mantine/notifications/styles.css';
import '@mantine/nprogress/styles.css';
import '@mantine/tiptap/styles.css';
import 'mapbox-gl/dist/mapbox-gl.css';
import '@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw.css';
import '@mantine/dropzone/styles.css';
import '@mantine/dates/styles.css';
import '../styles/globals.css';
Expand Down
178 changes: 178 additions & 0 deletions src/pages/me/claims/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { Alert, Anchor, Button, Grid, Group, Switch, TextInput } from '@mantine/core';
import { IconAlertCircle, IconCheck, IconChevronLeft, IconDeviceFloppy, IconQuestionMark } from '@tabler/icons-react';

import { Divide } from 'tabler-icons-react';
import Map from '../../../components/map/Map';
import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { NextPage } from 'next';
import Page from '../../../components/Page';
import fetcher from '../../../utils/Fetcher';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { showNotification } from '@mantine/notifications';
import { useRouter } from 'next/router';
import { useState } from 'react';
import { useUser } from '../../../hooks/useUser';

const ClaimPage: NextPage = ({ claimId, data }: any) => {
const [polygon, setPolygon] = useState<any>({
type: 'Feature',
id: data?.id,
properties: {},
geometry: {
type: 'Polygon',
coordinates: [data?.area.map((p: string) => p.split(', ').map(Number))],
},
});
const [draw, setDraw] = useState(new MapboxDraw({ displayControlsDefault: false }));
const user = useUser();
const router = useRouter();
const [loading, setLoading] = useState(false);

const [additionalData, setAdditionalData] = useState({
name: data.name,
id: data.id,
finished: data.finished,
active: data.active,
});

const editData = (property: string, value: any) => {
setAdditionalData({ ...additionalData, [property]: value });
};

const handleSubmit = () => {
setLoading(true);
fetch(process.env.NEXT_PUBLIC_API_URL + `/claims/${additionalData.id}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + user.token,
},
body: JSON.stringify({ ...additionalData, area: polygon.geometry.coordinates[0] }),
})
.then((res) => res.json())
.then((res) => {
if (res.errors) {
showNotification({
title: 'Update failed',
message: res.error,
color: 'red',
});
setLoading(false);
} else {
showNotification({
title: 'Claim updated',
message: 'All Data has been saved',
color: 'green',
icon: <IconCheck />,
});
setAdditionalData({ ...data, ...res });
setLoading(false);
}
});
};

return (
<Page
head={{
title: 'Edit Claim',
image: 'https://cdn.buildtheearth.net/static/thumbnails/me.png',
}}
smallPadding
>
{!data ? (
<></>
) : (
<Grid>
<Grid.Col span={{ md: 6 }}>
<TextInput
label="Claim Name"
required
defaultValue={additionalData.name}
onChange={(e) => editData('name', e.target.value)}
mb="md"
/>
<Switch
defaultChecked={additionalData.finished}
label="Claim is finished"
onChange={(e) => editData('finished', e.target.checked)}
mb="md"
/>
<Switch
defaultChecked={additionalData.active}
label="Show on map"
onChange={(e) => editData('active', e.target.checked)}
mb="md"
/>
<h3>Builders</h3>
<Alert
variant="light"
color="yellow"
mb="xl"
icon={<IconAlertCircle />}
title="Editing Builders currently does not work"
>
If there are any incorrect Builders listed for this claim, message us.
</Alert>
<Group>
<Button
variant="outline"
component="a"
href={
router.query.z && router.query.lat && router.query.lng
? `/map?z=${router.query.z}&lat=${router.query.lat}&lng=${router.query.lng}`
: '/map'
}
leftSection={<IconChevronLeft />}
>
Back
</Button>
<Button
onClick={handleSubmit}
disabled={!user?.token}
loading={loading}
leftSection={<IconDeviceFloppy />}
>
Save
</Button>
</Group>
<Alert variant="light" color="blue" mt="xl" icon={<IconQuestionMark />} title="How to edit Claim Polygons">
You can find an tutorial how to use the map on the right side{' '}
<Anchor href="https://docs.buildtheearth.net/docs/building/guidebook/" target="_blank">
here
</Anchor>
.
</Alert>
</Grid.Col>
<Grid.Col span={{ md: 6 }}>
<div style={{ height: '100%', width: '100%', minHeight: '50vh' }}>
<Map
initialOptions={{ center: data?.center.split(', ').map(Number), zoom: 12 }}
layerSetup={(map) => {
map.addControl(draw, 'top-right');
draw.add(polygon);
draw.changeMode('direct_select', { featureId: polygon.id });
}}
onMapLoaded={(map) => {
map.on('draw.update', (e) => setPolygon(e.features[0]));
}}
/>
</div>
</Grid.Col>
</Grid>
)}
</Page>
);
};

export default ClaimPage;

export async function getServerSideProps(context: any) {
const res = await fetcher('/claims/' + context.query.id);
return {
props: {
claimId: context.query.id,
data: res,
...(await serverSideTranslations(context.locale, ['common'])),
},
};
}
2 changes: 1 addition & 1 deletion src/pages/teams/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ export default Teams;
export async function getStaticProps({ locale }: any) {
const res = await fetcher('/buildteams');

return { props: { data: res, ...(await serverSideTranslations(locale, ['common', 'teams'])) } };
return { props: { data: res, ...(await serverSideTranslations(locale, ['common'])) } };
}
Loading

0 comments on commit 6e56689

Please sign in to comment.