|
| 1 | +import { Component, OnInit, ViewChild, ViewEncapsulation } from '@angular/core'; |
| 2 | +import { DayService, WeekService, WorkWeekService, MonthService, AgendaService, ScheduleComponent, ActionEventArgs, EventSettingsModel, ReturnType } from '@syncfusion/ej2-angular-schedule'; |
| 3 | +import { forkJoin, Observable } from 'rxjs'; |
| 4 | +import { CrudService, EventData } from './crud.service'; |
| 5 | + |
| 6 | +@Component({ |
| 7 | + selector: 'app-root', |
| 8 | + templateUrl: './app.component.html', |
| 9 | + styleUrls: ['../styles.scss'], |
| 10 | + providers: [DayService, WeekService, WorkWeekService, MonthService, AgendaService], |
| 11 | + encapsulation: ViewEncapsulation.None |
| 12 | +}) |
| 13 | +export class AppComponent implements OnInit { |
| 14 | + @ViewChild('scheduleObj') scheduleObj: ScheduleComponent; |
| 15 | + |
| 16 | + public selectedDate: Date = new Date(2023, 4, 1); |
| 17 | + public eventSettings: EventSettingsModel = { |
| 18 | + fields: { |
| 19 | + id: 'id' |
| 20 | + } |
| 21 | + }; |
| 22 | + private $query = { skip: 0, take: 100 }; |
| 23 | + private isNavigation: boolean = false; |
| 24 | + |
| 25 | + constructor(private crudService: CrudService) { } |
| 26 | + |
| 27 | + ngOnInit(): void { |
| 28 | + this.crudService.eventData.subscribe((response: Array<EventData>) => { |
| 29 | + if (this.scheduleObj) { |
| 30 | + this.scheduleObj.eventSettings.dataSource = response; |
| 31 | + this.scheduleObj.hideSpinner(); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + onDataBinding(args: ReturnType): void { |
| 37 | + if (!(this.scheduleObj?.eventSettings.dataSource as Array<Record<string, any>>).length || this.isNavigation) { |
| 38 | + args.result = []; |
| 39 | + this.scheduleObj.showSpinner(); |
| 40 | + this.crudService.execute(this.$query); |
| 41 | + this.isNavigation = false; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + onNavigation(): void { |
| 46 | + this.isNavigation = true; |
| 47 | + } |
| 48 | + |
| 49 | + onActionBegin(args: ActionEventArgs): void { |
| 50 | + if (['eventCreate', 'eventChange', 'eventRemove'].indexOf(args.requestType) > -1) { |
| 51 | + args.cancel = true; |
| 52 | + const requests: Array<Observable<EventData>> = []; |
| 53 | + if (args.addedRecords.length) { |
| 54 | + args.addedRecords.forEach((data: any) => { |
| 55 | + requests.push(this.crudService.addEvent(data)); |
| 56 | + }); |
| 57 | + } |
| 58 | + if (args.changedRecords.length) { |
| 59 | + args.changedRecords.forEach((data: any) => { |
| 60 | + requests.push(this.crudService.updateEvent(data)); |
| 61 | + }); |
| 62 | + } |
| 63 | + if (args.deletedRecords.length) { |
| 64 | + args.deletedRecords.forEach((data: any) => { |
| 65 | + requests.push(this.crudService.deleteEvent(data)); |
| 66 | + }); |
| 67 | + } |
| 68 | + this.scheduleObj.showSpinner(); |
| 69 | + forkJoin(requests).subscribe({ |
| 70 | + next: () => { |
| 71 | + this.crudService.execute(this.$query); |
| 72 | + }, |
| 73 | + error: (error) => { |
| 74 | + this.scheduleObj.hideSpinner(); |
| 75 | + console.error(error); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments