Skip to content

Commit 66a2e1f

Browse files
committed
The update is now working on save
However the interactions with file versions is currently broken and need something
1 parent e271848 commit 66a2e1f

File tree

4 files changed

+119
-76
lines changed

4 files changed

+119
-76
lines changed

frontend/src/components/visualizations/JSON/JSON.tsx

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import React, { useEffect, useState } from "react";
2+
import {
3+
Button,
4+
Card,
5+
CardContent,
6+
CardActions,
7+
CircularProgress,
8+
} from "@mui/material";
9+
import CodeMirror from "@uiw/react-codemirror"; // CodeMirror editor
10+
import { json } from "@codemirror/lang-json"; // JSON language support for CodeMirror
11+
import { downloadVisData, fileDownloaded } from "../../../utils/visualization";
12+
import { updateFile as updateFileAction } from "../../../actions/file";
13+
import { readTextFromFile } from "../../../utils/common";
14+
import { downloadPublicVisData } from "../../../actions/public_visualization";
15+
import { filePublicDownloaded } from "../../../actions/public_file";
16+
import { useDispatch, useSelector } from "react-redux";
17+
import { RootState } from "../../../types/data";
18+
19+
type jsonProps = {
20+
fileId?: string;
21+
visualizationId?: string;
22+
publicView?: boolean;
23+
};
24+
25+
export default function JSONVisualizer(props: jsonProps) {
26+
const { fileId, visualizationId, publicView } = props;
27+
const [jsonContent, setJsonContent] = useState<string | undefined>();
28+
const [fileName, setFileName] = useState<string | undefined>();
29+
const [loading, setLoading] = useState<boolean>(false);
30+
31+
// use useSelector to get fileSummary to get filename.
32+
const fileData = useSelector((state: RootState) => state.file);
33+
34+
// use useDispatch to update file
35+
const dispatch = useDispatch();
36+
const updateFile = async (file: File, fileId: string | undefined) =>
37+
dispatch(updateFileAction(file, fileId));
38+
39+
useEffect(() => {
40+
if (fileData.fileSummary) {
41+
setFileName(fileData.fileSummary.name);
42+
}
43+
}, [fileData.fileSummary]);
44+
45+
useEffect(() => {
46+
console.log("fileData", fileData.fileSummary.content_type);
47+
}, [fileName]);
48+
49+
useEffect(() => {
50+
const fetchData = async () => {
51+
try {
52+
let blob;
53+
if (visualizationId) {
54+
blob = publicView
55+
? await downloadPublicVisData(visualizationId)
56+
: await downloadVisData(visualizationId);
57+
} else {
58+
blob = publicView
59+
? await filePublicDownloaded(fileId)
60+
: await fileDownloaded(fileId, 0);
61+
}
62+
63+
const file = new File([blob], fileName);
64+
const text = await readTextFromFile(file);
65+
setJsonContent(text);
66+
} catch (error) {
67+
console.error("Error fetching data:", error);
68+
}
69+
};
70+
fetchData();
71+
}, [visualizationId, fileId, publicView]);
72+
73+
const handleSave = async () => {
74+
try {
75+
if (
76+
jsonContent !== undefined &&
77+
fileName &&
78+
fileData.fileSummary?.content_type
79+
) {
80+
// Parse the jsonContent to ensure it's valid JSON
81+
const textBlob = new Blob([jsonContent], { type: "text/plain" });
82+
const file = new File([textBlob], fileName, {
83+
type: fileData.fileSummary.content_type.content_type,
84+
});
85+
86+
setLoading(true);
87+
await updateFile(file, fileId);
88+
setLoading(false);
89+
}
90+
} catch (error) {
91+
console.error("Error updating file:", error);
92+
}
93+
};
94+
95+
return (
96+
<Card>
97+
<CardContent>
98+
{loading ? (
99+
<CircularProgress />
100+
) : (
101+
<CodeMirror
102+
value={jsonContent}
103+
extensions={[json()]}
104+
onChange={(value) => setJsonContent(value)}
105+
theme="dark"
106+
/>
107+
)}
108+
</CardContent>
109+
<CardActions>
110+
<Button variant="contained" color="primary" onClick={handleSave}>
111+
Save Changes
112+
</Button>
113+
</CardActions>
114+
</Card>
115+
);
116+
}

frontend/src/components/visualizations/JSON/manifest.json renamed to frontend/src/components/visualizations/JSONVisualizer/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "json-editor",
33
"version": "1.0.0",
44
"description": "A React component for visualizing and editing JSON data",
5-
"main": "JSON.tsx",
5+
"main": "JSONVisualizer.tsx",
66
"dependencies": {
77
"clowder2-core": "1.0.0",
88
"react": "^17.0.2",
@@ -11,7 +11,7 @@
1111
"@codemirror/lang-json": "^6.0.1"
1212
},
1313
"visConfig": {
14-
"name": "JSON",
14+
"name": "JSONVisualizer",
1515
"mainType": "application",
1616
"mimeTypes": ["application/json"],
1717
"fields": []

frontend/src/visualization.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ visComponentDefinitions.push({
9292
// component: React.createElement(registerComponent(configWordCloudSpec)),
9393
// });
9494

95-
const configJSON = require("./components/visualizations/JSON/manifest.json");
95+
const configJSON = require("./components/visualizations/JSONVisualizer/manifest.json");
9696
visComponentDefinitions.push({
9797
name: configJSON.name,
9898
mainType: configJSON.visConfig.mainType,

0 commit comments

Comments
 (0)