Files
doneit-web/src/app/shared/publication/new-publication/new-publication.page.ts
T
2023-02-02 12:01:18 +01:00

307 lines
8.3 KiB
TypeScript

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { SafeResourceUrl } from '@angular/platform-browser';
import { PublicationsService } from 'src/app/services/publications.service';
import { Publication } from 'src/app/models/publication';
import { Image } from 'src/app/models/image';
import { PhotoService } from 'src/app/services/photo.service';
import { ToastService } from 'src/app/services/toast.service';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ThemeService } from 'src/app/services/theme.service';
import { Camera, CameraResultType, CameraSource} from '@capacitor/camera';
@Component({
selector: 'app-new-publication',
templateUrl: './new-publication.page.html',
styleUrls: ['./new-publication.page.scss'],
})
export class NewPublicationPage implements OnInit {
showLoader: boolean;
pub: Publication = new Publication();
image: Image = new Image();
publicationTitle:string;
imgUrl:any;
Defaultimage:any = '';
Form: FormGroup;
validateFrom = false
photo: SafeResourceUrl;
publication: Publication = new Publication();
@Input() publicationType: string;
@Input() folderId: string;
@Input() publicationId:string;
@Output() closeDesktopComponent = new EventEmitter<any>();
@Output() openPublicationDetails = new EventEmitter<any>();
@Output() goBackToViewPublications = new EventEmitter<any>();
@Output() goBacktoPublicationDetails = new EventEmitter<any>();
guestPicture:any;
capturedImage:any = '';
capturedImageTitle:any;
photos: any[] = [];
constructor(
public photoService: PhotoService,
private publications: PublicationsService,
private toastService: ToastService,
public ThemeService: ThemeService
) {
this.publicationTitle = 'Nova Publicação';
}
ngOnInit() {
if(this.publicationType == '3'){
this.getPublicationDetail();
}
this.setTitle();
//this.clear();
//this.takePicture();
}
getPublicationDetail() {
this.showLoader = true;
this.publications.GetPublicationById(this.publicationId).subscribe( res =>{
this.publication = {
DateIndex: res.DateIndex,
DocumentId: res.DocumentId,
ProcessId:res.ProcessId,
Title:res.Title,
Message: res.Message,
DatePublication: res.DatePublication,
FileBase64: "data:image/jpg;base64," + res.FileBase64,
OriginalFileName: res.OriginalFileName,
FileExtension: 'jpeg',
}
this.pub = this.publication;
this.showLoader = false;
});
}
async takePicture() {
const capturedImage = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
resultType: CameraResultType.Base64,
source: CameraSource.Camera
});
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = capturedImage.path;
//
}
async laodPicture() {
const capturedImage = await Camera.getPhoto({
quality: 90,
resultType: CameraResultType.Base64,
source: CameraSource.Photos
});
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = capturedImage.path;
}
runValidation() {
this.validateFrom = true
}
injectValidation() {
this.Form = new FormGroup({
Subject: new FormControl(this.pub.Title, [
// Validators.required,
// Validators.minLength(4)
]),
Message: new FormControl(this.pub.Message, [
// Validators.required
Validators.maxLength(1000)
])
})
}
async save() {
this.injectValidation()
this.runValidation()
if(this.Form.invalid) return false
if(this.publicationType == '3') {
if(this.capturedImage != '') {
this.publication = {
DateIndex: this.publication.DateIndex,
DocumentId:this.publication.DocumentId,
ProcessId:this.publication.ProcessId,
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: this.publication.DatePublication,
OriginalFileName: this.capturedImageTitle,
FileBase64: this.capturedImage,
FileExtension: 'jpeg',
}
const loader = this.toastService.loading()
try {
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
this.toastService._successMessage()
this.goBack();
} catch (error) {
if(error.status == 0) {
this.toastService._badRequest('sem conexão ao servidor')
} else {
this.toastService._badRequest()
}
} finally {
loader.remove()
}
}
else if (!this.publication.OriginalFileName) { //
this.publication = {
DateIndex: this.publication.DateIndex,
DocumentId:this.publication.DocumentId,
ProcessId:this.publication.ProcessId,
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: this.publication.DatePublication,
OriginalFileName: this.capturedImageTitle,
// OriginalFileName: this.publication.OriginalFileName,
// FileBase64: this.publication.FileBase64,
// FileExtension: 'jpeg',
}
const loader = this.toastService.loading()
try {
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
this.toastService._successMessage()
this.goBack();
} catch (error) {
if(error.status == 0) {
this.toastService._badRequest('sem conexão ao servidor')
} else {
this.toastService._badRequest()
}
} finally {
loader.remove()
}
} else {
this.publication = {
DateIndex: this.publication.DateIndex,
DocumentId:this.publication.DocumentId,
ProcessId:this.publication.ProcessId,
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: this.publication.DatePublication,
OriginalFileName: this.capturedImageTitle,
FileBase64: this.capturedImage,
FileExtension: 'jpeg',
}
const loader = this.toastService.loading()
try {
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
this.toastService._successMessage()
this.goBack();
} catch (error) {
if(error.status == 0) {
this.toastService._badRequest('sem conexão ao servidor')
} else {
this.toastService._badRequest()
}
} finally {
loader.remove()
}
}
}
else {
let time = new Date()
this.publication = {
DateIndex: time,
DocumentId: null,
ProcessId:this.folderId,
Title: this.pub.Title,
Message: this.pub.Message,
DatePublication: time,
OriginalFileName: this.capturedImageTitle,
FileBase64: this.capturedImage,
FileExtension: 'jpeg',
}
const loader = this.toastService.loading()
try {
await this.publications.CreatePublication(this.folderId, this.publication).toPromise()
this.toastService._successMessage()
this.goBackToViewPublications.emit();
} catch (error) {
if(error.status == 0) {
this.toastService._badRequest('sem conexão ao servidor')
} else {
this.toastService._badRequest()
}
} finally {
loader.remove()
}
}
}
close(){
this.goBack();
}
clear(){
this.capturedImage = '';
}
setTitle(){
if(this.publicationType == '1') {
this.publicationTitle = 'Nova Publicação Rápida';
}
else if(this.publicationType == '2') {
this.publicationTitle = 'Nova Publicação';
}
else if(this.publicationType == '3') {
this.publicationTitle = 'Editar Publicação';
this.pub = this.publication;
}
}
async goBack(){
if(this.publicationType == '2'){
this.goBackToViewPublications.emit();
} else {
this.goBackToViewPublications.emit();
//this.goBacktoPublicationDetails.emit();
}
}
}