Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
934b854
handle creating waypoints
Michael-R-Dickinson Jan 16, 2025
3a3959d
added useWaypoint hook for handling all waypoint operations
Michael-R-Dickinson Jan 16, 2025
75e4385
updated waypoint UI
Michael-R-Dickinson Jan 16, 2025
56d8d7c
fixed endpoint delete function
Michael-R-Dickinson Jan 16, 2025
3737301
synced ordering logic up with backend
Michael-R-Dickinson Jan 17, 2025
7bc6854
fixed waypoint editing logic
Michael-R-Dickinson Jan 17, 2025
8ea3f41
deserialize waypoints from backend correctly
Michael-R-Dickinson Jan 17, 2025
22e8f24
prevented frontend from updating waypoints if the backend query fails
Michael-R-Dickinson Jan 17, 2025
3fd7d45
Merge branch 'master' of https://github.com/ubcuas/GCOM-Front-End int…
Michael-R-Dickinson Jan 17, 2025
e9abff9
removed uesless fields
Michael-R-Dickinson Jan 21, 2025
df9f96d
updated name field size to be fullwidth
Michael-R-Dickinson Jan 21, 2025
c70df20
ensure that waypoint's id's are properly set on frontend
Michael-R-Dickinson Feb 8, 2025
eb79fce
removed waypoint fields
Michael-R-Dickinson Feb 9, 2025
b3f9b82
moved to use appslice to persist waypoint data
Michael-R-Dickinson Feb 9, 2025
dc20603
fixed waypoint creation map view to work with new waypoint system
Michael-R-Dickinson Feb 9, 2025
b65e1e5
added error handling for waypoint form data
Michael-R-Dickinson Feb 9, 2025
cf7685f
changed waypoint form logic to be explicit about edit states
Michael-R-Dickinson Feb 9, 2025
486bce2
made waypoint coords show live as dragged on map
Michael-R-Dickinson Feb 9, 2025
2af46be
added comments
Michael-R-Dickinson Feb 9, 2025
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
32 changes: 26 additions & 6 deletions src/api/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Waypoint } from "../types/Waypoint";
import api from "./api";

// TODO: Implement new endpoint logic
import { AxiosResponse } from "axios";
import { deserializeWaypoints, serializeWaypointForGCOM } from "./formatters";

export const armDrone = async (arm: boolean) => {
return await api.post("/drone/arm", { arm });
Expand All @@ -11,14 +11,34 @@ export const takeoffDrone = async (altitude?: number) => {
return await api.post("/drone/takeoff", { altitude });
};

export const postWaypointsToDrone = async (waypoints: Waypoint[]) => {
return await api.post("/drone/queue", waypoints);
};

export const getGCOM = async (): Promise<Waypoint[]> => {
return (await api.get("/drone/queue")) as Waypoint[];
};

export const getRoute = async (): Promise<Waypoint[]> => {
return (await api.get("/route")) as Waypoint[];
};

export const getWaypointsQuery = async (): Promise<Waypoint[]> => {
const response = await api.get("/waypoint");
const waypoints = deserializeWaypoints(response.data);
return waypoints;
};

export const createWaypointQuery = async (waypoint: Waypoint): Promise<AxiosResponse> => {
const serializedWaypoint = serializeWaypointForGCOM(waypoint);
return api.post("/waypoint/", serializedWaypoint);
};

export const updateWaypointQuery = async (waypoint: Waypoint): Promise<AxiosResponse> => {
const serializedWaypoint = serializeWaypointForGCOM(waypoint);
return api.patch(`/waypoint/${waypoint.id}/`, serializedWaypoint);
};

export const deleteWaypointQuery = async (id: string): Promise<AxiosResponse> => {
return api.delete(`/waypoint/${id}/`);
};

export const reorderWaypointsQuery = async (waypointIds: string[]): Promise<AxiosResponse> => {
return api.post("/waypoint/reorder", waypointIds);
};
39 changes: 39 additions & 0 deletions src/api/formatters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Waypoint } from "../types/Waypoint";

