-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OMKR-1 | Added file upload fetaure and celebration card updated
- Loading branch information
1 parent
20ee419
commit d8c8f89
Showing
18 changed files
with
365 additions
and
324 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
8 changes: 8 additions & 0 deletions
8
src/app/components/general/dialog/confirm-dialog/confirm-dialog.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h1 mat-dialog-title>Confirm Upload</h1> | ||
<div mat-dialog-content> | ||
<p>Are you sure you want to upload this file?</p> | ||
</div> | ||
<div mat-dialog-actions> | ||
<button mat-button [mat-dialog-close]="false">Cancel</button> | ||
<button mat-button color="primary" [mat-dialog-close]="true">Upload</button> | ||
</div> |
21 changes: 21 additions & 0 deletions
21
src/app/components/general/dialog/confirm-dialog/confirm-dialog.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { ConfirmDialogComponent } from './confirm-dialog.component'; | ||
|
||
describe('ConfirmDialogComponent', () => { | ||
let component: ConfirmDialogComponent; | ||
let fixture: ComponentFixture<ConfirmDialogComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ConfirmDialogComponent] | ||
}); | ||
fixture = TestBed.createComponent(ConfirmDialogComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
src/app/components/general/dialog/confirm-dialog/confirm-dialog.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Component } from '@angular/core'; | ||
import { MatDialogRef } from '@angular/material/dialog'; | ||
|
||
@Component({ | ||
selector: 'app-confirm-dialog', | ||
templateUrl: './confirm-dialog.component.html', | ||
styleUrls: ['./confirm-dialog.component.css'] | ||
}) | ||
export class ConfirmDialogComponent { | ||
constructor(public dialogRef: MatDialogRef<ConfirmDialogComponent>) { } | ||
} |
Empty file.
15 changes: 15 additions & 0 deletions
15
src/app/components/general/file/file-upload/file-upload.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="file-upload-container"> | ||
<mat-form-field class="example-full-width" appearance="fill"> | ||
<mat-label>File name</mat-label> | ||
<input matInput placeholder="Enter file name" [(ngModel)]="fileName"> | ||
</mat-form-field> | ||
|
||
<div class="file-input-wrapper"> | ||
<input type="file" #fileInput (change)="onFileSelected($event)" hidden> | ||
<button mat-raised-button (click)="fileInput.click()">Choose file</button> | ||
<span class="file-name">{{ selectedFile?.name || 'No file selected' }}</span> | ||
</div> | ||
</div> | ||
<button mat-raised-button color="primary" (click)="onUpload()">Upload</button> | ||
<br> | ||
<mat-progress-bar *ngIf="uploadInProgress" mode="determinate" [value]="uploadProgress"></mat-progress-bar> |
21 changes: 21 additions & 0 deletions
21
src/app/components/general/file/file-upload/file-upload.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { FileUploadComponent } from './file-upload.component'; | ||
|
||
describe('FileUploadComponent', () => { | ||
let component: FileUploadComponent; | ||
let fixture: ComponentFixture<FileUploadComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [FileUploadComponent] | ||
}); | ||
fixture = TestBed.createComponent(FileUploadComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
94 changes: 94 additions & 0 deletions
94
src/app/components/general/file/file-upload/file-upload.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Component } from '@angular/core'; | ||
import { MatDialog } from '@angular/material/dialog'; | ||
import { ConfirmDialogComponent } from '../../dialog/confirm-dialog/confirm-dialog.component'; | ||
import { FileService } from 'src/app/services/general/file/file.service'; | ||
import { MatSnackBar } from '@angular/material/snack-bar'; | ||
import { HttpEventType } from '@angular/common/http'; | ||
|
||
@Component({ | ||
selector: 'app-file-upload', | ||
templateUrl: './file-upload.component.html', | ||
styleUrls: ['./file-upload.component.css'] | ||
}) | ||
export class FileUploadComponent { | ||
_fileName = ''; | ||
selectedFile: File | null = null; | ||
uploadProgress = 0; | ||
uploadInProgress = false; | ||
|
||
constructor(public dialog: MatDialog, private fileService: FileService, | ||
private snackBar: MatSnackBar) { | ||
|
||
} | ||
onUpload() { | ||
if (this.selectedFile) { | ||
const dialogRef = this.dialog.open(ConfirmDialogComponent); | ||
|
||
dialogRef.afterClosed().subscribe(result => { | ||
if (result) { | ||
console.log('Confirmed upload:', this.selectedFile); | ||
|
||
// Set upload in progress and reset progress value | ||
this.uploadInProgress = true; | ||
this.uploadProgress = 4; | ||
|
||
if (this.selectedFile) { | ||
this.fileService.uploadFile(this.selectedFile, this._fileName).subscribe(event => { | ||
|
||
if (event.type === HttpEventType.UploadProgress) { | ||
// Calculate and update the upload progress | ||
this.uploadProgress = event.total ? (event.loaded / event.total) * 100 : 0; | ||
} else if (event.type === HttpEventType.Response) { | ||
// Handle the response from the server for a successful upload | ||
console.log('Upload successful', event.body); | ||
|
||
// Reset the progress bar and indicate that upload is complete | ||
this.uploadInProgress = false; | ||
this.uploadProgress = 0; | ||
|
||
// Display a success message | ||
this.snackBar.open('File uploaded successfully!', 'Close', { | ||
duration: 5000, | ||
horizontalPosition: 'center', | ||
verticalPosition: 'bottom', | ||
}); | ||
} | ||
}, error => { | ||
console.error('Upload failed', error); | ||
|
||
// Reset the progress bar and indicate that upload is complete | ||
this.uploadInProgress = false; | ||
this.uploadProgress = 0; | ||
|
||
// Display an error message | ||
this.snackBar.open('Failed to upload file!', 'Close', { | ||
duration: 5000, | ||
horizontalPosition: 'center', | ||
verticalPosition: 'bottom', | ||
}); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
onFileSelected(event: Event): void { | ||
const input = event.target as HTMLInputElement; | ||
if (input.files && input.files.length) { | ||
this.selectedFile = input.files[0]; | ||
this.fileName = this.selectedFile.name; // Update the filename input if a file is selected | ||
} | ||
} | ||
|
||
set fileName(value: string) { | ||
this._fileName = value; | ||
if (this.selectedFile) { | ||
this.selectedFile = new File([this.selectedFile], this._fileName, { type: this.selectedFile.type }); | ||
} | ||
} | ||
|
||
get fileName(): string { | ||
return this._fileName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface FileDetails { | ||
name: string; | ||
extension: string; | ||
url: string; | ||
key: string; | ||
size: string; | ||
mimeType: string; | ||
description: string; | ||
category: string; | ||
subCategory: string; | ||
tags: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { FileService } from './file.service'; | ||
|
||
describe('FileService', () => { | ||
let service: FileService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(FileService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { HttpClient, HttpRequest } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable, switchMap } from 'rxjs'; | ||
import { environment } from 'src/environments/environment'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class FileService { | ||
|
||
private apiUrl = ``; | ||
private url = ``; | ||
|
||
constructor(private http: HttpClient) { } | ||
|
||
getUrl(fileName: string): Observable<string> { | ||
|
||
this.apiUrl = `${environment.awsUserApiBaseUrl}${environment.fileApiEndpoints.getUrl}`; | ||
this.apiUrl = this.apiUrl.replace('{key}', fileName); | ||
console.log('get file Url endpoint: ' + this.apiUrl); | ||
return this.http.get<string>(this.apiUrl, { responseType: 'text' as 'json' }); | ||
} | ||
|
||
getUrl2(url: string): Observable<string> { | ||
console.log('get file Url endpoint: ' + url); | ||
return this.http.get<string>(url, { responseType: 'text' as 'json' }); | ||
} | ||
|
||
|
||
generateUrl(fileName: string): Observable<string> { | ||
const apiUrl = `${environment.awsUserApiBaseUrl}${environment.fileApiEndpoints.generateUrl}`; | ||
const urlWithFileName = apiUrl.replace('{key}', fileName); | ||
console.log('generate file Url endpoint: ' + urlWithFileName); | ||
return this.http.get<string>(urlWithFileName, { responseType: 'text' as 'json' }); | ||
} | ||
|
||
uploadFile(file: File, fileName: string): Observable<any> { | ||
if (!fileName) { | ||
fileName = file.name; | ||
} | ||
|
||
// Use switchMap to use the URL from generateUrl to make the PUT request | ||
return this.generateUrl(fileName).pipe( | ||
switchMap((url: string) => { | ||
console.log('generatedUrl: ' + url); | ||
|
||
const req = new HttpRequest('PUT', url, file, { | ||
reportProgress: true, // Enable progress reporting | ||
responseType: 'json' | ||
}); | ||
|
||
// Make the PUT request directly with the file in the request body | ||
// Note: Do not set Content-Type header as it is automatically set by the browser | ||
// when passing a File object as the body, which is required for pre-signed S3 URLs | ||
return this.http.request(req); | ||
}) | ||
); | ||
} | ||
} |
Oops, something went wrong.