Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Reset back to initial model, if any [PT-187321069] #50

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 8 additions & 5 deletions src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { clsx } from "clsx";
import { useCODAP } from "../hooks/use-codap";
import { useGraph } from "../hooks/use-graph";
import { useGenerator } from "../hooks/use-generator";
import { Edge, Node } from "../type";
import { Edge, GraphData, Node } from "../type";
import { Drawing } from "./drawing";
import { Dataset } from "./dataset";

Expand Down Expand Up @@ -87,10 +87,12 @@ export const App = () => {
const currentSequenceIndex = useRef(0);
const animationInterval = useRef<number>();
const { graph, updateGraph, setGraph } = useGraph();
const [initialGraph, setInitialGraph] = useState<GraphData>();
const { dragging, outputToDataset, viewMode, setViewMode, notifyStateIsDirty, loadState } = useCODAP({
onCODAPDataChanged: updateGraph,
getGraph: useCallback(() => graph, [graph]),
setGraph
setGraph,
setInitialGraph
});
const { generate } = useGenerator();
const innerOutputRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -403,15 +405,16 @@ export const App = () => {
notifyStateIsDirty();
};

const handleReset = () => {
const handleReset = useCallback(() => {
if (confirm("Are you sure you want to reset?\n\nAny changes you have made will be lost.")) {
setGraph({nodes: [], edges: []});
setGraph(initialGraph ? {...initialGraph} : {nodes: [], edges: []});
}
};
}, [initialGraph, setGraph]);

const handleReturnToMainMenu = () => {
if (confirm("Are you sure you want to go back to the main menu?\n\nAny changes you have made will be lost.")) {
setGraph({nodes: [], edges: []});
setInitialGraph(undefined);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming Jie wants initial graph erased when going back to main menu instead of a saved state?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

setViewMode(undefined);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const nodeLoopPath = (node: D3Node) => {
};

const graphSignature = (graph: D3Graph) => {
const nodeSignature = graph.nodes.map(n => `${n.id}/${n.label}/${n.weight}/${n.radius}`);
const nodeSignature = graph.nodes.map(n => `${n.id}/${n.label}/${n.weight}/${n.radius}/${n.x}/${n.y}`);
const edgeSignature = graph.edges.map(e => `${e.source.id}/${e.target.id}/${e.weight}`);
return `${nodeSignature}::${edgeSignature}`;
};
Expand Down
9 changes: 6 additions & 3 deletions src/hooks/use-codap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useState } from "react";
import { GraphData, Node } from "../type";
import { Edge, GraphData, Node } from "../type";
import {
getValuesForAttribute,
// entityInfo,
Expand Down Expand Up @@ -34,9 +34,10 @@
onCODAPDataChanged: OnCODAPDataChanged;
getGraph: GetGraphCallback;
setGraph: React.Dispatch<React.SetStateAction<GraphData>>
setInitialGraph: React.Dispatch<React.SetStateAction<GraphData|undefined>>
};

export const useCODAP = ({onCODAPDataChanged, getGraph, setGraph}: UseCODAPOptions) => {
export const useCODAP = ({onCODAPDataChanged, getGraph, setGraph, setInitialGraph}: UseCODAPOptions) => {
const [loadState, setLoadState] = useState<"loading"|"loaded">("loading");
const [initialized, setInitialized] = useState(false);
const [dragging, setDragging] = useState(false);
Expand Down Expand Up @@ -89,6 +90,8 @@
const {nodes, edges} = values;
if (nodes !== undefined && edges !== undefined) {
setGraph({nodes, edges});
// save a copy of the graph
setInitialGraph({nodes: nodes.map((n: Node) => ({...n})), edges: edges.map((e: Edge) => ({...e}))});
}
} else {
if (values?.attribute) {
Expand All @@ -97,7 +100,7 @@
}
}
}
}, [setAttribute, handleDataChanged, setGraph]);
}, [setAttribute, handleDataChanged, setGraph, setInitialGraph]);

const handleDrop = useCallback(async (iMessage: any) => {
let newAttribute: CODAPAttribute;
Expand Down Expand Up @@ -145,7 +148,7 @@
"action": "get",
"resource": "dataContextList"
}).catch((reason) => {
console.log("unable to get data context list because " + reason);

Check warning on line 151 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 151 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
});
if (tListResult?.success) {
tFoundValue = tListResult.values.find((iValue: any) => {
Expand Down Expand Up @@ -176,10 +179,10 @@
]
}
}).catch((reason) => {
console.log("unable to create output dataset because " + reason);

Check warning on line 182 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 182 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
});
if (!(tCreateResult?.success)) {
console.log("unable to create output dataset");

Check warning on line 185 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 185 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
return;
}
// setOutputDatasetID(tCreateResult.values.id);
Expand All @@ -191,7 +194,7 @@
"action": "get",
"resource": "componentList"
}).catch((reason) => {
console.log("unable to get component list because " + reason);

Check warning on line 197 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 197 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
});
if (tListResult?.success) {
tFoundValue = tListResult.values.find((iValue: any) => {
Expand All @@ -210,10 +213,10 @@
position: "top"
}
}).catch((reason) => {
console.log("unable to create case table because " + reason);

Check warning on line 216 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 216 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
});
if (!(tCreateResult?.success)) {
console.log("unable to create case table");

Check warning on line 219 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 219 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
}
}
}, []);
Expand All @@ -231,7 +234,7 @@
resource: `dataContext[${OutputDatasetName}].item`,
values: requests
}).catch((reason) => {
console.log("unable to create items because " + reason);

Check warning on line 237 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / Build

Unexpected console statement

Check warning on line 237 in src/hooks/use-codap.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement
});

incrementSequenceNumber();
Expand Down
Loading