import {Component, inject, input, output, signal} from '@angular/core'; import {NzNotificationService} from "ng-zorro-antd/notification"; import {FormControl, FormGroup, ReactiveFormsModule} from "@angular/forms"; import {firstValueFrom} from "rxjs"; import {NzCardComponent} from "ng-zorro-antd/card"; import {NzFormControlComponent, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form"; import {NzIconDirective} from "ng-zorro-antd/icon"; import {NzColDirective} from "ng-zorro-antd/grid"; import {NzInputDirective} from "ng-zorro-antd/input"; import {CommunicationsService, GetCommunicationDto, UpdateCommunicationDto} from "../../../services/api"; @Component({ selector: 'app-opportunity-card', imports: [ NzCardComponent, ReactiveFormsModule, NzFormLabelComponent, NzFormControlComponent, NzFormItemComponent, NzIconDirective, NzColDirective, NzInputDirective ], templateUrl: './opportunity-card.html', styleUrl: './opportunity-card.css', }) export class OpportunityCard { private communicationsService = inject(CommunicationsService); private notificationService = inject(NzNotificationService); communicationEdit = signal(null); communicationsLoading = signal(false); communicationForm = new FormGroup({ calling: new FormControl(null), email: new FormControl(null), meeting: new FormControl(null), }, { validators: atLeastOneRequired }) async submitForm() { this.communicationsLoading.set(true); const communicationValue: UpdateCommunicationDto = { calling: this.communicationForm.value.calling, email: this.communicationForm.value.email, meeting: this.communicationForm.value.meeting, } try { const communication = await firstValueFrom(this.communicationsService.updateCommunicationEndpoint(this.communication().id, communicationValue)); this.communicationEdit.set(communication as unknown as UpdateCommunicationDto); console.log(communication); this.communicationForm.reset(); this.edit.set(false); this.triggerEdited.emit(); } catch (e) { this.notificationService.error('Erreur', '(ou Erreur de communication avec l\'API)'); } this.communicationsLoading.set(false); } communication = input(null); edit = signal(false); triggerEdited = output(); protected Edit() { this.edit.set(true); this.communicationForm.patchValue({ calling: this.communication().calling, email: this.communication().email, meeting: this.communication().meeting, }); } protected Back() { this.edit.set(false); this.communicationForm.reset(); } async Delete() { this.communicationsLoading.set(true); try { await firstValueFrom(this.communicationsService.deleteCommunicationEndpoint(this.communication().id)); this.communicationForm.reset(); this.triggerEdited.emit(); } catch (e) { this.notificationService.error('Erreur', '(ou Erreur de communication avec l\'API)'); } this.communicationsLoading.set(false); } } function atLeastOneRequired(group: FormGroup) { const { calling, email, meeting } = group.controls; const valid = [calling, email, meeting].some(c => c.value); return valid ? null : { atLeastOneRequired: true }; }