Skip to content

Commit

Permalink
feat: Add cachedFData state to EntryPage component
Browse files Browse the repository at this point in the history
This commit adds a new state variable, cachedFData, to the EntryPage component. The cachedFData state is used to store the previous value of fData before making changes to it. This allows for easy rollback to the last saved state in case of failed changes. The commit also includes minor code cleanup and commented out code for future reference.
  • Loading branch information
bramses committed Nov 6, 2024
1 parent c1b945c commit 3999d0c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
13 changes: 11 additions & 2 deletions src/components/EntryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const EntryPage = () => {
useState<TransactionManager | null>(null);
const [tempIds, setTempIds] = useState<string[]>([]);
const [tempCommentIDs, setTempCommentIDs] = useState<any[]>([]);
const [cachedFData, setCachedFData] = useState<any>(null);

const openModal = (key: string) =>
setModalStates((prev) => ({ ...prev, [key]: true }));
Expand Down Expand Up @@ -728,7 +729,8 @@ const EntryPage = () => {
setIsSaving(false);
alert('All changes saved successfully.');
// reload the page
window.location.reload();
// setCachedFData(fData);
// window.location.reload();
} catch (error) {
alert('Failed to save changes. Rolling back to the last saved state.');
// Rollback UI to last saved state
Expand Down Expand Up @@ -1052,6 +1054,12 @@ const EntryPage = () => {
comments: [],
internalLinks: [],
});

if (cachedFData) {
setFData(cachedFData);
console.log('using cached fdata:', cachedFData);
return;
}
await generateFData(data, data.metadata.aliasData);
// console.log('setting fdata:', fdata);
// setFData(fdata);
Expand Down Expand Up @@ -1202,7 +1210,8 @@ const EntryPage = () => {
...data.metadata,
});
}
window.location.reload();
setCachedFData(fData);
//window.location.reload();
}}
>
{data.metadata.isStarred ? 'Unstar' : 'Star'}
Expand Down
15 changes: 4 additions & 11 deletions src/components/ForceDirectedGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ const ForceDirectedGraph = ({ data }: any) => {
}),
);
}, [data]);


return (
<div ref={containerRef} style={{ width: '100%', margin: '0 auto' }}>
Expand All @@ -289,18 +290,9 @@ const ForceDirectedGraph = ({ data }: any) => {
onRequestClose={closeModal}
contentLabel="Node Content"
ariaHideApp={false}
style={{
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)',
},
}}
>
{modalContent.image ? (
<div className="flex flex-col">
{modalContent.image ? (
<img
src={modalContent.image}
alt="thumbnail"
Expand All @@ -317,6 +309,7 @@ const ForceDirectedGraph = ({ data }: any) => {
<button onClick={closeModal} type="button">
Close
</button>
</div>
</Modal>
</div>
);
Expand Down

0 comments on commit 3999d0c

Please sign in to comment.