2020-12-01 14:03:15 +01:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2021-07-06 16:18:00 +01:00
|
|
|
import { ModalController, NavParams } from '@ionic/angular';
|
2020-12-09 12:10:19 +01:00
|
|
|
|
2020-12-11 18:00:38 +01:00
|
|
|
/* import {Plugins, CameraResultType, CameraSource} from '@capacitor/core'; */
|
2020-12-09 12:10:19 +01:00
|
|
|
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
|
|
|
|
|
import { PublicationsService } from 'src/app/services/publications.service';
|
|
|
|
|
import { Publication } from 'src/app/models/publication';
|
2020-12-10 11:22:06 +01:00
|
|
|
import { Image } from 'src/app/models/image';
|
2020-12-11 15:09:53 +01:00
|
|
|
import { PhotoService } from 'src/app/services/photo.service';
|
2021-01-15 11:07:20 +01:00
|
|
|
//Capacitor
|
2020-12-11 18:00:38 +01:00
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
import { ToastService } from 'src/app/services/toast.service';
|
2021-07-06 16:18:00 +01:00
|
|
|
import { FormControl, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
import { ThemePalette } from '@angular/material/core';
|
2021-07-15 17:01:32 +01:00
|
|
|
import { formatDate } from 'src/plugin/momentG.js'
|
2021-08-23 16:06:05 +01:00
|
|
|
import { FileLoaderService } from 'src/app/services/file/file-loader.service';
|
|
|
|
|
import { FileToBase64Service } from 'src/app/services/file/file-to-base64.service';
|
2021-11-09 10:13:48 +01:00
|
|
|
import { ThemeService } from 'src/app/services/theme.service';
|
|
|
|
|
import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera';
|
2020-12-01 14:03:15 +01:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-new-publication',
|
|
|
|
|
templateUrl: './new-publication.page.html',
|
|
|
|
|
styleUrls: ['./new-publication.page.scss'],
|
|
|
|
|
})
|
|
|
|
|
export class NewPublicationPage implements OnInit {
|
2021-07-06 16:18:00 +01:00
|
|
|
|
|
|
|
|
// date picker
|
|
|
|
|
public date: any;
|
|
|
|
|
public disabled = false;
|
|
|
|
|
public showSpinners = true;
|
|
|
|
|
public showSeconds = false;
|
|
|
|
|
public touchUi = false;
|
|
|
|
|
public enableMeridian = false;
|
|
|
|
|
public minDate = new Date().toISOString().slice(0,10)
|
|
|
|
|
public endMinDate = new Date(new Date().getTime() + 15 * 60000);
|
|
|
|
|
public stepHour = 1;
|
|
|
|
|
public stepMinute = 5;
|
|
|
|
|
public stepSecond = 5;
|
|
|
|
|
public color: ThemePalette = 'primary';
|
|
|
|
|
|
|
|
|
|
Form: FormGroup;
|
|
|
|
|
validateFrom = false
|
|
|
|
|
|
2020-12-15 19:37:42 +01:00
|
|
|
showLoader: boolean;
|
2020-12-09 12:10:19 +01:00
|
|
|
publication: Publication;
|
|
|
|
|
pub: Publication = new Publication();
|
2020-12-10 11:22:06 +01:00
|
|
|
folderId: string;
|
|
|
|
|
image: Image = new Image();
|
2020-12-09 12:10:19 +01:00
|
|
|
|
2020-12-01 17:22:45 +01:00
|
|
|
publicationType:string;
|
|
|
|
|
publicationTitle:string;
|
2020-12-09 12:10:19 +01:00
|
|
|
imgUrl:any;
|
|
|
|
|
|
2020-12-10 11:22:06 +01:00
|
|
|
Defaultimage:any = '';
|
|
|
|
|
|
2020-12-09 12:10:19 +01:00
|
|
|
photo: SafeResourceUrl;
|
|
|
|
|
|
|
|
|
|
guestPicture:any;
|
|
|
|
|
|
2021-11-09 10:13:48 +01:00
|
|
|
capturedImage:any = '';
|
2020-12-11 18:00:38 +01:00
|
|
|
capturedImageTitle:any;
|
2021-11-09 10:13:48 +01:00
|
|
|
public photos: any[] = [];
|
2020-12-11 18:00:38 +01:00
|
|
|
|
2020-12-01 17:22:45 +01:00
|
|
|
constructor(
|
|
|
|
|
private modalController: ModalController,
|
2020-12-11 15:09:53 +01:00
|
|
|
public photoService: PhotoService,
|
2020-12-01 17:22:45 +01:00
|
|
|
private navParams: NavParams,
|
2020-12-09 12:10:19 +01:00
|
|
|
private publications: PublicationsService,
|
2021-06-29 14:16:49 +01:00
|
|
|
private toastService: ToastService,
|
2021-08-23 16:06:05 +01:00
|
|
|
private fileLoaderService: FileLoaderService,
|
2021-10-27 14:42:10 +01:00
|
|
|
private fileToBase64Service: FileToBase64Service,
|
|
|
|
|
public ThemeService: ThemeService
|
2020-12-01 17:22:45 +01:00
|
|
|
) {
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2020-12-01 17:22:45 +01:00
|
|
|
this.publicationType = this.navParams.get('publicationType');
|
2020-12-10 11:22:06 +01:00
|
|
|
this.folderId = this.navParams.get('folderId');
|
2020-12-01 17:22:45 +01:00
|
|
|
this.publicationTitle = 'Nova Publicação';
|
2021-07-06 16:18:00 +01:00
|
|
|
}
|
2020-12-01 14:03:15 +01:00
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-12-01 17:22:45 +01:00
|
|
|
this.setTitle();
|
2021-11-09 12:21:23 +01:00
|
|
|
console.log(this.folderId);
|
|
|
|
|
|
2021-05-07 13:19:50 +01:00
|
|
|
// this.takePicture();
|
2020-12-01 17:22:45 +01:00
|
|
|
}
|
2021-07-06 16:18:00 +01:00
|
|
|
|
2021-11-09 10:13:48 +01:00
|
|
|
async takePicture() {
|
|
|
|
|
const capturedImage = await Camera.getPhoto({
|
|
|
|
|
quality: 90,
|
|
|
|
|
// allowEditing: true,
|
|
|
|
|
resultType: CameraResultType.Uri,
|
|
|
|
|
source: CameraSource.Camera
|
2020-12-11 18:00:38 +01:00
|
|
|
|
2021-11-09 10:13:48 +01:00
|
|
|
});
|
|
|
|
|
const response = await fetch(capturedImage.webPath!);
|
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
|
|
this.photos.unshift({
|
|
|
|
|
filepath: "soon...",
|
|
|
|
|
webviewPath: capturedImage.webPath
|
2021-02-09 13:16:41 +01:00
|
|
|
});
|
2021-11-09 10:13:48 +01:00
|
|
|
|
|
|
|
|
this.capturedImage = await this.convertBlobToBase64(blob);
|
2021-11-09 11:54:56 +01:00
|
|
|
this.capturedImageTitle = new Date().getTime() + '.jpeg';
|
2021-11-09 10:13:48 +01:00
|
|
|
|
|
|
|
|
//console.log(this.capturedImage);
|
|
|
|
|
|
2020-12-11 15:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-09 10:13:48 +01:00
|
|
|
convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
|
|
|
|
|
const reader = new FileReader;
|
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
reader.onload = () => {
|
|
|
|
|
resolve(reader.result);
|
|
|
|
|
};
|
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-23 16:06:05 +01:00
|
|
|
|
2021-11-09 16:38:26 +01:00
|
|
|
async laodPicture() {
|
|
|
|
|
const capturedImage = await Camera.getPhoto({
|
|
|
|
|
resultType: CameraResultType.Uri,
|
|
|
|
|
source: CameraSource.Photos,
|
|
|
|
|
quality: 90,
|
|
|
|
|
width: 1080,
|
|
|
|
|
height: 720,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await fetch(capturedImage.webPath!);
|
|
|
|
|
const blob = await response.blob();
|
|
|
|
|
|
|
|
|
|
this.photos.unshift({
|
|
|
|
|
filepath: "soon...",
|
|
|
|
|
webviewPath: capturedImage.webPath
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.capturedImage = await this.convertBlobToBase64(blob);
|
|
|
|
|
this.capturedImageTitle = new Date().getTime() + '.jpeg';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* laodPicture() {
|
2021-08-23 16:06:05 +01:00
|
|
|
const input = this.fileLoaderService.createInput({
|
|
|
|
|
accept: ['image/apng', 'image/jpeg', 'image/png']
|
|
|
|
|
})
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-08-23 16:06:05 +01:00
|
|
|
input.onchange = async () => {
|
|
|
|
|
const file = this.fileLoaderService.getFirstFile(input)
|
|
|
|
|
|
|
|
|
|
const imageData = await this.fileToBase64Service.convert(file)
|
|
|
|
|
this.capturedImage = imageData;
|
|
|
|
|
this.capturedImageTitle = file.name
|
2021-08-24 11:15:13 +01:00
|
|
|
|
|
|
|
|
console.log(this.capturedImage)
|
2021-08-23 16:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
2021-11-09 16:38:26 +01:00
|
|
|
} */
|
2021-08-23 16:06:05 +01:00
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
|
2020-12-09 12:10:19 +01:00
|
|
|
|
2021-07-06 16:18:00 +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 16:18:00 +01:00
|
|
|
// Validators.minLength(4)
|
|
|
|
|
]),
|
|
|
|
|
capturedImage: new FormControl(this.capturedImage, [
|
|
|
|
|
|
|
|
|
|
]),
|
|
|
|
|
Message: new FormControl(this.pub.Message, [
|
2021-07-08 14:23:37 +01:00
|
|
|
// Validators.required
|
2021-07-15 15:41:19 +01:00
|
|
|
Validators.maxLength(1000)
|
2021-07-15 15:39:10 +01:00
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
async save() {
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
this.injectValidation()
|
|
|
|
|
this.runValidation()
|
|
|
|
|
|
|
|
|
|
if(this.Form.invalid) return false
|
|
|
|
|
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-07-14 15:38:58 +01:00
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
if(this.publicationType == '3') {
|
2020-12-17 10:59:11 +01:00
|
|
|
console.log(this.navParams.get('publication'));
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-11-09 12:21:23 +01:00
|
|
|
if(this.capturedImage != ''){
|
2020-12-11 18:00:38 +01:00
|
|
|
this.publication = {
|
2020-12-17 10:59:11 +01:00
|
|
|
DateIndex: this.publication.DateIndex,
|
2020-12-11 18:00:38 +01:00
|
|
|
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,
|
2020-12-17 10:59:11 +01:00
|
|
|
FileExtension: 'jpeg',
|
2020-12-11 18:00:38 +01:00
|
|
|
}
|
2021-06-29 14:16:49 +01:00
|
|
|
|
2021-07-12 11:13:29 +01:00
|
|
|
const loader = this.toastService.loading()
|
|
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
try {
|
|
|
|
|
console.log(this.publication);
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
|
2021-11-09 12:21:23 +01:00
|
|
|
this.toastService.successMessage("Publicação editada")
|
2021-06-29 14:16:49 +01:00
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
|
} catch (error) {
|
2021-11-09 12:21:23 +01:00
|
|
|
this.toastService.badRequest("Publicação não editada")
|
2021-07-12 11:13:29 +01:00
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
2021-06-29 14:16:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-10 11:22:06 +01:00
|
|
|
}
|
2021-07-14 15:38:58 +01:00
|
|
|
else if (!this.publication.OriginalFileName) {
|
|
|
|
|
|
2020-12-11 18:00:38 +01:00
|
|
|
this.publication = {
|
2020-12-17 10:59:11 +01:00
|
|
|
DateIndex: this.publication.DateIndex,
|
2020-12-11 18:00:38 +01:00
|
|
|
DocumentId:this.publication.DocumentId,
|
|
|
|
|
ProcessId:this.publication.ProcessId,
|
|
|
|
|
Title: this.pub.Title,
|
|
|
|
|
Message: this.pub.Message,
|
|
|
|
|
DatePublication: this.publication.DatePublication,
|
2021-07-14 15:38:58 +01:00
|
|
|
// OriginalFileName: this.publication.OriginalFileName,
|
|
|
|
|
// FileBase64: this.publication.FileBase64,
|
|
|
|
|
// FileExtension: 'jpeg',
|
2020-12-11 18:00:38 +01:00
|
|
|
}
|
2021-04-14 15:27:50 +01:00
|
|
|
/* console.log('Edit - keep image');
|
|
|
|
|
console.log(this.publication); */
|
2021-07-12 11:13:29 +01:00
|
|
|
|
|
|
|
|
const loader = this.toastService.loading()
|
|
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
try {
|
|
|
|
|
console.log(this.publication);
|
|
|
|
|
await this.publications.UpdatePublication(this.publication.ProcessId, this.publication).toPromise()
|
|
|
|
|
this.toastService.successMessage("Publicação criado")
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
this.close();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.toastService.badRequest("Publicação não criado")
|
2021-07-12 11:13:29 +01:00
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
2021-06-29 14:16:49 +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()
|
|
|
|
|
this.toastService.successMessage("Publicação criado")
|
|
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.toastService.badRequest("Publicação não criado")
|
|
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 12:10:19 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-07-06 16:18:00 +01:00
|
|
|
else {
|
2021-07-14 16:57:06 +01:00
|
|
|
|
2021-07-15 17:01:32 +01:00
|
|
|
const date = formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss')
|
2021-11-09 12:21:23 +01:00
|
|
|
console.log(date);
|
|
|
|
|
console.log(this.folderId);
|
|
|
|
|
|
2021-07-15 17:01:32 +01:00
|
|
|
|
2020-12-10 11:22:06 +01:00
|
|
|
this.publication = {
|
2021-07-15 17:01:32 +01:00
|
|
|
DateIndex: date,
|
2020-12-11 15:09:53 +01:00
|
|
|
DocumentId:null,
|
|
|
|
|
ProcessId:this.folderId,
|
|
|
|
|
Title: this.pub.Title,
|
|
|
|
|
Message: this.pub.Message,
|
2021-07-15 17:01:32 +01:00
|
|
|
DatePublication: date,
|
2020-12-11 18:00:38 +01:00
|
|
|
OriginalFileName: this.capturedImageTitle,
|
|
|
|
|
FileBase64: this.capturedImage,
|
2020-12-17 10:59:11 +01:00
|
|
|
FileExtension: 'jpeg',
|
2020-12-10 11:22:06 +01:00
|
|
|
}
|
2021-06-29 14:16:49 +01:00
|
|
|
|
2021-07-12 11:13:29 +01:00
|
|
|
const loader = this.toastService.loading()
|
|
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
try {
|
|
|
|
|
console.log(this.publication);
|
2021-06-30 16:11:07 +01:00
|
|
|
await this.publications.CreatePublication(this.folderId, this.publication).toPromise();
|
|
|
|
|
this.close();
|
2021-06-29 14:16:49 +01:00
|
|
|
this.toastService.successMessage("Publicação criado")
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-06-29 14:16:49 +01:00
|
|
|
|
|
|
|
|
this.close();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
this.toastService.badRequest("Publicação não criado")
|
2021-07-12 11:13:29 +01:00
|
|
|
} finally {
|
|
|
|
|
loader.remove()
|
2021-06-29 14:16:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-09 12:10:19 +01:00
|
|
|
}
|
2020-12-01 17:22:45 +01:00
|
|
|
}
|
2020-12-09 12:10:19 +01:00
|
|
|
|
|
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
close() {
|
2020-12-15 19:37:42 +01:00
|
|
|
this.modalController.dismiss().then(()=>{
|
|
|
|
|
this.showLoader=true;
|
|
|
|
|
});
|
2020-12-01 17:22:45 +01:00
|
|
|
}
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2021-07-06 16:18:00 +01:00
|
|
|
clear() {
|
2020-12-11 18:00:38 +01:00
|
|
|
this.capturedImage = '';
|
2020-12-11 15:09:53 +01:00
|
|
|
}
|
2021-07-06 16:18:00 +01:00
|
|
|
|
|
|
|
|
setTitle() {
|
2021-06-30 12:03:28 +01:00
|
|
|
if(this.publicationType == '1') {
|
2020-12-10 11:22:06 +01:00
|
|
|
this.publicationTitle = 'Nova Publicação Rápida';
|
2020-12-01 17:22:45 +01:00
|
|
|
}
|
2021-06-30 12:03:28 +01:00
|
|
|
else if(this.publicationType == '2') {
|
2020-12-10 11:22:06 +01:00
|
|
|
this.publicationTitle = 'Nova Publicação';
|
2020-12-01 17:22:45 +01:00
|
|
|
}
|
2021-06-30 12:03:28 +01:00
|
|
|
else if(this.publicationType == '3') {
|
2020-12-09 12:10:19 +01:00
|
|
|
this.publicationTitle = 'Editar Publicação';
|
|
|
|
|
this.pub = this.navParams.get('publication');
|
2021-07-14 15:38:58 +01:00
|
|
|
|
|
|
|
|
console.log(this.pub, 'pub')
|
2020-12-02 15:08:45 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 15:09:53 +01:00
|
|
|
/* async openGallery() {
|
2020-12-02 15:08:45 +01:00
|
|
|
const modal = await this.modalController.create({
|
|
|
|
|
component: GalleryPage,
|
|
|
|
|
componentProps:{
|
|
|
|
|
},
|
|
|
|
|
cssClass: 'new-publication',
|
|
|
|
|
backdropDismiss: false
|
|
|
|
|
});
|
|
|
|
|
await modal.present();
|
|
|
|
|
modal.onDidDismiss();
|
2020-12-11 15:09:53 +01:00
|
|
|
} */
|
2020-12-01 14:03:15 +01:00
|
|
|
|
2020-12-11 18:00:38 +01:00
|
|
|
/* async takePicture(){
|
2020-12-09 12:10:19 +01:00
|
|
|
const image = await Plugins.Camera.getPhoto({
|
|
|
|
|
quality: 100,
|
|
|
|
|
allowEditing: false,
|
|
|
|
|
resultType: CameraResultType.DataUrl,
|
|
|
|
|
source: CameraSource.Camera
|
|
|
|
|
});
|
2020-12-11 15:09:53 +01:00
|
|
|
console.log(image);
|
2021-11-09 10:13:48 +01:00
|
|
|
|
2020-12-09 12:10:19 +01:00
|
|
|
this.photo = this.sanitizer.bypassSecurityTrustResourceUrl(image && (image.dataUrl));
|
2020-12-11 18:00:38 +01:00
|
|
|
} */
|
2020-12-09 12:10:19 +01:00
|
|
|
|
2021-11-09 10:13:48 +01:00
|
|
|
}
|