Skip to content

Commit 057465b

Browse files
committed
Fixed typescript errors.
1 parent 8114499 commit 057465b

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

src/reactComponents/Menu.tsx

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
import * as Antd from 'antd';
2222
import * as React from 'react';
23+
import { RcFile, UploadRequestOption } from 'rc-upload/lib/interface';
2324
import * as commonStorage from '../storage/common_storage';
2425
import * as createPythonFiles from '../storage/create_python_files';
2526
import * as I18Next from 'react-i18next';
@@ -38,6 +39,8 @@ import {
3839
BgColorsOutlined,
3940
GlobalOutlined,
4041
CheckOutlined,
42+
DownloadOutlined,
43+
UploadOutlined,
4144
} from '@ant-design/icons';
4245
import FileManageModal from './FileManageModal';
4346
import ProjectManageModal from './ProjectManageModal';
@@ -180,6 +183,7 @@ export function Component(props: MenuProps): React.JSX.Element {
180183
const [noProjects, setNoProjects] = React.useState<boolean>(false);
181184
const [aboutDialogVisible, setAboutDialogVisible] = React.useState<boolean>(false);
182185
const [themeModalOpen, setThemeModalOpen] = React.useState<boolean>(false);
186+
const [showUploadAndDownload, _setShowUploadAndDownload] = React.useState(false);
183187

184188
const handleThemeChange = (newTheme: string) => {
185189
props.setTheme(newTheme);
@@ -322,7 +326,7 @@ export function Component(props: MenuProps): React.JSX.Element {
322326
}
323327
};
324328

325-
// TODO: Add menu or button or something for the download action.
329+
// TODO: Add UI for the download action.
326330
/** Handles the download action to generate and download json files. */
327331
const handleDownload = async (): Promise<void> => {
328332
if (!props.project || !props.storage) {
@@ -349,11 +353,11 @@ export function Component(props: MenuProps): React.JSX.Element {
349353
}
350354
}
351355

352-
// TODO: Add menu or button or something for the upload action.
356+
// TODO: Add UI for the upload action.
353357
/** Handles the upload action to upload a previously downloaded project. */
354-
const handleUpload = (): Antd.UploadProps => {
358+
const handleUpload = (): Antd.UploadProps | null => {
355359
if (!props.storage) {
356-
return;
360+
return null;
357361
}
358362

359363
const uploadProps: Antd.UploadProps = {
@@ -363,46 +367,35 @@ export function Component(props: MenuProps): React.JSX.Element {
363367
if (!isBlocks) {
364368
// TODO: i18n
365369
props.setAlertErrorMessage(file.name + ' is not a blocks file');
366-
setAlertErrorVisible(true);
367370
return false;
368371
}
369372
return isBlocks || Antd.Upload.LIST_IGNORE;
370373
},
371-
onChange: (info) => {
374+
onChange: (_info) => {
372375
},
373-
customRequest: ({ file, onSuccess, onError }) => {
376+
customRequest: (options: UploadRequestOption) => {
374377
const reader = new FileReader();
375378
reader.onload = (event) => {
376-
const dataUrl = event.target.result;
379+
if (!event.target) {
380+
return;
381+
}
382+
const dataUrl = event.target.result as string;
377383
const existingProjectNames: string[] = [];
378384
projects.forEach(project => {
379385
existingProjectNames.push(project.projectName);
380386
});
387+
const file = options.file as RcFile;
381388
const uploadProjectName = commonStorage.makeUploadProjectName(file.name, existingProjectNames);
382389
if (props.storage) {
383-
props.storage.uploadProject(
384-
uploadProjectName, dataUrl,
385-
(success: boolean, errorMessage: string) => {
386-
if (success) {
387-
// TODO: i18n
388-
onSuccess('Upload successful');
389-
// TODO: Select the project that was just uploaded.
390-
} else {
391-
onError(errorMessage);
392-
// TODO: i18n
393-
props.setAlertErrorMessage('Unable to upload the project');
394-
}
395-
}
396-
);
390+
props.storage.uploadProject(uploadProjectName, dataUrl);
397391
}
398392
};
399-
reader.onerror = (error) => {
400-
onError(error);
393+
reader.onerror = (_error) => {
394+
console.log('Error reading file: ' + reader.error);
401395
// TODO: i18n
402396
props.setAlertErrorMessage(t('UPLOAD_FAILED') || 'Failed to upload project');
403-
setAlertErrorVisible(true);
404397
};
405-
reader.readAsDataURL(file);
398+
reader.readAsDataURL(options.file as Blob);
406399
},
407400
};
408401
return uploadProps;
@@ -468,6 +461,30 @@ export function Component(props: MenuProps): React.JSX.Element {
468461
items={menuItems}
469462
onClick={handleClick}
470463
/>
464+
{showUploadAndDownload ? (
465+
<div>
466+
<Antd.Upload
467+
{...handleUpload()}
468+
showUploadList={false}
469+
>
470+
<Antd.Button
471+
icon={<UploadOutlined />}
472+
size="small"
473+
style={{ color: 'white' }}
474+
/>
475+
</Antd.Upload>
476+
<Antd.Button
477+
icon={<DownloadOutlined />}
478+
size="small"
479+
disabled={!props.project}
480+
onClick={handleDownload}
481+
style={{ color: 'white' }}
482+
/>
483+
</div>
484+
) : (
485+
<div>
486+
</div>
487+
)}
471488
<AboutDialog
472489
visible={aboutDialogVisible}
473490
onClose={() => setAboutDialogVisible(false)}

0 commit comments

Comments
 (0)