export const serializeWaypointForGCOM = (waypoint: Waypoint) => {
return {
name: waypoint.name,
latitude: waypoint.lat,
longitude: waypoint.long,
altitude: waypoint.alt,
radius: waypoint.radius,
order: waypoint.order,
route: 1,
};
};

export const deserializeWaypoints = (
waypoints: Array<{
id: string;
name: string;
latitude: number;
longitude: number;
altitude: number;
radius: number;
pass_radius: number;
pass_option: number;
order: number;
route: number;
}>,
): Waypoint[] => {
return waypoints
.sort((a, b) => a.order - b.order)
.map((wp) => ({
id: wp.id,
name: wp.name,
lat: wp.latitude,
long: wp.longitude,
alt: wp.altitude,
radius: wp.radius,
}));
};
79 changes: 43 additions & 36 deletions src/components/Map/WaypointCreationMap.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
// Honestly its just easier to have this in a separate file rather than having it be in with MapView.tsx

import { Place } from "@mui/icons-material";
import { AddHomeWork, Place } from "@mui/icons-material";
import { Fragment, useState } from "react";
import { Layer, LayerProps, Map, MapLayerMouseEvent, Marker, Source } from "react-map-gl/maplibre";
import {
addToQueuedWaypoints,
editWaypointAtIndex,
selectMapCenterCoords,
selectQueuedWaypoints,
} from "../../store/slices/appSlice";
import { selectMapCenterCoords, selectWaypoints } from "../../store/slices/appSlice";
import { useAppDispatch, useAppSelector } from "../../store/store";
import WaypointItem from "../WaypointItem";
import { roundTo } from "../../utils/routeTo";
import { Box } from "@mui/material";
import { WaypointEditState } from "../../types/Waypoint";
import { Waypoint, WaypointEditState } from "../../types/Waypoint";
import { useWaypoints } from "../../utils/useWaypoints";

