diff --git a/client-react/src/client/connectors/google_drive_v2/api.js b/client-react/src/client/connectors/google_drive_v2/api.js index e91b9ef0c..1b0b23b6d 100644 --- a/client-react/src/client/connectors/google_drive_v2/api.js +++ b/client-react/src/client/connectors/google_drive_v2/api.js @@ -84,7 +84,7 @@ function normalizeResource(resource) { modifiedDate: Date.parse(resource.modifiedDate), title: resource.title, type: resource.mimeType === 'application/vnd.google-apps.folder' ? 'dir' : 'file', - size: typeof resource.fileSize === 'undefined' ? resource.fileSize : parseInt(resource.fileSize), + size: typeof resource.fileSize === 'undefined' ? resource.fileSize : parseInt(resource.fileSize, 10), parents: resource.parents, capabilities: resource.capabilities, downloadUrl: resource.downloadUrl, @@ -188,7 +188,11 @@ async function downloadResources({ resources, apiOptions, trackers: { if (resources.length === 1) { const downloadParams = getDownloadParams(resources[0]); onStart({ name: `Downloading ${downloadParams.fileName}...`, quantity: 1 }); - const result = await downloadResource({ resource: resources[0], params: downloadParams, onProgress, i: 0, l: 1 }); + const result = await downloadResource({ resource: resources[0], params: downloadParams, onProgress, i: 0, l: 1 }). + catch(err => { + console.error('Error downloading resource: ', err); + onFail() + }); onSuccess(); return result; } @@ -207,6 +211,9 @@ async function downloadResources({ resources, apiOptions, trackers: { direct: false }, onProgress, i, l + }).catch(err => { + console.error('Error downloading archive: ', err); + onFail() }) ), onProgress @@ -251,7 +258,7 @@ async function uploadChunk({ sessionUrl, size, startByte, content }) { set('Content-Encoding', 'base64'). send(btoa(content.slice(startByte, endByte))). end((err, res) => { - if (err) { } // pass + if (err) { } // pass // TBD how to handle errors resolve(res); }); }); @@ -291,27 +298,50 @@ async function uploadFileToId(parentId, { onStart, onSuccess, onFail, onProgress } } -async function createFolder(apiOptions, parentId, folderName) { - return await window.gapi.client.drive.files.insert({ +async function createFolder(apiOptions, parentId, folderName, { onFail }) { + return window.gapi.client.drive.files.insert({ title: folderName, parents: [{ id: parentId }], mimeType: 'application/vnd.google-apps.folder' - }); + }).then( + res => res, + err => { + console.error(`Filemanager. createFolder(${id})`, err); + onFail() + } + ); } -async function renameResource(apiOptions, id, newName) { - return await window.gapi.client.drive.files.patch({ +async function renameResource(apiOptions, id, newName, { onFail }) { + return window.gapi.client.drive.files.patch({ fileId: id, title: newName - }); + }).then( + res => res, + err => { + console.error('Failed to rename resource: ', err); + onFail() + } + ) } function getResourceName(apiOptions, resource) { return resource.title; } -async function removeResources() { +async function removeResource(apiOptions, resource) { + return window.gapi.client.drive.files.delete({ + fileId: resource.id, + }); +} +async function removeResources(apiOptions, selectedResources, { onSuccess, onFail }) { + let success = await Promise.all(selectedResources.map(async (resource) => await removeResource(apiOptions, resource))). + catch((error) => { + console.error(`Filemanager. removeResources`, error); + onFail(); + }); + onSuccess(); } async function signIn() { diff --git a/client-react/src/client/connectors/google_drive_v2/capabilities/create-folder.js b/client-react/src/client/connectors/google_drive_v2/capabilities/create-folder.js index 6317b0bfc..183a4cf6a 100644 --- a/client-react/src/client/connectors/google_drive_v2/capabilities/create-folder.js +++ b/client-react/src/client/connectors/google_drive_v2/capabilities/create-folder.js @@ -1,7 +1,9 @@ +import React from 'react'; import api from '../api'; import sanitizeFilename from 'sanitize-filename'; import ContextMenuItem from '../../../components/ContextMenuItem'; import SetNameDialog from '../../../components/SetNameDialog'; +import onFailError from '../../utils/onFailError'; let icon = require('@opuscapita/svg-icons/lib/create_new_folder.svg'); let label = 'Create folder'; @@ -19,6 +21,14 @@ function handler(apiOptions, { getResourceLocation, getNotifications }) { + const onFail = ({ message } = {}) => onFailError({ + getNotifications, + label, + notificationId: 'createFolder', + updateNotifications, + message + }); + showDialog(( { + let resource = getResource(); + navigateToDir(resource.id, null, false); + }; + const onFail = ({ message } = {}) => onFailError({ + getNotifications, + label, + notificationId: 'delete', + updateNotifications, + message + }); + + let selectedResources = getSelectedResources(); + let dialogNameText = `Do you really want to remove\n`; + let dialogFilesText = selectedResources.length > 1 ? + `${selectedResources.length} files ?` : + `"${selectedResources[0].name}" ?`; + + return showDialog(( + { + hideDialog(); + api.removeResources(apiOptions, selectedResources, { onSuccess, onFail }); + }} + headerText={dialogNameText + dialogFilesText} + cancelButtonText={'Cancel'} + submitButtonText={'Confirm'} + /> + )); } export default (apiOptions, { @@ -37,9 +70,14 @@ export default (apiOptions, { label, title: 'Remove', shouldBeAvailable: (apiOptions) => { - // let selectedResources = getSelectedResources(); + let selectedResources = getSelectedResources(); + if (!selectedResources.length) { + return false; + } + // return selectedResources.every(resource => resource.capabilities.canDelete); - return false; + console.log(selectedResources) // returns ...capabilities: {canCopy: true, canEdit: true}, 'canDelete' missing + return selectedResources.every(resource => resource.capabilities.canEdit); // TBD is this correct? }, availableInContexts: ['row', 'toolbar'], handler: () => handler(apiOptions, { @@ -55,7 +93,21 @@ export default (apiOptions, { getNotifications }), contextMenuRenderer: (apiOptions) => ( - + handler(apiOptions, { + showDialog, + hideDialog, + navigateToDir, + updateNotifications, + getSelection, + getSelectedResources, + getResource, + getResourceChildren, + getResourceLocation, + getNotifications + })} + > {label} ) diff --git a/client-react/src/client/connectors/google_drive_v2/capabilities/download.js b/client-react/src/client/connectors/google_drive_v2/capabilities/download.js index ed17312ef..93ee84cf3 100644 --- a/client-react/src/client/connectors/google_drive_v2/capabilities/download.js +++ b/client-react/src/client/connectors/google_drive_v2/capabilities/download.js @@ -5,6 +5,7 @@ import ContextMenuItem from '../../../components/ContextMenuItem'; import NotificationProgressItem from '../../../components/NotificationProgressItem'; import notifUtils from '../../../components/Notifications/utils'; import { getIcon } from '../icons'; +import onFailError from '../../utils/onFailError'; const icon = require('@opuscapita/svg-icons/lib/file_download.svg'); const label = 'Download'; @@ -66,7 +67,14 @@ function handler(apiOptions, { updateNotifications(newNotifications); }; - const onFail = () => { }; + const onFail = ({ message } = {}) => onFailError({ + getNotifications, + label, + notificationId, + updateNotifications, + message + }); + const onProgress = (progress) => { const notifications = getNotifications(); const notification = notifUtils.getNotification(notifications, notificationId); @@ -93,7 +101,11 @@ function handler(apiOptions, { ...(mimeType === 'application/pdf' ? { target: '_blank' } : null) }) : promptToSaveBlob({ content: file, name: fileName }) - ).catch(err => console.log(err)) + ) + // .catch(err => { + // console.log('Failed to download resource: ', err); + // onFail() + // }) } export default (apiOptions, { diff --git a/client-react/src/client/connectors/google_drive_v2/capabilities/rename.js b/client-react/src/client/connectors/google_drive_v2/capabilities/rename.js index 03297969b..93233fc2d 100644 --- a/client-react/src/client/connectors/google_drive_v2/capabilities/rename.js +++ b/client-react/src/client/connectors/google_drive_v2/capabilities/rename.js @@ -1,7 +1,9 @@ +import React from 'react'; import api from '../api'; import sanitizeFilename from 'sanitize-filename'; import ContextMenuItem from '../../../components/ContextMenuItem'; import SetNameDialog from '../../../components/SetNameDialog'; +import onFailError from '../../utils/onFailError'; let icon = ``; let label = 'Rename'; @@ -19,6 +21,14 @@ function handler(apiOptions, { getResourceLocation, getNotifications }) { + const onFail = ({ message } = {}) => onFailError({ + getNotifications, + label, + notificationId: 'rename', + updateNotifications, + message + }); + showDialog(( {}; + const onFail = ({ message } = {}) => onFailError({ + getNotifications, + label, + notificationId, + updateNotifications, + message + }); + let onProgress = (progress) => { let notifications = getNotifications(); let notification = notifUtils.getNotification(notifications, notificationId); diff --git a/client-react/src/client/connectors/nodejs_v1/api.js b/client-react/src/client/connectors/nodejs_v1/api.js index 3e5c2c66e..16921c3be 100644 --- a/client-react/src/client/connectors/nodejs_v1/api.js +++ b/client-react/src/client/connectors/nodejs_v1/api.js @@ -56,7 +56,7 @@ async function getChildrenForId(options, { id, sortBy = 'name', sortDirection = let response = await request(method, route).catch((error) => { console.error(`Filemanager. getChildrenForId(${id})`, error); if (onFail) { - onFail({ message: 'Unable to read a directory.' }) // TODO doesn't intercept for some reason + onFail({ message: 'Unable to read a directory.' }) } }); diff --git a/client-react/src/client/connectors/nodejs_v1/capabilities/create-folder.js b/client-react/src/client/connectors/nodejs_v1/capabilities/create-folder.js index 0068ff679..d0af068d0 100644 --- a/client-react/src/client/connectors/nodejs_v1/capabilities/create-folder.js +++ b/client-react/src/client/connectors/nodejs_v1/capabilities/create-folder.js @@ -21,11 +21,12 @@ function handler(apiOptions, { getResourceLocation, getNotifications }) { - const onFail = _ => onFailError({ + const onFail = ({ message } = {}) => onFailError({ getNotifications, label, notificationId: 'createFolder', - updateNotifications + updateNotifications, + message }); showDialog(( diff --git a/client-react/src/client/connectors/nodejs_v1/capabilities/delete-resource.js b/client-react/src/client/connectors/nodejs_v1/capabilities/delete-resource.js index 92e7a1eab..7737e93c0 100644 --- a/client-react/src/client/connectors/nodejs_v1/capabilities/delete-resource.js +++ b/client-react/src/client/connectors/nodejs_v1/capabilities/delete-resource.js @@ -25,11 +25,12 @@ function handler(apiOptions, { navigateToDir(resource.id, null, false); }; - const onFail = _ => onFailError({ + const onFail = ({ message } = {}) => onFailError({ getNotifications, label, notificationId: 'delete', - updateNotifications + updateNotifications, + message }); let selectedResources = getSelectedResources(); diff --git a/client-react/src/client/connectors/nodejs_v1/capabilities/download.js b/client-react/src/client/connectors/nodejs_v1/capabilities/download.js index ff63cfa92..88f060aca 100644 --- a/client-react/src/client/connectors/nodejs_v1/capabilities/download.js +++ b/client-react/src/client/connectors/nodejs_v1/capabilities/download.js @@ -67,11 +67,12 @@ function handler(apiOptions, { updateNotifications(newNotifications); }; - const onFail = _ => onFailError({ + const onFail = ({ message } = {}) => onFailError({ getNotifications, label, notificationId, - updateNotifications + updateNotifications, + message }); const onProgress = (progress) => { diff --git a/client-react/src/client/connectors/nodejs_v1/capabilities/rename.js b/client-react/src/client/connectors/nodejs_v1/capabilities/rename.js index 7f71bea7e..38829bd14 100644 --- a/client-react/src/client/connectors/nodejs_v1/capabilities/rename.js +++ b/client-react/src/client/connectors/nodejs_v1/capabilities/rename.js @@ -21,7 +21,7 @@ function handler(apiOptions, { getResourceLocation, getNotifications }) { - const onFail = ({ message }) => onFailError({ + const onFail = ({ message } = {}) => onFailError({ getNotifications, label, notificationId: 'rename', diff --git a/client-react/src/client/connectors/nodejs_v1/capabilities/sort.js b/client-react/src/client/connectors/nodejs_v1/capabilities/sort.js index 5461c6d56..020cc7881 100644 --- a/client-react/src/client/connectors/nodejs_v1/capabilities/sort.js +++ b/client-react/src/client/connectors/nodejs_v1/capabilities/sort.js @@ -16,7 +16,7 @@ export default (apiOptions, { }) => ({ id: 'sort', handler: async ({ sortBy, sortDirection }) => { - const onFail = ({ message }) => onFailError({ + const onFail = ({ message } = {}) => onFailError({ getNotifications, label, notificationId: 'rename', diff --git a/client-react/src/client/connectors/nodejs_v1/capabilities/upload.js b/client-react/src/client/connectors/nodejs_v1/capabilities/upload.js index ec8add96b..590a9332c 100644 --- a/client-react/src/client/connectors/nodejs_v1/capabilities/upload.js +++ b/client-react/src/client/connectors/nodejs_v1/capabilities/upload.js @@ -72,11 +72,12 @@ function handler(apiOptions, { } }; - const onFail = _ => onFailError({ + const onFail = ({ message } = {}) => onFailError({ getNotifications, label, notificationId, - updateNotifications + updateNotifications, + message }); let onProgress = (progress) => {