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 { 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-08-23 16:00:58 +01:00
|
|
|
import { FileLoaderService } from 'src/app/services/file/file-loader.service'
|
|
|
|
|
import { FileToBase64Service } from 'src/app/services/file/file-to-base64.service';
|
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(
|
|
|
|
|
public photoService: PhotoService,
|
|
|
|
|
private publications: PublicationsService,
|
|
|
|
|
private camera: Camera,
|
2021-06-15 15:09:20 +01:00
|
|
|
private toastService: ToastService,
|
2021-08-23 16:00:58 +01:00
|
|
|
private fileLoaderService: FileLoaderService,
|
|
|
|
|
private fileToBase64Service: FileToBase64Service
|
2021-08-23 16:06:05 +01:00
|
|
|
) {
|
|
|
|
|
this.publicationTitle = 'Nova Publicação';
|
|
|
|
|
}
|
2021-03-15 12:06:06 +01:00
|
|
|
|
|
|
|
|
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); */
|
2021-08-23 17:17:07 +01:00
|
|
|
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-08-23 16:00:58 +01:00
|
|
|
takePicture() {
|
2021-03-15 12:06:06 +01:00
|
|
|
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) => {
|
2021-08-23 16:00:58 +01:00
|
|
|
|
2021-03-15 12:06:06 +01:00
|
|
|
this.capturedImage = imageData;
|
|
|
|
|
this.capturedImageTitle = new Date().getTime() + '.jpeg';
|
|
|
|
|
}, (err) => {
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 16:00:58 +01:00
|
|
|
laodPicture() {
|
2021-08-23 16:01:07 +01:00
|
|
|
const input = this.fileLoaderService.createInput({
|
|
|
|
|
accept: ['image/apng', 'image/jpeg', 'image/png']
|
|
|
|
|
})
|
2021-08-23 16:00:58 +01:00
|
|
|
|
|
|
|
|
input.onchange = async () => {
|
2021-08-23 16:01:07 +01:00
|
|
|
const file = this.fileLoaderService.getFirstFile(input)
|
2021-08-23 16:00:58 +01:00
|
|
|
|
2021-08-23 16:01:07 +01:00
|
|
|
const imageData = await this.fileToBase64Service.convert(file)
|
2021-03-15 12:06:06 +01:00
|
|
|
this.capturedImage = imageData;
|
2021-08-23 16:01:07 +01:00
|
|
|
this.capturedImageTitle = file.name
|
2021-08-23 16:00:58 +01:00
|
|
|
};
|
2021-03-15 12:06:06 +01:00
|
|
|
|
2021-08-23 16:00:58 +01:00
|
|
|
}
|
2021-07-06 12:26:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
runValidation() {
|
|
|
|
|
this.validateFrom = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
injectValidation() {
|
|
|
|
|
|
|
|
|
|
this.Form = new FormGroup({
|
|
|
|
|
Subject: new FormControl(this.pub.Title, [
|
2021-07-26 15:19:03 +01:00
|
|
|
// Validators.required,
|
2021-07-06 12:26:45 +01:00
|
|
|
// Validators.minLength(4)
|
2021-07-15 15:39:10 +01:00
|
|
|
]),
|
|
|
|
|
Message: new FormControl(this.pub.Message, [
|
|
|
|
|
// Validators.required
|
|
|
|
|
Validators.maxLength(1000)
|
2021-07-06 12:26:45 +01:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-07-14 19:04:33 +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-07-26 16:40:51 +01:00
|
|
|
|
2021-05-25 16:12:40 +01:00
|
|
|
if(this.capturedImage != '') {
|
2021-07-26 16:40:51 +01:00
|
|
|
|
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-11-09 15:37:59 +01:00
|
|
|
this.toastService._successMessage()
|
2021-05-25 16:12:40 +01:00
|
|
|
|
|
|
|
|
this.goBack();
|
|
|
|
|
} catch (error) {
|
2021-11-09 15:37:59 +01:00
|
|
|
this.toastService._badRequest()
|
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-08-23 16:00:58 +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-08-23 16:00:58 +01:00
|
|
|
OriginalFileName: this.capturedImageTitle,
|
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-11-09 15:37:59 +01:00
|
|
|
this.toastService._successMessage()
|
2021-05-25 16:12:40 +01:00
|
|
|
|
|
|
|
|
this.goBack();
|
|
|
|
|
} catch (error) {
|
2021-11-09 15:37:59 +01:00
|
|
|
this.toastService._badRequest()
|
2021-07-12 11:13:29 +01:00
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
2021-05-25 16:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-26 15:19:03 +01:00
|
|
|
} 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',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('Edit change image');
|
|
|
|
|
console.log(this.publication);
|
|
|
|
|
const loader = this.toastService.loading()
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
|
2021-11-09 15:37:59 +01:00
|
|
|
this.toastService._successMessage()
|
2021-07-26 15:19:03 +01:00
|
|
|
|
|
|
|
|
this.goBack();
|
|
|
|
|
} catch (error) {
|
2021-11-09 15:37:59 +01:00
|
|
|
this.toastService._badRequest()
|
2021-07-26 15:19:03 +01:00
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
let time = new Date()
|
2021-03-15 12:06:06 +01:00
|
|
|
this.publication = {
|
2021-07-14 16:57:06 +01:00
|
|
|
DateIndex: time,
|
2021-07-26 15:19:03 +01:00
|
|
|
DocumentId: null,
|
2021-03-15 12:06:06 +01:00
|
|
|
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-11-09 15:37:59 +01:00
|
|
|
this.toastService._successMessage()
|
2021-05-25 16:12:40 +01:00
|
|
|
|
|
|
|
|
this.goBackToViewPublications.emit();
|
|
|
|
|
} catch (error) {
|
2021-11-09 15:37:59 +01:00
|
|
|
this.toastService._badRequest()
|
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-08-23 17:17:07 +01:00
|
|
|
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-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(){
|
2021-08-23 16:00:58 +01:00
|
|
|
if(this.publicationType == '1') {
|
2021-03-15 12:06:06 +01:00
|
|
|
this.publicationTitle = 'Nova Publicação Rápida';
|
|
|
|
|
}
|
2021-08-23 16:00:58 +01:00
|
|
|
else if(this.publicationType == '2') {
|
2021-03-15 12:06:06 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 09:01:24 +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-17 09:01:24 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-15 12:06:06 +01:00
|
|
|
}
|