Files
doneit-web/src/app/shared/agenda/edit-event/edit-event.page.ts
T

425 lines
11 KiB
TypeScript
Raw Normal View History

2021-06-23 15:39:45 +01:00
import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
2021-02-24 09:14:58 +01:00
import { ModalController } from '@ionic/angular';
import { EventBody } from 'src/app/models/eventbody.model';
import { EventPerson } from 'src/app/models/eventperson.model';
import { EventsService } from 'src/app/services/events.service';
import { Event } from 'src/app/models/event.model';
import { AlertController } from '@ionic/angular';
2021-04-08 20:11:25 +01:00
import { removeDuplicate } from 'src/plugin/removeDuplicate.js'
import { SearchPage } from 'src/app/pages/search/search.page';
2021-04-19 18:03:41 +01:00
import { AttachmentsService } from 'src/app/services/attachments.service';
import { Attachment } from 'src/app/models/attachment.model';
2021-06-23 15:39:45 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-07-01 16:23:47 +01:00
import { FormControl, FormGroup, Validators } from '@angular/forms';
2021-09-03 12:19:21 +01:00
import { ParticipantsPipe } from 'src/app/pipes/participants.pipe';
2021-06-23 15:39:45 +01:00
2021-02-24 09:14:58 +01:00
@Component({
selector: 'app-edit-event',
2021-06-03 14:10:16 +01:00
templateUrl: './edit-event.page.html',
styleUrls: ['./edit-event.page.scss'],
2021-02-24 09:14:58 +01:00
})
2021-07-06 16:18:00 +01:00
2021-06-03 14:10:16 +01:00
export class EditEventPage implements OnInit {
2021-07-13 11:15:19 +01:00
2021-02-24 09:14:58 +01:00
stEvent: Event;
isRecurring:string;
isEventEdited: boolean;
loadedEvent: Event;
2021-06-29 10:50:05 +01:00
initCalendarName: string;
2021-02-24 09:14:58 +01:00
eventBody: EventBody;
segment:string = "true";
eventAttendees: EventPerson[];
2021-06-23 15:39:45 +01:00
// minDate: string;
2021-07-09 12:50:03 +01:00
loadedEventAttachments: Attachment[]=[];
2021-08-31 15:05:42 +01:00
recurringTypes = [];
selectedRecurringType: any;
2021-06-23 15:39:45 +01:00
public date: any;
public disabled = false;
public showSpinners = true;
public showSeconds = false;
public touchUi = false;
public enableMeridian = false;
2021-07-08 14:45:38 +01:00
public minDate = new Date().toISOString().slice(0,10)
2021-07-12 16:30:03 +01:00
public endMinDate = new Date(new Date().getTime() + 15 * 60000).toISOString().slice(0,10)
2021-06-23 15:39:45 +01:00
public maxDate: any;
public stepHour = 1;
public stepMinute = 5;
public stepSecond = 5;
2021-07-01 16:23:47 +01:00
Form: FormGroup;
validateFrom = false
2021-04-08 13:39:48 +01:00
@Input() taskParticipants: EventPerson[];
@Input() taskParticipantsCc: EventPerson[];
2021-02-24 09:14:58 +01:00
@Input() profile:string;
@Input() selectedSegment: string;
@Input() postEvent: Event;
@Output() clearContact = new EventEmitter<any>();
2021-02-24 09:14:58 +01:00
2021-03-25 15:18:12 +01:00
@Output() openAttendeesComponent = new EventEmitter<any>();
@Output() closeComponent = new EventEmitter<any>();
2021-07-12 16:39:55 +01:00
2021-04-07 15:13:31 +01:00
@Output() setIntervenient = new EventEmitter<any>();
@Output() setIntervenientCC = new EventEmitter<any>();
2021-04-08 13:39:48 +01:00
@Output() clearPostEvent = new EventEmitter<any>();
2021-02-24 09:14:58 +01:00
2021-06-23 15:39:45 +01:00
showLoader = false
@ViewChild('picker') picker: any;
@ViewChild('fim') fim: any;
@ViewChild('inicio') inicio: any;
@ViewChild('picker1') picker1: any;
public options = [
{ value: true, label: 'True' },
{ value: false, label: 'False' }
];
public listColors = ['primary', 'accent', 'warn'];
public stepHours = [1, 2, 3, 4, 5];
public stepMinutes = [1, 5, 10, 15, 20, 25];
public stepSeconds = [1, 5, 10, 15, 20, 25];
2021-09-03 12:19:21 +01:00
private participantsPipe = new ParticipantsPipe()
2021-02-24 09:14:58 +01:00
constructor(
private modalController: ModalController,
private eventsService: EventsService,
public alertController: AlertController,
2021-04-19 18:03:41 +01:00
private attachmentsService: AttachmentsService,
2021-09-03 12:19:21 +01:00
private toastService: ToastService,
2021-09-27 16:23:41 +01:00
) {}
2021-02-24 09:14:58 +01:00
ngOnInit() {
2021-08-31 15:05:42 +01:00
2021-06-30 14:44:48 +01:00
if(!this.restoreTemporaryData()) {
// clear
2021-07-12 16:39:55 +01:00
2021-06-30 14:44:48 +01:00
if(this.postEvent) {
2021-04-19 18:03:41 +01:00
if( this.postEvent.Body){
if(typeof(this.postEvent.Body.Text) == 'string'){
this.postEvent.Body.Text = this.postEvent.Body.Text.replace(/<[^>]+>/g, '');
}
}
}
2021-07-12 16:39:55 +01:00
2021-09-03 12:19:21 +01:00
const result = this.participantsPipe.transform(this.postEvent.Attendees)
this.taskParticipants = result.taskParticipants
this.taskParticipantsCc = result.taskParticipantsCc
2021-07-12 16:39:55 +01:00
this.taskParticipants = removeDuplicate(this.taskParticipants);
this.taskParticipantsCc = removeDuplicate(this.taskParticipantsCc);
this.setIntervenient.emit(this.taskParticipants);
this.setIntervenientCC.emit(this.taskParticipantsCc);
2021-07-12 16:39:55 +01:00
this.isEventEdited = false;
2021-07-12 16:39:55 +01:00
2021-06-30 09:45:56 +01:00
if(this.postEvent.IsRecurring == false) {
this.isRecurring = "Não se repete";
}
else{
this.isRecurring = "Repete";
}
2021-02-24 09:14:58 +01:00
}
2021-04-19 18:03:41 +01:00
2021-10-06 09:28:58 +01:00
this.loadedEventAttachments = this.loadedEventAttachments.concat(this.postEvent.Attachments)
2021-07-12 16:39:55 +01:00
this.getRecurrenceTypes();
2021-07-13 15:20:16 +01:00
2021-07-15 16:09:30 +01:00
this.postEvent.EventRecurrence.Type = this.postEvent.EventRecurrence.Type.toString();
2021-09-03 12:19:21 +01:00
2021-07-12 16:39:55 +01:00
setTimeout(() => {
2021-07-15 16:09:30 +01:00
this.postEvent.EventRecurrence.Type = this.postEvent.EventRecurrence.Type.toString();
console.log( this.postEvent.EventRecurrence.Type);
2021-07-12 16:39:55 +01:00
}, 1000);
2021-07-13 09:31:42 +01:00
2021-02-24 09:14:58 +01:00
}
2021-07-12 16:39:55 +01:00
2021-06-23 15:39:45 +01:00
close() {
this.closeComponent.emit();
2021-04-08 13:39:48 +01:00
this.setIntervenient.emit([]);
this.setIntervenientCC.emit([]);
this.clearContact.emit();
this.deleteTemporaryData();
2021-02-24 09:14:58 +01:00
}
2021-04-08 13:39:48 +01:00
2021-07-12 16:39:55 +01:00
getRecurrenceTypes() {
this.eventsService.getRecurrenceTypes().subscribe(res=>{
console.log(res);
this.recurringTypes = res;
});
}
2021-07-01 16:23:47 +01:00
runValidation() {
this.validateFrom = true
}
injectValidation() {
2021-07-20 19:22:11 +01:00
if (typeof(this.postEvent.EventRecurrence.Type) == 'number') {
const str: any = this.postEvent.EventRecurrence.Type.toString()
this.postEvent.EventRecurrence.Type = str
}
2021-09-15 15:39:55 +01:00
2021-07-01 16:23:47 +01:00
this.Form = new FormGroup({
Subject: new FormControl(this.postEvent.Subject, [
Validators.required,
// Validators.minLength(4)
]),
Location: new FormControl(this.postEvent.Location, [
Validators.required,
]),
CalendarName: new FormControl(this.postEvent.CalendarName),
Categories: new FormControl(this.postEvent.Category, [
2021-07-01 16:23:47 +01:00
Validators.required
]),
2021-09-02 16:41:43 +01:00
dateOccurrence: new FormControl(this.postEvent.EventRecurrence.Type, [
2021-07-01 16:23:47 +01:00
Validators.required
2021-07-13 11:15:19 +01:00
]),
2021-07-08 13:41:27 +01:00
participantes: new FormControl(this.taskParticipants, [
2021-07-20 12:30:52 +01:00
// Validators.required
2021-07-01 16:23:47 +01:00
]),
2021-09-02 17:08:43 +01:00
Date: new FormControl( new Date(this.postEvent.StartDate).toLocaleString('pt') < new Date(this.postEvent.EndDate).toLocaleString('pt')? 'ok': null,[
2021-07-12 16:05:05 +01:00
Validators.required
]),
2021-07-01 16:23:47 +01:00
})
}
2021-07-13 11:15:19 +01:00
openInicio() {
let input: any = document.querySelector('#new-inicio')
if(input) {
console.log(input)
input.click()
}
}
openFim() {
let input: any = document.querySelector('#new-fim')
if(input) {
input.click()
}
}
openLastOccurrence() {
let input: any = document.querySelector('#last-occurrence')
if(input) {
input.click()
}
}
2021-07-15 16:09:30 +01:00
onSelectedRecurringChanged(ev:any){
console.log(ev);
if(ev.length > 1){
console.log(ev.filter(data => data != '-1'));
this.postEvent.EventRecurrence.Type = ev.filter(data => data != '-1');
}
if(ev.length == 0){
this.postEvent.EventRecurrence.Type = "-1";
}
}
2021-07-01 16:23:47 +01:00
2021-06-23 15:39:45 +01:00
async save() {
2021-07-01 16:23:47 +01:00
this.injectValidation()
this.runValidation()
2021-07-06 16:18:00 +01:00
if(this.Form.invalid) {
return false
}
2021-07-01 16:23:47 +01:00
2021-04-08 20:11:25 +01:00
this.postEvent.Attendees = this.taskParticipants.concat(this.taskParticipantsCc);
2021-04-08 13:39:48 +01:00
2021-07-20 12:30:52 +01:00
if(!this.postEvent.EventRecurrence.hasOwnProperty('Type')) {
this.postEvent.EventRecurrence.Type = '-1'
2021-09-03 12:19:21 +01:00
} else {
2021-07-20 12:30:52 +01:00
}
if(this.postEvent.EventRecurrence.Type == undefined) {
this.postEvent.EventRecurrence.Type = '-1'
}
2021-07-15 14:26:26 +01:00
this.showLoader = true;
2021-04-08 13:39:48 +01:00
await this.eventsService.editEvent(this.postEvent, 2, 3).subscribe(async () => {
2021-07-07 11:13:31 +01:00
if(this.initCalendarName != this.postEvent.CalendarName) {
2021-06-29 10:50:05 +01:00
let body = {
"EventId": this.postEvent.EventId,
"CalendarDestinationName": this.postEvent.CalendarName,
}
2021-07-14 16:49:56 +01:00
2021-07-13 17:01:59 +01:00
try {
await this.eventsService.changeAgenda(body).toPromise();
} catch (e) {}
2021-06-29 10:50:05 +01:00
}
this.showLoader = false;
2021-06-23 15:39:45 +01:00
this.toastService.successMessage()
},
error => {
this.showLoader = false
this.toastService.badRequest()
2021-02-24 09:14:58 +01:00
});
2021-04-08 13:39:48 +01:00
this.clearPostEvent.emit();
this.deleteTemporaryData();
2021-07-12 16:39:55 +01:00
2021-10-06 09:28:58 +01:00
await this.saveDocument()
this.close();
2021-07-09 12:50:03 +01:00
}
2021-10-06 09:28:58 +01:00
async saveDocument() {
2021-07-09 12:50:03 +01:00
2021-10-06 09:28:58 +01:00
console.log(this.loadedEventAttachments)
await this.loadedEventAttachments.forEach( async (e)=>{
2021-07-12 16:39:55 +01:00
2021-07-09 12:50:03 +01:00
const id: any = e.Id
const remove = e['remove']
2021-07-12 16:39:55 +01:00
2021-07-09 12:50:03 +01:00
if ( id == 'add') {
//data.selected
const DocumentToSave = {
SourceTitle: e.SourceName,
ParentId: this.postEvent.EventId,
Source: '1',
SourceId: e.SourceId,
ApplicationId: e.ApplicationId.toString(),
Id: '0',
Link: '',
SerialNumber: '',
};
this.attachmentsService.setEventAttachmentById(DocumentToSave).subscribe(()=>{
this.getAttachments(this.postEvent.EventId);
});
} else if(remove) {
this.attachmentsService.deleteEventAttachmentById(e.Id).subscribe( res=> {})
}
})
2021-04-08 13:39:48 +01:00
}
2021-04-07 15:13:31 +01:00
2021-04-08 13:39:48 +01:00
async addParticipants() {
this.saveTemporaryData();
2021-04-08 13:39:48 +01:00
this.openAttendeesComponent.emit({
type: "intervenient"
});
this.clearContact.emit();
2021-02-24 09:14:58 +01:00
}
2021-04-08 13:39:48 +01:00
async addParticipantsCc() {
this.saveTemporaryData();
2021-04-08 13:39:48 +01:00
this.openAttendeesComponent.emit({
type: "CC"
});
2021-02-24 09:14:58 +01:00
2021-03-25 15:18:12 +01:00
this.clearContact.emit();
2021-04-07 15:13:31 +01:00
}
2021-02-24 09:14:58 +01:00
2021-06-23 15:39:45 +01:00
saveTemporaryData() {
2021-07-12 16:39:55 +01:00
window['temp.path:/home/agenda/edit-event.component.ts'] = {
postEvent: this.postEvent,
eventBody: this.eventBody,
segment: this.segment
}
}
2021-06-23 15:39:45 +01:00
restoreTemporaryData(): boolean {
2021-07-12 16:39:55 +01:00
const restoredData = window['temp.path:/home/agenda/edit-event.component.ts']
if(JSON.stringify(restoredData) != "{}" && undefined != restoredData){
this.postEvent = restoredData.postEvent
this.eventBody = restoredData.eventBody
this.segment = restoredData.segment
2021-07-12 16:39:55 +01:00
return true;
} else {
return false;
}
2021-07-12 16:39:55 +01:00
}
deleteTemporaryData(){
window['temp.path:/home/agenda/edit-event.component.ts'] = {}
}
2021-04-19 18:03:41 +01:00
getAttachments(eventId: string){
this.attachmentsService.getAttachmentsById(eventId).subscribe(res=>{
this.loadedEventAttachments = res;
2021-07-12 16:39:55 +01:00
console.log('res', res);
2021-04-19 18:03:41 +01:00
});
}
2021-07-09 12:50:03 +01:00
deleteAttachment(attachmentID: string, index) {
2021-04-20 15:05:40 +01:00
2021-07-09 12:50:03 +01:00
const id: any = this.loadedEventAttachments[index].Id
2021-07-12 16:39:55 +01:00
2021-07-09 12:50:03 +01:00
if(id == 'add') {
this.loadedEventAttachments = this.loadedEventAttachments.filter((e,i)=> i!=index)
} else {
this.loadedEventAttachments[index]['remove'] = true
}
2021-04-20 16:06:31 +01:00
}
async getDoc() {
2021-07-29 15:57:08 +01:00
2021-04-20 16:06:31 +01:00
const modal = await this.modalController.create({
component: SearchPage,
2021-04-21 19:59:49 +01:00
cssClass: 'modal-width-100-width-background modal',
2021-04-20 16:06:31 +01:00
componentProps: {
type: 'AccoesPresidenciais & ArquivoDespachoElect',
showSearchInput: true,
select: true,
2021-04-20 16:06:31 +01:00
}
});
await modal.present();
modal.onDidDismiss().then( async (res)=>{
if(res){
const data = res.data;
2021-07-09 12:50:03 +01:00
const ApplicationIdDocumentToSave: any = {
SourceName: data.selected.Assunto,
2021-04-20 16:06:31 +01:00
ParentId: this.postEvent.EventId,
SourceId: data.selected.Id,
2021-07-09 12:50:03 +01:00
Stakeholders: false,
2021-04-20 16:06:31 +01:00
ApplicationId: data.selected.ApplicationType.toString(),
2021-07-09 12:50:03 +01:00
CreateDate: false,
// needed to attach this document
Id: 'add',
SourceTitle: data.selected.Assunto,
Source: '1',
2021-04-20 16:06:31 +01:00
Link: '',
SerialNumber: '',
2021-07-09 12:50:03 +01:00
}
console.log( this.loadedEventAttachments)
this.loadedEventAttachments.push(ApplicationIdDocumentToSave)
2021-04-20 16:06:31 +01:00
}
});
2021-04-20 15:05:40 +01:00
}
2021-04-20 16:06:31 +01:00
2021-02-24 09:14:58 +01:00
}