Skip to content

Commit

Permalink
update locations array when location is edited in codap
Browse files Browse the repository at this point in the history
  • Loading branch information
bacalj committed Aug 19, 2024
1 parent 8cec602 commit be3e4af
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
33 changes: 17 additions & 16 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,33 @@ export const App: React.FC = () => {
}, []);

const handleDataContextChange = useCallback(async (listenerRes: ClientNotification) => {
console.log("| dataContextChangeNotice: ", listenerRes);
const { resource, values } = listenerRes;
const isResource = resource === `dataContextChangeNotice[${kDataContextName}]`;
if (!isResource || !values.result.success) return;
if (!isResource || !values.result.success || !values.result.cases) return;

const caseType = values.result.cases[0].parent ? "child" : "parent";
const casesDeleted = values.operation === "selectCases" && values.result.cases.length === 0
const caseSelected = values.operation === "selectCases" && values.result.cases.length === 1;
const parentCaseUpdated = values.operation === "updateCases" && caseType === "parent" && values.result.cases.length === 1;

//TODO: there is an unhandled path when we edit the location name
// we can use this to update the location name in the UI

if (casesDeleted) {
if (casesDeleted || parentCaseUpdated) {
const uniqueLocations = await getUniqueLocationsRef.current();
if (uniqueLocations) setLocations(uniqueLocations);
}

else if (caseSelected) {
const parentCaseId = values.result.cases[0].parent;
const selectedDay = values.result.cases[0].values.dayOfYear;
const parentCase = await getCaseByID(kDataContextName, parentCaseId);
const selectedLatitude = parentCase.values.case.values.latitude;
const selectedLongitude = parentCase.values.case.values.longitude;
handleCaseSelectionInCodap(
selectedLatitude,
selectedLongitude,
selectedDay
);
if (caseType === "child") {
const parentCaseId = values.result.cases[0].parent;
const selectedDay = values.result.cases[0].values.dayOfYear;
const parentCase = await getCaseByID(kDataContextName, parentCaseId);
const selectedLatitude = parentCase.values.case.values.latitude;
const selectedLongitude = parentCase.values.case.values.longitude;
handleCaseSelectionInCodap(
selectedLatitude,
selectedLongitude,
selectedDay
);
}
}
}, [handleCaseSelectionInCodap]);

Expand Down
1 change: 0 additions & 1 deletion src/components/simulation-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const SimulationTab: React.FC<SimulationTabProps> = ({
const { getDayLengthData, getUniqueLocationsInCodapData } = useCodapData();

const handleGetDataClick = async () => {
console.log("||| SIM tab click");
const name = `(${latitude}, ${longitude})`;
const currentLocation: ILocation = { name, latitude: Number(latitude), longitude: Number(longitude) };
const locationExists = locations.some(item => locationsEqual(item, currentLocation));
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useCodapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export const useCodapData = () => {
const locationAttr = await getAttribute(kDataContextName, kParentCollectionName, "location");
if (locationAttr.success){
const allItems = await getAllItems(kDataContextName);
console.log("||| getting locations in CODAP data... sync opportunity ", allItems);
if (allItems.success){
const uniqeLocations: ILocation[] = extractUniqueLocations(allItems.values);
return uniqeLocations;
Expand Down

0 comments on commit be3e4af

Please sign in to comment.