import {Component, inject, input, OnDestroy, OnInit} from '@angular/core'; import {GetGroupDetailsDto, MessagesService} from "../../services/api"; import {firstValueFrom} from "rxjs"; import {IonicModule, LoadingController, ToastController} from "@ionic/angular"; import {addIcons} from "ionicons"; import {chatbubbleEllipsesOutline, moonOutline} from 'ionicons/icons'; import {ChatService} from "../../services/chat-service"; import {MessagesListComponent} from "../messages-list/messages-list.component"; import {MessageFormComponent} from "../message-form/message-form.component"; addIcons({ "chatbubble-ellipses-outline": chatbubbleEllipsesOutline, "moon-outline": moonOutline, }) @Component({ selector: 'app-message', templateUrl: './message.component.html', styleUrls: ['./message.component.scss'], imports: [ IonicModule, MessagesListComponent, MessageFormComponent ] }) export class MessageComponent implements OnInit, OnDestroy { private messagesService = inject(MessagesService); private loadCtrl = inject(LoadingController); private toastCtrl = inject(ToastController); private chatService = inject(ChatService); messages = this.chatService.messages; groupId = input.required(); async ngOnInit() { const loading = await this.loadCtrl.create({ message: 'Chargement...', spinner: 'lines-sharp-small' }); await loading.present(); try { const messages = await firstValueFrom(this.messagesService.getMessagesEndpoint(this.groupId())); this.chatService.setMessages(messages); await this.chatService.connect(this.groupId()); } catch { const toast = await this.toastCtrl.create({ message: 'Impossible de charger les messages.', duration: 2000, color: 'danger' }); await toast.present(); } await loading.dismiss(); } async ngOnDestroy() { await this.chatService.disconnect(); } }