Created
July 4, 2022 07:57
-
-
Save aasumitro/a7b7fae00517cdd63575631a7f579a6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* eslint-disable @typescript-eslint/member-ordering */ | |
// noinspection DuplicatedCode | |
import {Component, OnDestroy, OnInit} from '@angular/core'; | |
import {ModalController} from '@ionic/angular'; | |
import {NadiService} from '../../../../../../services/nadi.service'; | |
import {CameraUsecase} from '../../../../data/usecases/camera.usecase'; | |
import {Router} from '@angular/router'; | |
import {FileConverterUtil} from '../../../../utils/file-converter.util'; | |
@Component({ | |
selector: 'app-livechat-camera', | |
templateUrl: './camera.component.html', | |
styleUrls: ['./camera.component.scss'], | |
}) | |
export class CameraComponent implements OnInit, OnDestroy { | |
selectedImageFormat: string = null; | |
selectedImage: string = null; | |
preSignUrl: string = null; | |
constructor( | |
private nadiService: NadiService, | |
private router: Router, | |
private modalController: ModalController, | |
public cameraUsecase: CameraUsecase | |
) { } | |
async ngOnInit() {} | |
async confirmAndUpload() { | |
if (this.preSignUrl) { | |
await this.nadiService.commonService.showLoading(); | |
await this.modalController.dismiss({ | |
payload: this.preSignUrl | |
}); | |
return; | |
} | |
try { | |
await this.nadiService.commonService.showLoading(); | |
const file = new FileConverterUtil().base64StringToFile( | |
this.selectedImage, | |
`${new Date().getTime()}.${this.selectedImageFormat}` | |
); | |
await this.performUploadAction(file); | |
} catch (e) { | |
await this.nadiService.commonService.hideLoading(); | |
await this.handleAlertMessage( | |
'Error', | |
`Something went wrong: ${e.message}, please try again!`, | |
() => {}, | |
'OK' | |
); | |
} | |
} | |
private async performUploadAction(file: File) { | |
await this.performGetPreSignedUrl().then((response) => { | |
this.performUploadFile(response.payload.signedUrl, file) | |
//.then(resp => console.log(resp)) | |
//.catch(err => console.log(err)) | |
.finally(async () => { | |
await this.nadiService.commonService.hideLoading(); | |
this.preSignUrl = response.payload.fileUrl; | |
await this.modalController.dismiss({ | |
payload: response.payload.fileUrl | |
}); | |
}); | |
}).catch(async (error) => { | |
await this.nadiService.commonService.hideLoading(); | |
await this.handleAlertMessage( | |
'Error, Pre-sign!', | |
`Something went wrong: ${error.message}, please try again!`, | |
() => {}, | |
'OK' | |
); | |
}); | |
} | |
private async performGetPreSignedUrl(): Promise<any> { | |
const url = `/v1/customer/livechat/presigned/photo/${this.selectedImageFormat}`; | |
return new Promise((resolve, reject) => { | |
this.nadiService.httpService | |
.getRequest(url) | |
.then((response) => resolve(response)) | |
.catch((error) => reject(error)); | |
}); | |
} | |
private async performUploadFile(url: string, file: File): Promise<any> { | |
return this.nadiService.httpService.http | |
.put<any>(url, file) | |
.toPromise(); | |
} | |
private async handleAlertMessage( | |
title: string, | |
message: string, | |
callback: () => void, | |
cbTitle: string = 'OK' | |
) { | |
const alert = await this.nadiService.alertController.create({ | |
header: title, message, | |
backdropDismiss : false, | |
buttons: [ { text: cbTitle, handler: _ => { callback(); } } ] | |
}); | |
await alert.present(); | |
} | |
close = () => this.modalController.dismiss(); | |
ngOnDestroy() { | |
this.selectedImage = null; | |
this.preSignUrl = null; | |
this.cameraUsecase.files = []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment