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

564 lines
14 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core';
2021-07-02 11:19:52 +01:00
import { FormControl, FormGroup, Validators } from '@angular/forms';
2021-06-29 10:50:05 +01:00
import { Router } from '@angular/router';
2022-04-26 16:14:55 +01:00
import { AlertController, ModalController, NavParams } from '@ionic/angular';
2021-04-23 10:35:53 +01:00
import { Attachment } from 'src/app/models/attachment.model';
import { EventBody } from 'src/app/models/eventbody.model';
import { EventPerson } from 'src/app/models/eventperson.model';
2021-04-23 10:35:53 +01:00
import { AttachmentsService } from 'src/app/services/attachments.service';
import { EventsService } from 'src/app/services/events.service';
2021-06-15 15:09:20 +01:00
import { ToastService } from 'src/app/services/toast.service';
import { Event } from '../../../models/event.model';
2021-06-15 15:28:03 +01:00
import { AttendeesPageModal } from '../../events/attendees/attendees.page';
2021-04-23 10:35:53 +01:00
import { SearchPage } from '../../search/search.page';
2022-01-21 15:54:53 +01:00
import { ThemeService } from 'src/app/services/theme.service';
import { NgxMatDateFormats } from '@angular-material-components/datetime-picker';
import { NGX_MAT_DATE_FORMATS } from '@angular-material-components/datetime-picker';
2022-04-02 14:51:57 +01:00
import { SessionStore } from 'src/app/store/session.service';
2022-01-21 15:54:53 +01:00
const CUSTOM_DATE_FORMATS: NgxMatDateFormats = {
parse: {
dateInput: "YYYY-MMMM-DD HH:mm"
},
display: {
dateInput: "DD MMM YYYY H:mm",
monthYearLabel: "MMM YYYY",
dateA11yLabel: "LL",
monthYearA11yLabel: "MMMM YYYY"
}
}
@Component({
selector: 'app-edit-event',
templateUrl: './edit-event.page.html',
styleUrls: ['./edit-event.page.scss'],
2022-01-21 15:54:53 +01:00
providers: [
{ provide: NGX_MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMATS },
]
})
export class EditEventPage implements OnInit {
2021-07-02 11:19:52 +01:00
Form: FormGroup;
validateFrom = false
2021-07-13 12:05:17 +01:00
postEvent: Event;
isRecurring:string;
isEventEdited: boolean;
loadedEvent: Event;
eventBody: EventBody;
segment:string = "true";
profile:string;
eventAttendees: EventPerson[];
selectedSegment: string;
selectedDate: Date;
2021-06-29 10:50:05 +01:00
initCalendarName: string;
caller:string;
2021-07-08 21:31:14 +01:00
recurringTypes: any;
selectedRecurringType: any;
2022-01-21 15:54:53 +01:00
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).toISOString().slice(0,10)
public maxDate: any;
public stepHour = 1;
public stepMinute = 15;
public stepSecond = 15;
currentDate = this.roundTimeQuarterHour();
2022-01-21 15:54:53 +01:00
2021-07-09 12:35:11 +01:00
loadedEventAttachments: Attachment[] = [];
2021-04-06 11:28:46 +01:00
taskParticipants: any = [];
taskParticipantsCc: any = [];
adding: "intervenient" | "CC" = "intervenient";
2021-04-09 13:57:31 +01:00
showAttendees = false;
2022-01-21 15:54:53 +01:00
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];
2022-04-02 14:51:57 +01:00
sesseionStora = SessionStore
constructor(
private modalController: ModalController,
private navParams: NavParams,
private eventsService: EventsService,
public alertController: AlertController,
2021-04-23 10:35:53 +01:00
private attachmentsService: AttachmentsService,
2021-06-15 15:09:20 +01:00
private toastService: ToastService,
2021-06-29 10:50:05 +01:00
private router: Router,
2021-10-22 15:43:57 +01:00
public ThemeService: ThemeService
2021-04-07 10:04:58 +01:00
) {
2021-07-01 11:26:45 +01:00
2022-01-21 15:54:53 +01:00
/* this.postEvent = new Event(); */
this.isEventEdited = false;
2022-01-21 15:54:53 +01:00
/* this.postEvent.EventRecurrence = { Type:'-1', LastOccurrence:''}; */
2021-07-09 14:39:45 +01:00
this.postEvent = this.navParams.get('event');
2022-06-29 15:51:28 +01:00
// console.log(this.postEvent);
2022-01-21 15:54:53 +01:00
2021-06-29 10:50:05 +01:00
this.caller = this.navParams.get('caller');
this.initCalendarName = this.postEvent.CalendarName;
2021-04-23 10:35:53 +01:00
2021-04-08 09:14:28 +01:00
if(this.postEvent){
2021-04-23 10:35:53 +01:00
if( this.postEvent.Body){
if(typeof(this.postEvent.Body.Text) == 'string'){
this.postEvent.Body.Text = this.postEvent.Body.Text.replace(/<[^>]+>/g, '');
}
}
2021-04-08 09:14:28 +01:00
}
2021-04-06 11:28:46 +01:00
2021-05-06 13:32:54 +01:00
if(this.postEvent.Attendees == null) {
2021-04-06 11:28:46 +01:00
this.taskParticipants = []
} else {
2021-04-08 13:39:48 +01:00
this.postEvent.Attendees.forEach(e =>{
if(e.IsRequired){
this.taskParticipants.push(e);
} else {
this.taskParticipantsCc.push(e);
}
})
2021-04-06 11:28:46 +01:00
}
2021-04-12 10:01:43 +01:00
if(this.postEvent.IsRecurring == false) {
this.isRecurring = "Não se repete";
}
2021-04-12 10:01:43 +01:00
else {
this.isRecurring = "Repete";
}
2021-07-13 12:05:17 +01:00
2021-04-23 10:35:53 +01:00
this.getAttachments(this.postEvent.EventId);
2021-07-13 12:05:17 +01:00
}
ngOnInit() {
2022-01-21 15:54:53 +01:00
2021-02-24 20:23:15 +01:00
window.onresize = (event) => {
// if not mobile remove all component
2021-05-06 13:32:54 +01:00
if( window.innerWidth >= 1024) {
2021-02-24 20:23:15 +01:00
this.modalController.dismiss();
}
2021-05-06 13:32:54 +01:00
}
2021-06-10 15:38:08 +01:00
if(window.innerWidth > 800){
this.showAttendees=true;
}
2021-07-08 13:20:54 +01:00
this.getRecurrenceTypes();
2021-07-12 11:20:08 +01:00
setTimeout(() => {
this.selectedRecurringType = this.postEvent.EventRecurrence.Type.toString();
}, 500);
2021-07-13 12:05:17 +01:00
}
2021-07-13 12:05:17 +01:00
2021-04-12 10:01:43 +01:00
close() {
this.modalController.dismiss();
}
2021-04-07 11:52:28 +01:00
2021-06-29 10:50:05 +01:00
goBack() {
2022-04-28 09:32:27 +01:00
2021-06-29 10:50:05 +01:00
this.router.navigate(['/home',this.caller]);
}
2021-04-08 13:39:48 +01:00
2021-07-08 13:20:54 +01:00
getRecurrenceTypes() {
this.eventsService.getRecurrenceTypes().subscribe(res=>{
2022-04-28 09:32:27 +01:00
2021-07-08 21:31:14 +01:00
this.recurringTypes = res;
2021-07-08 13:20:54 +01:00
});
}
roundTimeQuarterHour() {
var timeToReturn = new Date();
// var minutes = timeToReturn.getMinutes();
var minutes = timeToReturn.getMinutes();
var hours = timeToReturn.getHours();
// console.log("MINUTOS: " +minutes);
// console.log("BEFORE MINUTES: " +(Math.round(minutes/15) * 15));
var m = (Math.round(minutes/15) * 15) % 60;
// console.log("AFTER MINUTES: " +m);
var h = minutes > 52 ? (hours === 23 ? 0 : ++hours) : hours;
if (m == 0) {
if(minutes > m){
m = m + 15;
}
timeToReturn.setHours(h);
timeToReturn.setMinutes(m);
}else{
if(minutes > m){
m = m + 15;
timeToReturn.setHours(h);
timeToReturn.setMinutes(m);
}else {
timeToReturn.setHours(h);
timeToReturn.setMinutes(m);
}
}
}
2022-04-20 14:41:02 +01:00
onSelectedRecurringChanged(ev?:any) {
this.calculetedLastOccurrence(ev);
2022-04-28 09:32:27 +01:00
2021-07-08 21:31:14 +01:00
if(ev.length > 1){
2022-04-28 09:32:27 +01:00
2021-07-08 21:31:14 +01:00
this.selectedRecurringType = ev.filter(data => data != '-1');
}
if(ev.length == 0){
this.selectedRecurringType = "-1";
}
}
calculetedLastOccurrence(type:number){
2022-06-29 15:51:28 +01:00
// console.log(type);
var valor;
var opcao: boolean;
if (type == 0) {
valor = 7;
opcao = true;
} else if(type == 1){
valor = 30;
opcao = true;
} else if(type == 2){
valor = 1;
opcao = false;
}else if(type == 3){
valor = 5;
opcao = false;
}
this.defineLastOccurrence(valor, opcao);
}
defineLastOccurrence(valor:number, opcao:boolean){
var time = new Date(this.postEvent.EndDate);
if (opcao == true) {
time.setDate(time.getDate() + valor);
this.postEvent.EventRecurrence.LastOccurrence = time;
} else {
time = new Date(
time.getFullYear() + valor,
time.getMonth(),
time.getDate(),
time.getHours(),
time.getMinutes()
);
this.postEvent.EventRecurrence.LastOccurrence = time;
}
}
2021-07-02 11:19:52 +01:00
runValidation() {
this.validateFrom = true
}
injectValidation() {
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, [
Validators.required
]),
Categories: new FormControl(this.postEvent.Category, [
2021-07-02 11:19:52 +01:00
Validators.required
]),
IsRecurring: new FormControl(this.postEvent.IsRecurring, [
Validators.required
]),
2022-07-06 09:12:57 +01:00
dateOccurrence: new FormControl(this.postEvent.EventRecurrence.Type.toString() == '-1' ? ['ok']: this.postEvent.EventRecurrence.LastOccurrence && new Date(this.postEvent.EventRecurrence.LastOccurrence).getTime() > new Date(this.postEvent.EndDate).getTime() ? 'ok': null, [
Validators.required
]),
2022-04-21 16:45:17 +01:00
Date: new FormControl(new Date(this.postEvent.StartDate).getTime() <= new Date(this.postEvent.EndDate).getTime()? 'ok': null,[
2021-07-13 16:02:14 +01:00
Validators.required
]),
2021-07-13 12:05:17 +01:00
2021-07-13 16:02:14 +01:00
participantes: new FormControl(this.taskParticipants, [
Validators.required
]),
2021-07-02 11:19:52 +01:00
})
}
2021-07-13 12:05:17 +01:00
2022-01-21 15:54:53 +01:00
openInicio() {
let input: any = document.querySelector('#new-inicio')
if(input) {
2022-04-28 09:32:27 +01:00
2022-01-21 15:54:53 +01:00
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-02 11:19:52 +01:00
2021-06-29 10:50:05 +01:00
save() {
2021-07-13 12:05:17 +01:00
2021-07-02 11:19:52 +01:00
this.injectValidation()
this.runValidation()
2021-07-09 14:39:45 +01:00
if(this.Form.invalid) return false;
if(this.selectedRecurringType != '-1'){
this.postEvent.EventRecurrence.Type = this.selectedRecurringType;
}
2021-07-13 12:05:17 +01:00
2021-04-08 13:39:48 +01:00
this.postEvent.Attendees = this.taskParticipants.concat(this.taskParticipantsCc)
2021-09-15 15:39:55 +01:00
2021-07-16 23:16:55 +01:00
this.postEvent.EventRecurrence.Type = this.selectedRecurringType;
2022-04-02 14:51:57 +01:00
if(this.sesseionStora.user.Profile == 'MDGPR' || this.sesseionStora.user.Profile == 'PR') {
this.eventsService.editEvent(this.postEvent, 2, 3).subscribe(async () => {
if(this.initCalendarName != this.postEvent.CalendarName) {
let body = {
"EventId": this.postEvent.EventId,
"CalendarDestinationName": this.postEvent.CalendarName,
}
try {
await this.eventsService.changeAgenda(body).toPromise();
} catch (error) {}
finally {
this.goBack();
}
2021-12-06 15:09:59 +01:00
}
2022-04-02 14:51:57 +01:00
this.toastService._successMessage();
2022-04-02 14:51:57 +01:00
}, error => {
2023-02-02 12:01:18 +01:00
if(error.status == 0) {
this.toastService._badRequest('sem conexão ao servidor')
} else {
this.toastService._badRequest()
}
2022-04-02 14:51:57 +01:00
});
} else {
this.eventsService.editEvent(this.postEvent, 2, 3, this.postEvent.CalendarId).subscribe(async () => {
if(this.initCalendarName != this.postEvent.CalendarName) {
let body = {
"EventId": this.postEvent.EventId,
"CalendarDestinationName": this.postEvent.CalendarName,
}
try {
await this.eventsService.changeAgenda(body).toPromise();
} catch (error) {}
finally {
this.goBack();
}
2021-12-06 15:09:59 +01:00
}
2022-04-02 14:51:57 +01:00
this.toastService._successMessage();
2022-04-02 14:51:57 +01:00
}, error => {
2023-02-02 12:01:18 +01:00
if(error.status == 0) {
this.toastService._badRequest('sem conexão ao servidor')
} else {
this.toastService._badRequest()
}
2022-04-02 14:51:57 +01:00
});
}
2022-01-21 15:54:53 +01:00
2022-04-02 14:51:57 +01:00
2021-05-24 16:49:25 +01:00
2021-07-16 23:16:55 +01:00
this.isEventEdited = true;
2021-09-15 15:39:55 +01:00
2021-07-16 23:16:55 +01:00
this.modalController.dismiss(this.isEventEdited);
2021-05-24 16:49:25 +01:00
2021-07-09 12:35:11 +01:00
this.saveDocument()
}
saveDocument() {
2022-04-20 14:41:02 +01:00
this.loadedEventAttachments.forEach((e) => {
2021-07-13 12:05:17 +01:00
2021-07-09 12:35:11 +01:00
const id: any = e.Id
const remove = e['remove']
2021-07-13 12:05:17 +01:00
2021-07-09 12:35:11 +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-07 10:04:58 +01:00
async openAttendees() {
2022-04-20 14:41:02 +01:00
if(window.innerWidth > 801) {
2021-06-10 15:38:08 +01:00
this.showAttendees=true;
}
2022-04-20 14:41:02 +01:00
else {
2021-06-10 15:38:08 +01:00
const modal = await this.modalController.create({
2021-06-15 15:28:03 +01:00
component: AttendeesPageModal,
2021-06-10 15:38:08 +01:00
componentProps: {
adding: this.adding,
taskParticipants: this.taskParticipants,
taskParticipantsCc: this.taskParticipantsCc
},
2021-08-30 14:32:15 +01:00
cssClass: 'modal attendee modal-desktop',
2021-06-10 15:38:08 +01:00
backdropDismiss: false
});
2021-06-10 15:38:08 +01:00
await modal.present();
2021-04-07 11:52:28 +01:00
2021-06-10 15:38:08 +01:00
modal.onDidDismiss().then((data) => {
2021-07-13 12:05:17 +01:00
2021-06-10 15:38:08 +01:00
if(data){
data = data['data'];
2021-05-04 15:44:48 +01:00
2021-06-10 15:38:08 +01:00
const newAttendees: EventPerson[] = data['taskParticipants'];
const newAttendeesCC: EventPerson[] = data['taskParticipantsCc'];
2021-05-04 15:44:48 +01:00
2021-08-30 14:32:15 +01:00
if(newAttendees.length) {
this.setIntervenient(newAttendees);
2022-04-20 14:41:02 +01:00
} else {
this.setIntervenient([]);
2021-08-30 14:32:15 +01:00
}
if(newAttendeesCC) {
this.setIntervenientCC(newAttendeesCC);
2022-04-20 14:41:02 +01:00
} else {
this.setIntervenientCC([]);
2021-08-30 14:32:15 +01:00
}
2021-09-15 15:39:55 +01:00
2021-06-10 15:38:08 +01:00
}
});
}
2021-05-04 15:44:48 +01:00
2021-04-07 11:52:28 +01:00
2021-04-06 11:28:46 +01:00
}
2022-04-21 16:45:17 +01:00
setIntervenient(data) {
2021-04-06 11:28:46 +01:00
this.taskParticipants = data;
this.postEvent.Attendees = data;
}
2021-07-13 12:05:17 +01:00
2022-04-21 16:45:17 +01:00
setIntervenientCC(data) {
2021-04-06 11:28:46 +01:00
this.taskParticipantsCc = data;
}
2022-04-21 16:45:17 +01:00
addParticipants() {
2021-04-06 11:28:46 +01:00
this.adding = 'intervenient'
2021-04-07 10:04:58 +01:00
this.openAttendees();
2021-04-06 11:28:46 +01:00
}
2022-04-21 16:45:17 +01:00
addParticipantsCC() {
2021-06-29 10:50:05 +01:00
this.adding = 'CC';
2021-04-07 10:04:58 +01:00
this.openAttendees();
2021-04-06 11:28:46 +01:00
}
2021-04-08 13:39:48 +01:00
dynamicSetIntervenient({taskParticipants, taskParticipantsCc}){
this.taskParticipants = taskParticipants;
this.taskParticipantsCc = taskParticipantsCc;
}
2021-07-13 12:05:17 +01:00
2021-04-23 10:35:53 +01:00
getAttachments(eventId: string){
this.attachmentsService.getAttachmentsById(eventId).subscribe(res=>{
this.loadedEventAttachments = res;
2022-04-28 09:32:27 +01:00
2022-12-21 16:25:09 +01:00
},((erro) => {
console.error('editgetAttchament', erro)
}));
2021-04-23 10:35:53 +01:00
}
2021-07-09 12:35:11 +01:00
deleteAttachment(attachmentID: string, index) {
2021-04-23 10:35:53 +01:00
2021-07-09 12:35:11 +01:00
const id: any = this.loadedEventAttachments[index].Id
2021-07-13 12:05:17 +01:00
2021-07-09 12:35:11 +01:00
if(id == 'add') {
this.loadedEventAttachments = this.loadedEventAttachments.filter((e,i)=> i!=index)
} else {
this.loadedEventAttachments[index]['remove'] = true
}
2021-07-13 12:05:17 +01:00
2021-04-23 10:35:53 +01:00
}
async getDoc() {
const modal = await this.modalController.create({
component: SearchPage,
cssClass: 'modal-width-100-width-background modal',
componentProps: {
type: 'AccoesPresidenciais & ArquivoDespachoElect',
2021-05-03 17:24:48 +01:00
showSearchInput: true,
select: true,
2021-04-23 10:35:53 +01:00
}
});
await modal.present();
2021-07-09 12:35:11 +01:00
modal.onDidDismiss().then( async (res)=> {
if(res) {
2021-04-23 10:35:53 +01:00
const data = res.data;
2021-07-09 12:35:11 +01:00
const ApplicationIdDocumentToSave: any = {
SourceName: data.selected.Assunto,
2021-04-23 10:35:53 +01:00
ParentId: this.postEvent.EventId,
SourceId: data.selected.Id,
2021-07-09 12:35:11 +01:00
Stakeholders: false,
2021-04-23 10:35:53 +01:00
ApplicationId: data.selected.ApplicationType.toString(),
2021-07-09 12:35:11 +01:00
CreateDate: false,
// needed to attach this document
Id: 'add',
SourceTitle: data.selected.Assunto,
Source: '1',
2021-04-23 10:35:53 +01:00
Link: '',
SerialNumber: '',
2021-07-09 12:35:11 +01:00
}
2021-04-23 10:35:53 +01:00
2022-04-28 09:32:27 +01:00
2021-07-09 12:35:11 +01:00
this.loadedEventAttachments.push(ApplicationIdDocumentToSave)
2021-04-23 10:35:53 +01:00
}
2021-07-09 12:35:11 +01:00
})
2021-04-23 10:35:53 +01:00
}
2021-07-13 12:05:17 +01:00
}