Files
BeReadyFrontend/src/app/components/message-form/message-form.component.ts
T

67 lines
2.1 KiB
TypeScript

import {Component, inject, input} from '@angular/core';
import {IonicModule, LoadingController, ToastController} from "@ionic/angular";
import {FormControl, FormGroup, ReactiveFormsModule, Validators} from "@angular/forms";
import {addIcons} from "ionicons";
import {sendOutline} from 'ionicons/icons';
import {GetGroupDetailsDto, MessagesService} from "../../services/api";
import {firstValueFrom} from "rxjs";
addIcons({
"send-outline" : sendOutline
})
@Component({
selector: 'app-message-form',
templateUrl: './message-form.component.html',
styleUrls: ['./message-form.component.scss'],
imports: [
IonicModule,
ReactiveFormsModule
]
})
export class MessageFormComponent {
private messagesService = inject(MessagesService);
private loadCtrl = inject(LoadingController);
private toastCtrl = inject(ToastController);
groupId = input.required<GetGroupDetailsDto["id"]>();
messageForm: FormGroup = new FormGroup({
libelle: new FormControl<string>(null, [Validators.required]),
})
async sendMessageOnGroup(){
const loading = await this.loadCtrl.create({
message: 'Envoi...',
spinner: 'lines-sharp-small'
});
await loading.present();
if (this.messageForm.invalid) {
this.messageForm.reset();
const toast = await this.toastCtrl.create({
message: 'Message incorrect',
duration: 2000,
color: 'danger'
});
await toast.present();
return;
}
try {
await firstValueFrom(this.messagesService.sendMessageEndpoint(this.groupId(), this.messageForm.getRawValue()));
this.messageForm.reset();
}
catch {
const toast = await this.toastCtrl.create({
message: 'Impossible d\'envoyer le messages.',
duration: 2000,
color: 'danger'
});
await toast.present();
this.messageForm.reset();
}
await loading.dismiss();
}
}