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

Skip pop-up blocker #163

Merged
merged 2 commits into from
Jan 19, 2024
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
20 changes: 16 additions & 4 deletions frontend/src/components/object/DownloadObjectButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import { ref } from 'vue';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';

import { Button, Dialog } from '@/lib/primevue';
import { Button, Dialog, useToast } from '@/lib/primevue';
import { useObjectStore } from '@/store';
import { ButtonMode } from '@/utils/enums';

const toast = useToast();

// Props
type Props = {
disabled?: boolean;
Expand All @@ -28,9 +30,19 @@ const displayNoFileDlg = ref(false);
const download = () => {
if (props.ids.length) {
// For now we are looping the supplied IDs (if multiple selected) until there is a batching feature
props.ids.forEach((i) => {
objectStore.downloadObject(i, props.versionId);
});
let alerted = false;
for (let i of props.ids) {
// get S3 url
objectStore.getObjectUrl(i, props.versionId).then((url) => {
// open new tab, window or save dialog
const newTab = window.open(url, '_blank');
// if browser blocked new tab
if (newTab === null && !alerted) {
toast.warn('Downloading Objects', 'Your browser blocked multiple new tabs from opening.', { life: 0 });
alerted = true;
}
});
}
} else {
displayNoFileDlg.value = true;
}
Expand Down
19 changes: 7 additions & 12 deletions frontend/src/services/objectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,15 @@ export default {
* Get an object
* @param {string} objectId The id for the object to get
* @param {string} versionId An optional versionId
* @returns {Promise} An axios response
*/
getObject(objectId: string, versionId?: string) {
// Running in 'url' download mode only, could add options for other modes if needed
return comsAxios()
.get(`${PATH}/${objectId}`, {
params: {
versionId: versionId,
download: 'url'
}
})
.then((response) => {
const url = response.data;
window.open(url, '_blank');
});
return comsAxios().get(`${PATH}/${objectId}`, {
params: {
versionId: versionId,
download: 'url'
}
});
},

/**
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/store/objectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ export const useObjectStore = defineStore('object', () => {
}
}

async function downloadObject(objectId: string, versionId?: string) {
async function getObjectUrl(objectId: string, versionId?: string) {
try {
appStore.beginIndeterminateLoading();
await objectService.getObject(objectId, versionId);
return objectService.getObject(objectId, versionId).then((response) => response.data);
} catch (error: any) {
toast.error('Downloading object', error);
} finally {
Expand Down Expand Up @@ -270,7 +270,7 @@ export const useObjectStore = defineStore('object', () => {
// Actions
createObject,
deleteObject,
downloadObject,
getObjectUrl,
fetchObjects,
findObjectById,
headObject,
Expand Down
8 changes: 4 additions & 4 deletions frontend/tests/unit/store/objectStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ describe('Object Store', () => {
});
});

describe('downloadObject', () => {
describe('getObjectUrl', () => {
it('gets the most recent object', async () => {
getObjectSpy.mockReturnValue({} as any);

await objectStore.downloadObject(obj.id);
await objectStore.getObjectUrl(obj.id);

expect(beginIndeterminateLoadingSpy).toHaveBeenCalledTimes(1);
expect(getObjectSpy).toHaveBeenCalledTimes(1);
Expand All @@ -148,7 +148,7 @@ describe('Object Store', () => {
it('gets the object by version', async () => {
getObjectSpy.mockReturnValue({} as any);

await objectStore.downloadObject(obj.id, '1');
await objectStore.getObjectUrl(obj.id, '1');

expect(beginIndeterminateLoadingSpy).toHaveBeenCalledTimes(1);
expect(getObjectSpy).toHaveBeenCalledTimes(1);
Expand All @@ -161,7 +161,7 @@ describe('Object Store', () => {
throw new Error();
});

await objectStore.downloadObject(obj.id);
await objectStore.getObjectUrl(obj.id);

expect(beginIndeterminateLoadingSpy).toHaveBeenCalledTimes(1);
expect(getObjectSpy).toHaveBeenCalledTimes(1);
Expand Down