Skip to content

Commit 79f22b2

Browse files
committed
Need to fix the update part of this
1 parent 7d054e0 commit 79f22b2

File tree

5 files changed

+311
-0
lines changed

5 files changed

+311
-0
lines changed

frontend/package-lock.json

Lines changed: 209 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"author": "NCSA",
3636
"dependencies": {
3737
"@appbaseio/reactivesearch": "^3.40.2",
38+
"@codemirror/lang-json": "^6.0.1",
3839
"@emotion/react": "^11.7.1",
3940
"@emotion/styled": "^11.6.0",
4041
"@material-ui/core": "^4.12.3",
@@ -49,6 +50,7 @@
4950
"@rjsf/validator-ajv8": "^5.18.4",
5051
"@types/d3": "^7.4.3",
5152
"@types/react-gravatar": "^2.6.10",
53+
"@uiw/react-codemirror": "^4.23.5",
5254
"babel-polyfill": "^6.26.0",
5355
"classnames": "^2.2.6",
5456
"csvtojson": "^2.0.10",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React, { useEffect, useState } from "react";
2+
import { Button, Card, CardContent, CardActions } from "@mui/material";
3+
import CodeMirror from "@uiw/react-codemirror"; // CodeMirror editor
4+
import { json } from "@codemirror/lang-json"; // JSON language support for CodeMirror
5+
import {
6+
downloadVisData,
7+
fileDownloaded,
8+
updateFile,
9+
} from "../../../utils/visualization";
10+
import { readTextFromFile } from "../../../utils/common";
11+
import { downloadPublicVisData } from "../../../actions/public_visualization";
12+
import { filePublicDownloaded } from "../../../actions/public_file";
13+
14+
type jsonProps = {
15+
fileId?: string;
16+
visualizationId?: string;
17+
publicView?: boolean;
18+
};
19+
20+
export default function JSON(props: jsonProps) {
21+
const { fileId, visualizationId, publicView } = props;
22+
const [jsonContent, setJsonContent] = useState<string | undefined>();
23+
24+
useEffect(() => {
25+
const fetchData = async () => {
26+
try {
27+
let blob;
28+
if (visualizationId) {
29+
blob = publicView
30+
? await downloadPublicVisData(visualizationId)
31+
: await downloadVisData(visualizationId);
32+
} else {
33+
blob = publicView
34+
? await filePublicDownloaded(fileId)
35+
: await fileDownloaded(fileId, 0);
36+
}
37+
38+
const file = new File([blob], "data.json");
39+
const text = await readTextFromFile(file);
40+
setJsonContent(text);
41+
} catch (error) {
42+
console.error("Error fetching data:", error);
43+
}
44+
};
45+
fetchData();
46+
}, [visualizationId, fileId, publicView]);
47+
48+
const handleSave = async () => {
49+
try {
50+
await updateFile(fileId, jsonContent);
51+
} catch (error) {
52+
console.error("Error updating file:", error);
53+
}
54+
};
55+
56+
return (
57+
<Card>
58+
<CardContent>
59+
<CodeMirror
60+
value={jsonContent}
61+
extensions={[json()]}
62+
onChange={(value) => setJsonContent(value)}
63+
theme="dark"
64+
/>
65+
</CardContent>
66+
<CardActions>
67+
<Button variant="contained" color="primary" onClick={handleSave}>
68+
Save Changes
69+
</Button>
70+
</CardActions>
71+
</Card>
72+
);
73+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "json-editor",
3+
"version": "1.0.0",
4+
"description": "A React component for visualizing and editing JSON data",
5+
"main": "JSON.tsx",
6+
"dependencies": {
7+
"clowder2-core": "1.0.0",
8+
"react": "^17.0.2",
9+
"react-dom": "^17.0.2",
10+
"@uiw/react-codemirror": "^4.23.5",
11+
"@codemirror/lang-json": "^6.0.1"
12+
},
13+
"visConfig": {
14+
"name": "JSON",
15+
"mainType": "application",
16+
"mimeTypes": ["application/json"],
17+
"fields": []
18+
}
19+
}

frontend/src/visualization.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,12 @@ visComponentDefinitions.push({
9292
component: React.createElement(registerComponent(configWordCloudSpec)),
9393
});
9494

95+
const configJSON = require("./components/visualizations/JSON/manifest.json");
96+
visComponentDefinitions.push({
97+
name: configJSON.name,
98+
mainType: configJSON.visConfig.mainType,
99+
mimeTypes: configJSON.visConfig.mimeTypes,
100+
component: React.createElement(registerComponent(configJSON)),
101+
});
102+
95103
export { visComponentDefinitions };

0 commit comments

Comments
 (0)