Skip to content

Commit

Permalink
Display error message when download fails
Browse files Browse the repository at this point in the history
Fix: disappearing file on edit save
  • Loading branch information
goemen committed Sep 23, 2024
1 parent 8620334 commit bf61ef4
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ const handleDeleteLink = () => {
};
const handleDeleteFile = () => {
attachment.value = undefined;
fileDisplayName.value = undefined;
fileDisplayOnly.value = false;
};
Expand Down Expand Up @@ -841,6 +842,7 @@ const handleSave = handleSubmit(async (values) => {
linkUrl: isEmpty(values.linkUrl) ? undefined : values.linkUrl,
status: status.value,
attachment: attachment.value,
attachmentId: !values.attachment && !values.fileDisplayName ? undefined : values.attachmentId,
});
});
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { sanitizeUrl } from '@braintree/sanitize-url';
import ApiService from '../../services/apiService';
import { saveAs } from 'file-saver';
import { NotificationService } from '../../services/notificationService';
import { FILE_DOWNLOAD_ERROR } from '../../constants';
defineProps<{
announcement: Announcement;
Expand All @@ -67,11 +68,7 @@ async function downloadAnnouncementResource(
);
} catch (error) {
console.error(error);
NotificationService.pushNotificationError(
'There is a problem with this link/file, please try again later or contact the helpdesk.',
'',
30000,
);
NotificationService.pushNotificationError(FILE_DOWNLOAD_ERROR, '', 30000);
}
} else if (announcementResource.announcement_resource_file) {
//When a resource with type ATTACHMENT hasn't yet been uploaded to the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="root">
<a @click="ApiService.downloadFile(id)">
<a :aria-label="name" @click="downloadFile(id)">
{{ name }}
</a>
<v-btn
Expand All @@ -21,6 +21,8 @@
<script setup lang="ts">
import { defineProps } from 'vue';
import ApiService from '../../services/apiService';
import { NotificationService } from '../../services/notificationService';
import { FILE_DOWNLOAD_ERROR } from '../../constants';
interface AttachmentResourceProps {
id: string;
Expand All @@ -29,6 +31,15 @@ interface AttachmentResourceProps {
const { id, name } = defineProps<AttachmentResourceProps>();
const emits = defineEmits(['onEdit', 'onDelete']);
const downloadFile = async (id: string) => {
try {
await ApiService.downloadFile(id);
} catch (error) {
console.error(error);
NotificationService.pushNotificationError(FILE_DOWNLOAD_ERROR, '', 30000);
}
};
</script>
<style scoped lang="scss">
.root {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { fireEvent, render } from '@testing-library/vue';
import AttachmentResource from '../AttachmentResource.vue';
import { describe, it, expect, vi } from 'vitest';
import { FILE_DOWNLOAD_ERROR } from '../../../constants';

const mockDownloadFile = vi.fn();

vi.mock('../../../services/apiService', () => ({
default: {
downloadFile: (...args) => mockDownloadFile(...args),
},
}));

const pushNotificationErrorMock = vi.fn();
vi.mock('../../../services/notificationService', () => ({
NotificationService: {
pushNotificationError: (...args) => pushNotificationErrorMock(...args),
},
}));

describe('AttachmentResource', () => {
it('should download file', async () => {
const attachment = {
id: 1,
name: 'Test file',
};
const { getByLabelText } = await render(AttachmentResource, {
props: { ...attachment },
});
const link = await getByLabelText('Test file');
expect(link).toBeInTheDocument();
await fireEvent.click(link);
await expect(mockDownloadFile).toHaveBeenCalledWith(1);
});

it('should display error message when download fails', async () => {
mockDownloadFile.mockRejectedValue(new Error('Download failed'));
const attachment = {
id: 1,
name: 'Test file',
};
const { getByLabelText } = await render(AttachmentResource, {
props: { ...attachment },
});
const link = await getByLabelText('Test file');
expect(link).toBeInTheDocument();
await fireEvent.click(link);
await expect(pushNotificationErrorMock).toHaveBeenCalledWith(FILE_DOWNLOAD_ERROR, '', 30000);
});
});
3 changes: 3 additions & 0 deletions admin-frontend/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ export const RoleOptions = [
{ label: RoleLabels['PTRT-ADMIN'], value: 'PTRT-ADMIN' },
{ label: RoleLabels['PTRT-USER'], value: 'PTRT-USER' },
];

export const FILE_DOWNLOAD_ERROR =
'There is a problem with this link/file, please try again later or contact the helpdesk.';
10 changes: 8 additions & 2 deletions admin-frontend/src/services/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,15 @@ const buildFormData = (data: AnnouncementFormValue) => {
}

if (data.attachment) {
formData.append('fileDisplayName', data.fileDisplayName!);
formData.append('attachmentId', data.attachmentId!);
formData.append('file', data.attachment);
}

if (data.fileDisplayName) {
formData.append('fileDisplayName', data.fileDisplayName);
}

if (data.attachmentId) {
formData.append('attachmentId', data.attachmentId);
}
return formData;
};
3 changes: 1 addition & 2 deletions backend/src/v1/routes/announcement-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ router.put(
// Create announcement
const announcement = await updateAnnouncement(
req.params.id,
/* istanbul ignore next */
file ? data : omit(data, 'attachmentId'),
data,
user.admin_user_id,
);
return res.json(announcement);
Expand Down

0 comments on commit bf61ef4

Please sign in to comment.