type DraggedMarker = {
long: number;
Expand All @@ -23,20 +19,27 @@ type DraggedMarker = {

type CreationMapProps = {
handleDelete: (index: number) => void;
handleEdit: (index: number) => void;
editState: WaypointEditState;
startEditing: (index: number) => void;
submitWaypoint: (wp: Waypoint) => void;
setEditingCoords: (coords: { lat: number; long: number }) => void;
editingIndex: number;
};

export default function WaypointCreationMap({ handleDelete, handleEdit, editState }: CreationMapProps) {
export default function WaypointCreationMap({
handleDelete,
startEditing,
editingIndex,
setEditingCoords,
submitWaypoint,
}: CreationMapProps) {
const coords = useAppSelector(selectMapCenterCoords);
const clientWPQueue = useAppSelector(selectQueuedWaypoints);
const dispatch = useAppDispatch();
const [selectedWaypoints, setSelectedWaypoints] = useState<boolean[]>(clientWPQueue.map(() => false));
const { waypoints } = useWaypoints();
const [selectedWaypoints, setSelectedWaypoints] = useState<boolean[]>(waypoints?.map(() => false) || []);
const [draggedMarkerData, setDraggedMarkerData] = useState<DraggedMarker | null>(null);

const routeData: GeoJSON.GeoJSON = {
type: "LineString",
coordinates: clientWPQueue.map((waypoint) => [waypoint.long, waypoint.lat]),
coordinates: waypoints?.map((waypoint) => [waypoint.long, waypoint.lat]) || [],
};
const routeStyle: LayerProps = {
id: "mps-route",
Expand All @@ -49,13 +52,15 @@ export default function WaypointCreationMap({ handleDelete, handleEdit, editStat

const createNewWaypoint = (event: MapLayerMouseEvent) => {
if (event.originalEvent.detail !== 2) return;
dispatch(
addToQueuedWaypoints({
id: "-1",
lat: roundTo(event.lngLat.lat, 7),
long: roundTo(event.lngLat.lng, 7),
}),
);
const wp = {
lat: roundTo(event.lngLat.lat, 7),
long: roundTo(event.lngLat.lng, 7),
alt: 0,
radius: 123123,
name: "New Waypoint",
id: "-1",
};
submitWaypoint(wp);
setSelectedWaypoints((prev) => [...prev, false]);
};

Expand All @@ -77,15 +82,15 @@ export default function WaypointCreationMap({ handleDelete, handleEdit, editStat
mapStyle={
window.navigator.onLine
? "https://api.maptiler.com/maps/basic-v2/style.json?key=ioE7W2lCif3DO9oj1YJh"
: "./src/mapStyles/osmbright.json"
: "http://localhost:8000/api/map-tiles/osmbright"
}
onClick={createNewWaypoint}
doubleClickZoom={false}
style={{
minHeight: "500px",
}}
>
{clientWPQueue.map((waypoint, i) => {
{waypoints?.map((waypoint, i) => {
return (
<Fragment key={i}>
<Marker
Expand All @@ -94,24 +99,27 @@ export default function WaypointCreationMap({ handleDelete, handleEdit, editStat
e.originalEvent.stopPropagation();
handleSelectWaypoint(i);
}}
onDragStart={(e) => {
startEditing(i);
}}
onDrag={(e) => {
setEditingCoords({
lat: e.lngLat.lat,
long: e.lngLat.lng,
});
setDraggedMarkerData({
long: e.lngLat.lng,
lat: e.lngLat.lat,
index: i,
});
}}
onDragEnd={() => {
dispatch(
editWaypointAtIndex({
index: i,
waypoint: {
...waypoint,
lat: roundTo(draggedMarkerData!.lat, 7),
long: roundTo(draggedMarkerData!.long, 7),
},
}),
);
const wp = {
...waypoint,
lat: roundTo(draggedMarkerData!.lat, 7),
long: roundTo(draggedMarkerData!.long, 7),
};
submitWaypoint(wp);
setDraggedMarkerData(null);
}}
latitude={
Expand Down Expand Up @@ -172,7 +180,7 @@ export default function WaypointCreationMap({ handleDelete, handleEdit, editStat
width: "220px",
top: "10px",
border: "4px solid",
borderColor: i === editState.index ? "primary.main" : "transparent",
borderColor: i === editingIndex ? "primary.main" : "transparent",
}}
waypoint={waypoint}
handleDelete={() => {
Expand All @@ -182,7 +190,6 @@ export default function WaypointCreationMap({ handleDelete, handleEdit, editStat
return prev;
});
}}
handleEdit={() => handleEdit(i)}
/>
</Marker>
)}
Expand Down
65 changes: 0 additions & 65 deletions src/components/WaypointItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,71 +64,6 @@ export default function WaypointItem({ waypoint, sx, handleDelete, handleEdit }:
{waypoint.alt}
</Typography>
</Typography>
{waypoint.command && (
<Typography variant="body1">
Command{" "}
<Typography
component="span"
sx={{
color: (theme) => theme.palette.primary.main,
}}
>
{waypoint.command}
</Typography>
</Typography>
)}
{waypoint.param1 && (
<Typography variant="body1">
Param1{" "}
<Typography
component="span"
sx={{
color: (theme) => theme.palette.primary.main,
}}
>
{waypoint.param1}
</Typography>
</Typography>
)}
{waypoint.param2 && (
<Typography variant="body1">
Param2{" "}
<Typography
component="span"
sx={{
color: (theme) => theme.palette.primary.main,
}}
>
{waypoint.param2}
</Typography>
</Typography>
)}
{waypoint.param3 && (
<Typography variant="body1">
Param3{" "}
<Typography
component="span"
sx={{
color: (theme) => theme.palette.primary.main,
}}
>
{waypoint.param3}
</Typography>
</Typography>
)}
{waypoint.param4 && (
<Typography variant="body1">
Param4{" "}
<Typography
component="span"
sx={{
color: (theme) => theme.palette.primary.main,
}}
>
{waypoint.param4}
</Typography>
</Typography>
)}
{waypoint.remarks && <Typography color="grey">Remarks -- {waypoint.remarks}</Typography>}
</Paper>
);
Expand Down
Loading