Files
doneit-web/src/app/shared/publication/new-publication/new-publication.page.ts
T

316 lines
8.6 KiB
TypeScript
Raw Normal View History

2021-03-15 12:06:06 +01:00
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
2021-07-12 11:13:29 +01:00
import { AnimationController, ModalController } from '@ionic/angular';
import { SafeResourceUrl } from '@angular/platform-browser';
2021-03-15 12:06:06 +01:00
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 { Camera, CameraOptions } from '@ionic-native/camera/ngx';
2021-06-15 15:09:20 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-07-06 12:26:45 +01:00
import { FormControl, FormGroup, Validators } from '@angular/forms';
2021-03-15 12:06:06 +01:00
@Component({
selector: 'app-new-publication',
templateUrl: './new-publication.page.html',
styleUrls: ['./new-publication.page.scss'],
})
export class NewPublicationPage implements OnInit {
showLoader: boolean;
2021-04-06 17:39:27 +01:00
pub: Publication = new Publication();
2021-03-15 12:06:06 +01:00
image: Image = new Image();
publicationTitle:string;
imgUrl:any;
Defaultimage:any = '';
2021-07-06 12:26:45 +01:00
Form: FormGroup;
validateFrom = false
2021-03-15 12:06:06 +01:00
photo: SafeResourceUrl;
2021-04-06 17:39:27 +01:00
publication: Publication = new Publication();
2021-03-15 12:06:06 +01:00
@Input() publicationType: string;
@Input() folderId: string;
2021-04-08 11:55:44 +01:00
@Input() publicationId:string;
2021-03-15 12:06:06 +01:00
@Output() closeDesktopComponent = new EventEmitter<any>();
2021-03-17 10:03:39 +01:00
@Output() openPublicationDetails = new EventEmitter<any>();
@Output() goBackToViewPublications = new EventEmitter<any>();
@Output() goBacktoPublicationDetails = new EventEmitter<any>();
2021-03-15 12:06:06 +01:00
guestPicture:any;
capturedImage:any;
capturedImageTitle:any;
constructor(
private modalController: ModalController,
public photoService: PhotoService,
private publications: PublicationsService,
private camera: Camera,
2021-06-15 15:09:20 +01:00
private animationController: AnimationController,
private toastService: ToastService,
2021-03-15 12:06:06 +01:00
) {
this.publicationTitle = 'Nova Publicação';
}
ngOnInit() {
2021-04-08 11:55:44 +01:00
if(this.publicationType == '3'){
this.getPublicationDetail();
}
2021-03-15 12:06:06 +01:00
this.setTitle();
this.clear();
this.takePicture();
}
2021-04-08 11:55:44 +01:00
2021-07-12 11:13:29 +01:00
getPublicationDetail() {
2021-04-08 11:55:44 +01:00
this.showLoader = true;
2021-04-15 23:57:14 +01:00
//console.log(this.publicationId);
2021-04-08 11:55:44 +01:00
/* console.log(this.folderId); */
this.publications.GetPublicationById(this.publicationId).subscribe(res=>{
2021-04-15 23:57:14 +01:00
//console.log(res);
2021-04-08 11:55:44 +01:00
/* this.publication = 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;
});
}
2021-03-15 12:06:06 +01:00
takePicture(){
const options: CameraOptions = {
quality: 90,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
let base64Image = 'data:image/jpeg;base64,' + imageData;
this.capturedImage = imageData;
this.capturedImageTitle = new Date().getTime() + '.jpeg';
}, (err) => {
console.log(err);
});
}
getPicture(){
const options: CameraOptions = {
quality: 90,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
let base64Image = 'data:image/jpeg;base64,' + imageData;
this.capturedImage = imageData;
this.capturedImageTitle = new Date().getTime() + '.jpeg';
}, (err) => {
console.log(err);
});
}
2021-07-06 12:26:45 +01:00
runValidation() {
this.validateFrom = true
}
injectValidation() {
this.Form = new FormGroup({
Subject: new FormControl(this.pub.Title, [
Validators.required,
// Validators.minLength(4)
])
})
}
2021-04-06 17:39:27 +01:00
async save(){
2021-07-06 12:26:45 +01:00
this.injectValidation()
this.runValidation()
if(this.Form.invalid) return false
2021-07-14 15:38:58 +01:00
if(this.publicationType == '3') {
2021-03-15 12:06:06 +01:00
2021-05-25 16:12:40 +01:00
if(this.capturedImage != '') {
2021-03-15 12:06:06 +01:00
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',
}
2021-07-14 15:38:58 +01:00
2021-03-15 12:06:06 +01:00
console.log('Edit change image');
console.log(this.publication);
2021-07-12 11:13:29 +01:00
const loader = this.toastService.loading()
2021-05-25 16:12:40 +01:00
try {
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
2021-06-15 15:09:20 +01:00
this.toastService.successMessage("Publicação criado")
2021-05-25 16:12:40 +01:00
this.goBack();
} catch (error) {
2021-06-15 15:09:20 +01:00
this.toastService.badRequest("Publicação não criado")
2021-07-12 11:13:29 +01:00
} finally {
loader.remove()
2021-05-25 16:12:40 +01:00
}
2021-03-15 12:06:06 +01:00
}
2021-07-14 16:57:06 +01:00
else if (!this.publication.OriginalFileName) {
2021-03-15 12:06:06 +01:00
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,
2021-07-14 16:57:06 +01:00
// OriginalFileName: this.publication.OriginalFileName,
// FileBase64: this.publication.FileBase64,
// FileExtension: 'jpeg',
2021-03-15 12:06:06 +01:00
}
2021-07-12 11:13:29 +01:00
const loader = this.toastService.loading()
2021-05-25 16:12:40 +01:00
try {
console.log(this.publication);
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
2021-06-15 15:09:20 +01:00
this.toastService.successMessage("Publicação criado")
2021-05-25 16:12:40 +01:00
this.goBack();
} catch (error) {
2021-06-15 15:09:20 +01:00
this.toastService.badRequest("Publicação não criado")
2021-07-12 11:13:29 +01:00
} finally {
loader.remove()
2021-05-25 16:12:40 +01:00
}
2021-03-15 12:06:06 +01:00
}
}
2021-07-12 11:13:29 +01:00
else {
2021-07-14 16:57:06 +01:00
2021-07-14 17:02:18 +01:00
2021-07-14 16:57:06 +01:00
let time = new Date()
alert(time)
2021-03-15 12:06:06 +01:00
this.publication = {
2021-07-14 16:57:06 +01:00
DateIndex: time,
2021-03-15 12:06:06 +01:00
DocumentId:null,
ProcessId:this.folderId,
2021-04-06 17:39:27 +01:00
Title: this.pub.Title,
2021-03-15 12:06:06 +01:00
Message: this.pub.Message,
2021-07-14 16:57:06 +01:00
DatePublication: time,
2021-03-15 12:06:06 +01:00
OriginalFileName: this.capturedImageTitle,
FileBase64: this.capturedImage,
FileExtension: 'jpeg',
}
2021-04-06 17:39:27 +01:00
2021-07-12 11:13:29 +01:00
const loader = this.toastService.loading()
2021-05-25 16:12:40 +01:00
try {
console.log(this.publication);
await this.publications.CreatePublication(this.folderId, this.publication).toPromise()
2021-06-15 15:09:20 +01:00
this.toastService.successMessage("Publicação criado")
2021-05-25 16:12:40 +01:00
this.goBackToViewPublications.emit();
} catch (error) {
2021-06-15 15:09:20 +01:00
this.toastService.badRequest("Publicação não criado")
2021-07-12 11:13:29 +01:00
} finally {
loader.remove()
2021-05-25 16:12:40 +01:00
}
2021-03-15 12:06:06 +01:00
}
}
close(){
2021-03-17 10:03:39 +01:00
this.goBack();
2021-03-15 12:06:06 +01:00
}
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';
}
2021-07-14 15:38:58 +01:00
else if(this.publicationType == '3') {
2021-03-15 12:06:06 +01:00
this.publicationTitle = 'Editar Publicação';
2021-03-16 14:35:52 +01:00
this.pub = this.publication;
2021-03-15 12:06:06 +01:00
}
}
async goBack(){
2021-03-17 10:03:39 +01:00
if(this.publicationType == '2'){
this.goBackToViewPublications.emit();
} else {
2021-04-08 11:55:44 +01:00
this.goBackToViewPublications.emit();
//this.goBacktoPublicationDetails.emit();
2021-03-17 10:03:39 +01:00
}
}
2021-03-15 12:06:06 +01:00
/* async openGallery() {
const modal = await this.modalController.create({
component: GalleryPage,
componentProps:{
},
cssClass: 'new-publication',
backdropDismiss: false
});
await modal.present();
modal.onDidDismiss();
} */
/* async takePicture(){
const image = await Plugins.Camera.getPhoto({
quality: 100,
allowEditing: false,
resultType: CameraResultType.DataUrl,
source: CameraSource.Camera
});
console.log(image);
this.photo = this.sanitizer.bypassSecurityTrustResourceUrl(image && (image.dataUrl));
} */
2021-05-25 16:12:40 +01:00
2021-06-08 15:59:06 +01:00
2021-05-25 16:12:40 +01:00
2021-03-15 12:06:06 +01:00
}