Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import workpad #104722

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import { i18n } from '@kbn/i18n';
import { CANVAS, JSON as JSONString } from '../../../../i18n/constants';
import { useNotifyService } from '../../../services';
import { getId } from '../../../lib/get_id';
import { useCreateWorkpad } from './use_create_workpad';
import type { CanvasWorkpad } from '../../../../types';

export const useImportWorkpad = () => {
const notifyService = useNotifyService();
const createWorkpad = useCreateWorkpad();

return useCallback(
(file?: File, onComplete: (workpad?: CanvasWorkpad) => void = () => {}) => {
Expand All @@ -37,7 +39,7 @@ export const useImportWorkpad = () => {
const reader = new FileReader();

// handle reading the uploaded file
reader.onload = () => {
reader.onload = async () => {
try {
const workpad = JSON.parse(reader.result as string); // Type-casting because we catch below.
workpad.id = getId('workpad');
Expand All @@ -48,6 +50,7 @@ export const useImportWorkpad = () => {
throw new Error(errors.getMissingPropertiesErrorMessage());
}

await createWorkpad(workpad);
onComplete(workpad);
} catch (e) {
notifyService.error(e, {
Expand All @@ -62,7 +65,7 @@ export const useImportWorkpad = () => {
// read the uploaded file
reader.readAsText(file);
},
[notifyService]
[notifyService, createWorkpad]
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import Dropzone from 'react-dropzone';

import { useNotifyService } from '../../../services';
import { ErrorStrings } from '../../../../i18n';
import { useImportWorkpad, useCreateWorkpad } from '../hooks';
import { CanvasWorkpad } from '../../../../types';
import { useImportWorkpad } from '../hooks';

import { UploadDropzone as Component } from './upload_dropzone.component';

Expand All @@ -21,18 +20,8 @@ const { WorkpadDropzone: errors } = ErrorStrings;
export const UploadDropzone: FC = ({ children }) => {
const notify = useNotifyService();
const uploadWorkpad = useImportWorkpad();
const createWorkpad = useCreateWorkpad();
const [isDisabled, setIsDisabled] = useState(false);

const onComplete = async (workpad?: CanvasWorkpad) => {
if (!workpad) {
setIsDisabled(false);
return;
}

await createWorkpad(workpad);
};

const onDrop = (files: FileList) => {
if (!files) {
return;
Expand All @@ -44,7 +33,7 @@ export const UploadDropzone: FC = ({ children }) => {
}

setIsDisabled(true);
uploadWorkpad(files[0], onComplete);
uploadWorkpad(files[0], () => setIsDisabled(false));
};

return (
Expand Down