Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mariogiampieri committed Oct 19, 2024
1 parent d86f3eb commit 3b47517
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
53 changes: 4 additions & 49 deletions app/src/app/components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map | null> = useRef(null);
const mapContainer: MutableRefObject<HTMLDivElement | null> = 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();
Expand Down
37 changes: 36 additions & 1 deletion app/src/app/utils/api/apiHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export const getZonePopulations: (
export const getAvailableDistrictrMaps: (
limit?: number,
offset?: number
) => Promise<gerryDBView[]> = async (limit = 10, offset = 0) => {
) => Promise<DistrictrMap[]> = async (limit = 10, offset = 0) => {
return await axios
.get(
`${process.env.NEXT_PUBLIC_API_URL}/api/gerrydb/views?limit=${limit}&offset=${offset}`
Expand All @@ -185,6 +185,7 @@ export interface Assignment {
document_id: string;
geo_id: string;
zone: number;
parent_path?: string;
}

/**
Expand Down Expand Up @@ -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<ShatterResult> = 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;
});
};
31 changes: 31 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit 3b47517

Please sign in to comment.