Skip to content

Commit

Permalink
Merge pull request #26 from concord-consortium/188059331-get-data-but…
Browse files Browse the repository at this point in the history
…ton-in-sim

188059331 get data button in sim
  • Loading branch information
pjanik authored Aug 20, 2024
2 parents fbae411 + c67a390 commit 28e5c70
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 36 deletions.
18 changes: 18 additions & 0 deletions src/assets/scss/simulation-tab.scss
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
@import "./vars.scss";

.simulation-tab {
.get-data-button {
position: relative;
top: -15px;
height: 32px;
display: flex;
justify-content: center;
align-items: center;
margin-right: 25px;
color: $codap-teal;
border-radius: 3px;
border: 1px solid $codap-teal;
background-color: white;
cursor: pointer;
margin-left: auto;
}
}
54 changes: 38 additions & 16 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useCodapData } from "../hooks/useCodapData";
import { LocationTab } from "./location-tab";
import { SimulationTab } from "./simulation-tab";
import { Header } from "./header";
import { locationsEqual } from "../utils/daylight-utils";

import "../assets/scss/App.scss";

Expand Down Expand Up @@ -98,32 +99,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 Expand Up @@ -155,6 +157,23 @@ export const App: React.FC = () => {
});
};

const { getDayLengthData } = useCodapData();

const handleGetDataClick = async () => {
const name = locationSearch || `(${latitude}, ${longitude})`;
const currentLocation: ILocation = { name, latitude: Number(latitude), longitude: Number(longitude) };
const locationExists = locations.some(item => locationsEqual(item, currentLocation));
if (locationExists || !latitude || !longitude) return;

// if the location does not already exist, and we have params, get the data
const tableCreated = await getDayLengthData(currentLocation);
if (tableCreated?.success) {
const uniqeLocations = await getUniqueLocationsInCodapData();
if (uniqeLocations) setLocations(uniqeLocations);
}
};


return (
<div className="App">
<Header
Expand All @@ -175,6 +194,7 @@ export const App: React.FC = () => {
setDataContext={setDataContext}
locations={locations}
setLocations={setLocations}
handleGetDataClick={handleGetDataClick}
/>
</div>
<div className={clsx("tab-content", { active: activeTab === "simulation" })}>
Expand All @@ -187,6 +207,8 @@ export const App: React.FC = () => {
setLocationSearch={setLocationSearch}
dayOfYear={dayOfYear}
setDayOfYear={handleDayUpdateInTheSimTab}
setLocations={setLocations}
handleGetDataClick={handleGetDataClick}
/>
</div>
</div>
Expand Down
23 changes: 4 additions & 19 deletions src/components/location-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useCodapData } from "../hooks/useCodapData";
import { kChildCollectionAttributes } from "../constants";
import { ICodapDataContextInfo, ILocation } from "../types";
import { LocationPicker } from "./location-picker";
import { locationsEqual } from "../utils/daylight-utils";

import "../assets/scss/location-tab.scss";

Expand All @@ -20,6 +19,7 @@ interface LocationTabProps {
setSelectedAttributes: (attrs: string[]) => void;
setDataContext: (context: any) => void;
setLocations: (locations: ILocation[]) => void;
handleGetDataClick: (latitude: string, longitude: string) => void;
}

export const LocationTab: React.FC<LocationTabProps> = ({
Expand All @@ -32,15 +32,14 @@ export const LocationTab: React.FC<LocationTabProps> = ({
setLongitude,
setLocationSearch,
setSelectedAttributes,
setLocations
setLocations,
handleGetDataClick
}) => {

const {
dataContext,
handleClearData,
getDayLengthData,
updateAttributeVisibility,
getUniqueLocationsInCodapData
} = useCodapData();

useEffect(() => {
Expand Down Expand Up @@ -87,20 +86,6 @@ export const LocationTab: React.FC<LocationTabProps> = ({
setLocations([]);
};

const handleGetDataClick = async () => {
const name = locationSearch || `(${latitude}, ${longitude})`;
const currentLocation: ILocation = { name, latitude: Number(latitude), longitude: Number(longitude) };
const locationExists = locations.some(item => locationsEqual(item, currentLocation));
if (locationExists || !latitude || !longitude) return;

// if the location does not already exist, and we have params, get the data
const tableCreated = await getDayLengthData(currentLocation);
if (tableCreated?.success) {
const uniqeLocations = await getUniqueLocationsInCodapData();
if (uniqeLocations) setLocations(uniqeLocations);
}
};

return (
<div className="location-tab">
<LocationPicker
Expand Down Expand Up @@ -149,7 +134,7 @@ export const LocationTab: React.FC<LocationTabProps> = ({
<button onClick={handleClearDataClick} disabled={!dataContext}>
Clear Data
</button>
<button onClick={handleGetDataClick}>
<button onClick={() => handleGetDataClick(latitude, longitude)}>
Get Data
</button>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/components/simulation-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface SimulationTabProps {
setLocationSearch: (search: string) => void;
dayOfYear: number;
setDayOfYear: (day: number) => void;
setLocations: (locations: ILocation[]) => void;
handleGetDataClick: (latitude: string, longitude: string) => void;
}

export const SimulationTab: React.FC<SimulationTabProps> = ({
Expand All @@ -24,6 +26,8 @@ export const SimulationTab: React.FC<SimulationTabProps> = ({
setLocationSearch,
dayOfYear,
setDayOfYear,
setLocations,
handleGetDataClick
}) => {
return (
<div className="simulation-tab">
Expand All @@ -39,6 +43,9 @@ export const SimulationTab: React.FC<SimulationTabProps> = ({
locations={locations}
/>
</div>
<button className="get-data-button" onClick={() => handleGetDataClick(latitude, longitude)}>
Get Data
</button>
</div>
);
};
1 change: 0 additions & 1 deletion src/hooks/useCodapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const useCodapData = () => {
if (result.success) {
let dc = result.values;
let lastCollection = dc.collections[dc.collections.length - 1];
console.trace();
return await codapInterface.sendRequest({
action: "delete",
resource: `dataContext[${kDataContextName}].collection[${lastCollection.name}].allCases`
Expand Down

0 comments on commit 28e5c70

Please sign in to comment.