@@ -24,9 +24,15 @@ type jsonProps = {
2424
2525export default function JSONVisualizer ( props : jsonProps ) {
2626 const { fileId, visualizationId, publicView } = props ;
27+
28+ // State to store the original content of the file and the displayed JSON content that can be edited
29+ const [ originalContent , setOriginalContent ] = useState < string | undefined > ( ) ;
2730 const [ jsonContent , setJsonContent ] = useState < string | undefined > ( ) ;
31+
32+ // Utility states to help with saving the file, displaying loading spinner and validating JSON
2833 const [ fileName , setFileName ] = useState < string | undefined > ( ) ;
2934 const [ loading , setLoading ] = useState < boolean > ( false ) ;
35+ const [ validJson , setValidJson ] = useState < boolean > ( true ) ;
3036
3137 // use useSelector to get fileSummary to get filename.
3238 const fileData = useSelector ( ( state : RootState ) => state . file ) ;
@@ -42,10 +48,6 @@ export default function JSONVisualizer(props: jsonProps) {
4248 }
4349 } , [ fileData . fileSummary ] ) ;
4450
45- useEffect ( ( ) => {
46- console . log ( "fileData" , fileData . fileSummary . content_type ) ;
47- } , [ fileName ] ) ;
48-
4951 useEffect ( ( ) => {
5052 const fetchData = async ( ) => {
5153 try {
@@ -62,6 +64,7 @@ export default function JSONVisualizer(props: jsonProps) {
6264
6365 const file = new File ( [ blob ] , fileName ) ;
6466 const text = await readTextFromFile ( file ) ;
67+ setOriginalContent ( text ) ;
6568 setJsonContent ( text ) ;
6669 } catch ( error ) {
6770 console . error ( "Error fetching data:" , error ) ;
@@ -70,14 +73,27 @@ export default function JSONVisualizer(props: jsonProps) {
7073 fetchData ( ) ;
7174 } , [ visualizationId , fileId , publicView ] ) ;
7275
76+ const validateJson = ( jsonString : string ) => {
77+ try {
78+ JSON . parse ( jsonString ) ;
79+ return true ;
80+ } catch ( error ) {
81+ return false ;
82+ }
83+ }
84+
85+ const handleChange = ( value : string ) => {
86+ setJsonContent ( value ) ;
87+ setValidJson ( validateJson ( value ) ) ;
88+ }
89+
7390 const handleSave = async ( ) => {
7491 try {
7592 if (
7693 jsonContent !== undefined &&
7794 fileName &&
7895 fileData . fileSummary ?. content_type
7996 ) {
80- // Parse the jsonContent to ensure it's valid JSON
8197 const textBlob = new Blob ( [ jsonContent ] , { type : "text/plain" } ) ;
8298 const file = new File ( [ textBlob ] , fileName , {
8399 type : fileData . fileSummary . content_type . content_type ,
@@ -86,12 +102,19 @@ export default function JSONVisualizer(props: jsonProps) {
86102 setLoading ( true ) ;
87103 await updateFile ( file , fileId ) ;
88104 setLoading ( false ) ;
105+
106+ // Refreshing the page to reflect the changes. TODO: Find a better way to update the version
107+ window . location . reload ( ) ;
89108 }
90109 } catch ( error ) {
91110 console . error ( "Error updating file:" , error ) ;
92111 }
93112 } ;
94113
114+ const disableSaveButton = ( ) => {
115+ return originalContent === jsonContent || ! validJson ;
116+ }
117+
95118 return (
96119 < Card >
97120 < CardContent >
@@ -101,13 +124,13 @@ export default function JSONVisualizer(props: jsonProps) {
101124 < CodeMirror
102125 value = { jsonContent }
103126 extensions = { [ json ( ) ] }
104- onChange = { ( value ) => setJsonContent ( value ) }
127+ onChange = { ( value ) => handleChange ( value ) }
105128 theme = "dark"
106129 />
107130 ) }
108131 </ CardContent >
109132 < CardActions >
110- < Button variant = "contained" color = "primary" onClick = { handleSave } >
133+ < Button variant = "contained" color = "primary" onClick = { handleSave } disabled = { disableSaveButton ( ) } >
111134 Save Changes
112135 </ Button >
113136 </ CardActions >
0 commit comments