@@ -14,10 +14,12 @@ import {
1414 Tooltip ,
1515 Tree ,
1616 Typography ,
17- theme
17+ theme ,
18+ Upload
1819} from 'antd' ;
1920import type { InputRef } from 'antd' ;
2021import type { TreeDataNode , TreeProps } from 'antd' ;
22+ import type { UploadProps } from 'antd' ;
2123import {
2224 AppstoreAddOutlined as MechanismAddOutlined ,
2325 AppstoreOutlined as MechanismOutlined ,
@@ -348,7 +350,7 @@ const App: React.FC = () => {
348350 if ( errorMessage ) {
349351 setAlertErrorMessage ( 'Unable to load the list of modules: ' + errorMessage ) ;
350352 setAlertErrorVisible ( true ) ;
351- return
353+ return ;
352354 }
353355 if ( array != null ) {
354356 setModules ( array )
@@ -894,17 +896,65 @@ const App: React.FC = () => {
894896 setOpenPopconfirm ( true ) ;
895897 } ;
896898
897- const handleUploadClicked = ( ) => {
898- messageApi . open ( {
899- type : 'success' ,
900- content : 'Not implemented yet .' ,
901- } ) ;
899+ const uploadProps : UploadProps = {
900+ accept : commonStorage . UPLOAD_DOWNLOAD_FILE_EXTENSION ,
901+ beforeUpload : ( file ) => {
902+ const isBlocks = file . name . endsWith ( commonStorage . UPLOAD_DOWNLOAD_FILE_EXTENSION )
903+ if ( ! isBlocks ) {
904+ setAlertErrorMessage ( file . name + ' is not a blocks file' ) ;
905+ setAlertErrorVisible ( true ) ;
906+ return false ;
907+ }
908+ return isBlocks || Upload . LIST_IGNORE ;
909+ } ,
910+ onChange : ( info ) => {
911+ } ,
912+ customRequest : ( { file, onSuccess, onError } ) => {
913+ const reader = new FileReader ( ) ;
914+ reader . onload = ( event ) => {
915+ const dataUrl = event . target . result ;
916+ const uploadProjectName = commonStorage . makeUploadProjectName ( file . name , getProjectNames ( ) ) ;
917+ storage . uploadProject (
918+ uploadProjectName , dataUrl ,
919+ ( success : boolean , errorMessage : string ) => {
920+ if ( success ) {
921+ onSuccess ( 'Upload successful' ) ;
922+ afterListModulesSuccess . current = ( ) => {
923+ const uploadProjectPath = commonStorage . makeProjectPath ( uploadProjectName ) ;
924+ setCurrentModulePath ( uploadProjectPath ) ;
925+ } ;
926+ setTriggerListModules ( ! triggerListModules ) ;
927+ } else {
928+ onError ( errorMessage ) ;
929+ setAlertErrorMessage ( 'Unable to upload the project' ) ;
930+ setAlertErrorVisible ( true ) ;
931+ }
932+ } ) ;
933+ } ;
934+ reader . onerror = ( error ) => {
935+ onError ( error ) ;
936+ setAlertErrorMessage ( 'Unable to upload the project' ) ;
937+ setAlertErrorVisible ( true ) ;
938+ } ;
939+ reader . readAsDataURL ( file ) ;
940+ } ,
902941 } ;
903942
904943 const handleDownloadClicked = ( ) => {
905- messageApi . open ( {
906- type : 'success' ,
907- content : 'Not implemented yet .' ,
944+ checkIfBlocksWereModified ( ( ) => {
945+ storage . downloadProject (
946+ currentModule . projectName ,
947+ ( url : string | null , errorMessage : string ) => {
948+ if ( errorMessage ) {
949+ setAlertErrorMessage ( 'Unable to download the project: ' + errorMessage ) ;
950+ setAlertErrorVisible ( true ) ;
951+ return ;
952+ }
953+ const link = document . createElement ( 'a' ) ;
954+ link . href = url ;
955+ link . download = currentModule . projectName + commonStorage . UPLOAD_DOWNLOAD_FILE_EXTENSION ;
956+ link . click ( ) ;
957+ } ) ;
908958 } ) ;
909959 } ;
910960
@@ -1083,16 +1133,20 @@ const App: React.FC = () => {
10831133 >
10841134 </ Button >
10851135 </ Tooltip >
1086- < Tooltip title = "Upload" >
1087- < Button
1088- icon = { < UploadOutlined /> }
1089- size = "small"
1090- onClick = { handleUploadClicked }
1091- style = { { color : 'white' } }
1136+ < Tooltip title = "Upload Project" >
1137+ < Upload
1138+ { ...uploadProps }
1139+ showUploadList = { false }
10921140 >
1093- </ Button >
1141+ < Button
1142+ icon = { < UploadOutlined /> }
1143+ size = "small"
1144+ style = { { color : 'white' } }
1145+ >
1146+ </ Button >
1147+ </ Upload >
10941148 </ Tooltip >
1095- < Tooltip title = "Download" >
1149+ < Tooltip title = "Download Project " >
10961150 < Button
10971151 icon = { < DownloadOutlined /> }
10981152 size = "small"
0 commit comments