99import CodeMirror from "@uiw/react-codemirror" ; // CodeMirror editor
1010import { json } from "@codemirror/lang-json" ; // JSON language support for CodeMirror
1111import { downloadVisData , fileDownloaded } from "../../../utils/visualization" ;
12- import { updateFile as updateFileAction } from "../../../actions/file" ;
12+ import { fetchFileVersions , updateFile as updateFileAction } from "../../../actions/file" ;
1313import { readTextFromFile } from "../../../utils/common" ;
1414import { downloadPublicVisData } from "../../../actions/public_visualization" ;
1515import { filePublicDownloaded } from "../../../actions/public_file" ;
@@ -23,32 +23,41 @@ type jsonProps = {
2323} ;
2424
2525export 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