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

feat(file-upload): show info toast when diff file type is uploaded #2484

Merged
merged 3 commits into from
Oct 25, 2023
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 @@ -92,9 +92,9 @@ describe('File Upload Component', () => {
});
const filesAddedSpy = jest.spyOn(spectator.component.filesAdded, 'emit');
spectator.triggerEventHandler('input', 'change', { target: { files: fileList } });
expect(filesAddedSpy).not.toHaveBeenCalled();
expect(spectator.inject(NotificationService).createFailureToast).toHaveBeenCalledWith(
`File type should be any of ${config.supportedFileTypes.join(', ')}`
expect(filesAddedSpy).toHaveBeenCalled();
expect(spectator.inject(NotificationService).createInfoToast).toHaveBeenCalledWith(
`Make sure to choose file of type ${config.supportedFileTypes.join(', ')}`
);
});

Expand Down
22 changes: 14 additions & 8 deletions projects/components/src/file-upload/file-upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,22 @@ export class FileUploadComponent {
return false;
}

if (!this.areFileTypesValid(fileList)) {
this.showFileTypeErrorToast();
if (this.areFilesEmpty(fileList)) {
this.showEmptyFilesErrorToast();

return false;
}

if (this.areFilesEmpty(fileList)) {
this.showEmptyFilesErrorToast();
/**
* It's not advised to rely on mime based validation as there is limitations.
* So we use an info toast rather than stopping the flow.
*
* @see - https://developer.mozilla.org/en-US/docs/Web/API/File/type#result
*/
if (!this.areFileTypesValid(fileList)) {
this.showFileTypeInfoToast();

return false;
return true;
}

return true;
Expand Down Expand Up @@ -177,9 +183,9 @@ export class FileUploadComponent {
this.notificationService.createFailureToast(`File should not be empty`);
}

private showFileTypeErrorToast(): void {
this.notificationService.createFailureToast(
`File type should be any of ${this.config.supportedFileTypes.join(', ')}`
private showFileTypeInfoToast(): void {
this.notificationService.createInfoToast(
`Make sure to choose file of type ${this.config.supportedFileTypes.join(', ')}`
);
}

Expand Down