Skip to content

Commit 115b0b1

Browse files
committed
Updating JSON Visualizer to track file version change
Redux bug regarding selected fileversion for file
1 parent c1558f1 commit 115b0b1

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

frontend/src/actions/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function changeSelectedVersion(fileId, selectedVersion) {
231231
return (dispatch) => {
232232
dispatch({
233233
type: CHANGE_SELECTED_VERSION,
234-
version: selectedVersion,
234+
selected_version: selectedVersion,
235235
receivedAt: Date.now(),
236236
});
237237
};

frontend/src/components/files/File.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useParams, useSearchParams } from "react-router-dom";
1515
import { useDispatch, useSelector } from "react-redux";
1616

1717
import { a11yProps, TabPanel } from "../tabs/TabComponent";
18-
import { fetchFileSummary, fetchFileVersions } from "../../actions/file";
18+
import { fetchFileSummary, fetchFileVersions, changeSelectedVersion } from "../../actions/file";
1919
import { MainBreadcrumbs } from "../navigation/BreadCrumb";
2020
import { FileVersionHistory } from "../versions/FileVersionHistory";
2121
import { DisplayMetadata } from "../metadata/DisplayMetadata";
@@ -77,6 +77,8 @@ export const File = (): JSX.Element => {
7777
dispatch(deleteFileMetadataAction(fileId, metadata));
7878
const getFolderPath = (folderId: string | null) =>
7979
dispatch(fetchFolderPath(folderId));
80+
const changeFileVersion = (fileId: string | undefined, version: number) =>
81+
dispatch(changeSelectedVersion(fileId, version));
8082

8183
const file = useSelector((state: RootState) => state.file);
8284
const latestVersionNum = useSelector(
@@ -179,6 +181,11 @@ export const File = (): JSX.Element => {
179181
setSelectedTabIndex(newTabIndex);
180182
};
181183

184+
// Set file version
185+
useEffect(() => {
186+
changeFileVersion(fileId, selectedVersionNum);
187+
}, [selectedVersionNum]);
188+
182189
const setMetadata = (metadata: any) => {
183190
// TODO wrap this in to a function
184191
setMetadataRequestForms((prevState) => {

frontend/src/components/visualizations/JSONVisualizer/JSONVisualizer.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
import CodeMirror from "@uiw/react-codemirror"; // CodeMirror editor
1010
import { json } from "@codemirror/lang-json"; // JSON language support for CodeMirror
1111
import { downloadVisData, fileDownloaded } from "../../../utils/visualization";
12-
import { updateFile as updateFileAction } from "../../../actions/file";
12+
import {fetchFileVersions, updateFile as updateFileAction} from "../../../actions/file";
1313
import { readTextFromFile } from "../../../utils/common";
1414
import { downloadPublicVisData } from "../../../actions/public_visualization";
1515
import { filePublicDownloaded } from "../../../actions/public_file";
@@ -23,32 +23,41 @@ type jsonProps = {
2323
};
2424

2525
export default function JSONVisualizer(props: jsonProps) {
26-
const { fileId, visualizationId, publicView } = props;
26+
const { visualizationId, publicView } = props;
27+
// TODO: Use fileData to get the fileid to reflect version change
28+
const fileId = useSelector((state: RootState) => state.file.fileSummary?.id);
29+
const versionNum = useSelector((state: RootState) => state.file.selected_version_num);
30+
const fileSummary = useSelector((state: RootState) => state.file.fileSummary);
31+
const fileData = useSelector((state: RootState) => state.file);
2732

2833
// State to store the original content of the file and the displayed JSON content that can be edited
2934
const [originalContent, setOriginalContent] = useState<string | undefined>();
3035
const [jsonContent, setJsonContent] = useState<string | undefined>();
3136

32-
// Utility states to help with saving the file, displaying loading spinner and validating JSON
37+
// Utility state to help with saving the file, displaying loading spinner and validating JSON
3338
const [fileName, setFileName] = useState<string | undefined>();
3439
const [loading, setLoading] = useState<boolean>(false);
3540
const [validJson, setValidJson] = useState<boolean>(true);
3641

37-
// use useSelector to get fileSummary to get filename.
38-
const fileData = useSelector((state: RootState) => state.file);
39-
4042
// use useDispatch to update file
4143
const dispatch = useDispatch();
4244
const updateFile = async (file: File, fileId: string | undefined) =>
4345
dispatch(updateFileAction(file, fileId));
4446

4547
useEffect(() => {
46-
if (fileData.fileSummary) {
47-
setFileName(fileData.fileSummary.name);
48+
console.log("File Data: ", fileData);
49+
}, [fileData]);
50+
51+
useEffect(() => {
52+
if (fileSummary) {
53+
setFileName(fileSummary.name);
54+
console.log("File Summary: ", fileSummary);
4855
}
49-
}, [fileData.fileSummary]);
56+
}, [fileSummary]);
5057

5158
useEffect(() => {
59+
console.log("File ID: ", fileId);
60+
console.log("Version Num: ", versionNum);
5261
const fetchData = async () => {
5362
try {
5463
let blob;
@@ -59,7 +68,7 @@ export default function JSONVisualizer(props: jsonProps) {
5968
} else {
6069
blob = publicView
6170
? await filePublicDownloaded(fileId)
62-
: await fileDownloaded(fileId, 0);
71+
: await fileDownloaded(fileId, versionNum);
6372
}
6473

6574
const file = new File([blob], fileName);
@@ -71,7 +80,7 @@ export default function JSONVisualizer(props: jsonProps) {
7180
}
7281
};
7382
fetchData();
74-
}, [visualizationId, fileId, publicView]);
83+
}, [visualizationId, fileId, publicView, versionNum]);
7584

7685
const validateJson = (jsonString: string) => {
7786
try {
@@ -92,11 +101,11 @@ export default function JSONVisualizer(props: jsonProps) {
92101
if (
93102
jsonContent !== undefined &&
94103
fileName &&
95-
fileData.fileSummary?.content_type
104+
fileSummary?.content_type
96105
) {
97106
const textBlob = new Blob([jsonContent], { type: "text/plain" });
98107
const file = new File([textBlob], fileName, {
99-
type: fileData.fileSummary.content_type.content_type,
108+
type: fileSummary.content_type.content_type,
100109
});
101110

102111
setLoading(true);

0 commit comments

Comments
 (0)