From 3b47517077698678d7dd2f0f273ced5c6c5748c6 Mon Sep 17 00:00:00 2001 From: marioag Date: Sat, 19 Oct 2024 10:11:04 -0400 Subject: [PATCH] cleanup --- app/src/app/components/Map.tsx | 53 +++------------------------- app/src/app/utils/api/apiHandlers.ts | 37 ++++++++++++++++++- backend/app/main.py | 31 ++++++++++++++++ 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/app/src/app/components/Map.tsx b/app/src/app/components/Map.tsx index c03acfedd..9c530c95e 100644 --- a/app/src/app/components/Map.tsx +++ b/app/src/app/components/Map.tsx @@ -8,61 +8,16 @@ import { Protocol } from "pmtiles"; import type { MutableRefObject } from "react"; import React, { useEffect, useRef } from "react"; import { MAP_OPTIONS } from "../constants/configuration"; -import { - mapEvents, - useHoverFeatureIds, - handleResetMapSelectState, -} from "../utils/events/mapEvents"; -import { BLOCK_HOVER_LAYER_ID, BLOCK_SOURCE_ID } from "../constants/layers"; -import { useSearchParams } from "next/navigation"; +import { mapEvents } from "../utils/events/mapEvents"; +import { INTERACTIVE_LAYERS } from "../constants/layers"; import { useMapStore } from "../store/mapStore"; export const MapComponent: React.FC = () => { const map: MutableRefObject = useRef(null); const mapContainer: MutableRefObject = useRef(null); - const [mapLoaded, setMapLoaded] = useState(false); - const hoverFeatureIds = useHoverFeatureIds(); + const mapLock = useMapStore((state) => state.mapLock); - const patchUpdates = useMutation({ - mutationFn: patchUpdateAssignments, - onMutate: () => { - console.log("Updating assignments"); - }, - onError: (error) => { - console.log("Error updating assignments: ", error); - }, - onSuccess: (data: AssignmentsCreate) => { - console.log( - `Successfully upserted ${data.assignments_upserted} assignments` - ); - mapMetrics.refetch(); - }, - }); - - const { - activeTool, - freshMap, - zoneAssignments, - mapDocument, - setMapDocument, - setSelectedLayer, - setMapRef, - setMapMetrics, - } = useMapStore((state) => ({ - activeTool: state.activeTool, - freshMap: state.freshMap, - zoneAssignments: state.zoneAssignments, - mapDocument: state.mapDocument, - setMapDocument: state.setMapDocument, - setSelectedLayer: state.setSelectedLayer, - setMapRef: state.setMapRef, - setMapMetrics: state.setMapMetrics, - })); - - const mapMetrics = useQuery({ - queryKey: ["zonePopulations", mapDocument], - queryFn: mapDocument ? () => getZonePopulations(mapDocument) : skipToken, - }); + const setMapRef = useMapStore((state) => state.setMapRef); useEffect(() => { let protocol = new Protocol(); diff --git a/app/src/app/utils/api/apiHandlers.ts b/app/src/app/utils/api/apiHandlers.ts index e5921b6ae..02c67fd22 100644 --- a/app/src/app/utils/api/apiHandlers.ts +++ b/app/src/app/utils/api/apiHandlers.ts @@ -164,7 +164,7 @@ export const getZonePopulations: ( export const getAvailableDistrictrMaps: ( limit?: number, offset?: number -) => Promise = async (limit = 10, offset = 0) => { +) => Promise = async (limit = 10, offset = 0) => { return await axios .get( `${process.env.NEXT_PUBLIC_API_URL}/api/gerrydb/views?limit=${limit}&offset=${offset}` @@ -185,6 +185,7 @@ export interface Assignment { document_id: string; geo_id: string; zone: number; + parent_path?: string; } /** @@ -212,3 +213,37 @@ export const patchUpdateAssignments: ( return res.data; }); }; + +/** + * Shatter result + * @interface + * @property {string[]} parents - The parents. + * @property {Assignment[]} children - The children. + */ +export interface ShatterResult { + parents: { geoids: string[] }; + children: Assignment[]; +} + +/** + * Shatter parents + * + * @param document_id - string, the document id + * @param geoids - string[], the geoids to shatter + * @returns list of child assignments results from shattered parents + */ +export const patchShatterParents: (params: { + document_id: string; + geoids: string[]; +}) => Promise = async ({ document_id, geoids }) => { + return await axios + .patch( + `${process.env.NEXT_PUBLIC_API_URL}/api/update_assignments/${document_id}/shatter_parents`, + { + geoids: geoids, + } + ) + .then((res) => { + return res.data; + }); +}; diff --git a/backend/app/main.py b/backend/app/main.py index 50ba54c06..209095c34 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -142,6 +142,37 @@ async def update_assignments( return {"assignments_upserted": len(data.assignments)} +@app.patch( + "/api/update_assignments/{document_id}/shatter_parents", + response_model=ShatterResult, +) +async def shatter_parent( + document_id: str, data: GEOIDS, session: Session = Depends(get_session) +): + stmt = text( + """SELECT * + FROM shatter_parent(:input_document_id, :parent_geoids)""" + ).bindparams( + bindparam(key="input_document_id", type_=UUIDType), + bindparam(key="parent_geoids", type_=ARRAY(String)), + ) + results = session.execute( + statement=stmt, + params={ + "input_document_id": document_id, + "parent_geoids": data.geoids, + }, + ) + # :( was getting validation errors so am just going to loop + assignments = [ + Assignments(document_id=str(document_id), geo_id=geo_id, zone=zone) + for document_id, geo_id, zone in results + ] + result = ShatterResult(parents=data, children=assignments) + session.commit() + return result + + # called by getAssignments in apiHandlers.ts @app.get("/api/get_assignments/{document_id}", response_model=list[AssignmentsResponse]) async def get_assignments(document_id: str, session: Session = Depends(get_session)):