Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
Make file uploader statuses independent
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaschoal committed May 10, 2019
1 parent 564539c commit 6527e8b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/ng-chat/ng-chat.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@

<!-- File Upload -->
<ng-container *ngIf="fileUploadAdapter">
<a *ngIf="!isUploadingFile" class="btn-add-file" (click)="triggerNativeFileUpload(getUniqueFileUploadInstanceId(window))">
<a *ngIf="!isUploadingFile(window)" class="btn-add-file" (click)="triggerNativeFileUpload(window)">
<i class="upload-icon"></i>
</a>
<input
Expand All @@ -117,7 +117,7 @@
style="display: none;"
[attr.id]="getUniqueFileUploadInstanceId(window)"
(change)="onFileChosen(window)" />
<div *ngIf="isUploadingFile" class="loader"></div>
<div *ngIf="isUploadingFile(window)" class="loader"></div>
</ng-container>
</div>
</ng-container>
Expand Down
36 changes: 28 additions & 8 deletions src/ng-chat/ng-chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class NgChat implements OnInit, IChatController {
public unsupportedViewport: boolean = false;

// File upload state
public isUploadingFile = false;
public fileUploadersInUse: string[] = []; // Id bucket of uploaders in use
public fileUploadAdapter: IFileUploadAdapter;

windows: Window[] = [];
Expand Down Expand Up @@ -903,12 +903,32 @@ export class NgChat implements OnInit, IChatController {
}

// Triggers native file upload for file selection from the user
triggerNativeFileUpload(fileUploadInstanceId: string): void
triggerNativeFileUpload(window: Window): void
{
const uploadElementRef = this.nativeFileInputs.filter(x => x.nativeElement.id === fileUploadInstanceId)[0];
if (window)
{
const fileUploadInstanceId = this.getUniqueFileUploadInstanceId(window);
const uploadElementRef = this.nativeFileInputs.filter(x => x.nativeElement.id === fileUploadInstanceId)[0];

if (uploadElementRef)
uploadElementRef.nativeElement.click();
if (uploadElementRef)
uploadElementRef.nativeElement.click();
}
}

private clearInUseFileUploader(fileUploadInstanceId: string): void
{
const uploaderInstanceIdIndex = this.fileUploadersInUse.indexOf(fileUploadInstanceId);

if (uploaderInstanceIdIndex > -1) {
this.fileUploadersInUse.splice(uploaderInstanceIdIndex, 1);
}
}

isUploadingFile(window: Window): boolean
{
const fileUploadInstanceId = this.getUniqueFileUploadInstanceId(window);

return this.fileUploadersInUse.indexOf(fileUploadInstanceId) > -1;
}

// Handles file selection and uploads the selected file using the file upload adapter
Expand All @@ -920,11 +940,11 @@ export class NgChat implements OnInit, IChatController {
{
const file: File = uploadElementRef.nativeElement.files[0];

this.isUploadingFile = true;
this.fileUploadersInUse.push(fileUploadInstanceId);

this.fileUploadAdapter.uploadFile(file, window.participant.id)
.subscribe(fileMessage => {
this.isUploadingFile = false;
this.clearInUseFileUploader(fileUploadInstanceId);

fileMessage.fromId = this.userId;

Expand All @@ -938,7 +958,7 @@ export class NgChat implements OnInit, IChatController {
// Resets the file upload element
uploadElementRef.nativeElement.value = '';
}, (error) => {
this.isUploadingFile = false;
this.clearInUseFileUploader(fileUploadInstanceId);

// Resets the file upload element
uploadElementRef.nativeElement.value = '';
Expand Down
15 changes: 8 additions & 7 deletions src/spec/ng-chat.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1548,20 +1548,21 @@ describe('NgChat', () => {
it('Should filter by file instance id and upload file when a file upload "onFileChosen" event is triggered', () => {
const mockedFileMessageServerResponse = new FileMessage();

const chattingTo = new User();
chattingTo.id = 88;

const chatWindow = new Window(chattingTo, false, false);

spyOn(MockableFileUploadAdapter.prototype, 'uploadFile').and.callFake(() => {
// At this stage the 'isUploadingFile' should be true
expect(subject.isUploadingFile).toBeTruthy();
expect(subject.isUploadingFile(chatWindow)).toBeTruthy();

return of(mockedFileMessageServerResponse);
});

spyOn(MockableAdapter.prototype, 'sendMessage');
const scrollSpy = spyOn(subject, 'scrollChatWindow');

const chattingTo = new User();
chattingTo.id = 88;

const chatWindow = new Window(chattingTo, false, false);

const fakeFile = new File([''], 'filename', { type: 'text/html' });

const fakeFileElement = {
Expand Down Expand Up @@ -1597,7 +1598,7 @@ describe('NgChat', () => {
expect(scrollSpy.calls.mostRecent().args[1]).toBe(ScrollDirection.Bottom);
expect(fakeFileElement.nativeElement.value).toBe('');
expect(anotherFakeFileElement.nativeElement.value).toBe('test');
expect(subject.isUploadingFile).toBeFalsy();
expect(subject.isUploadingFile(chatWindow)).toBeFalsy();
});

it('Assert message type must default to text when no message type is defined in a message instance', () => {
Expand Down

0 comments on commit 6527e8b

Please sign in to comment.