import {Component, computed, inject, input, OnInit, output, signal} from '@angular/core'; import {GetGroupDetailsDto, GroupsService} from "../../services/api"; import {addIcons} from "ionicons"; import {peopleOutline, banOutline, arrowUpCircleOutline} from "ionicons/icons"; import {IonicModule, LoadingController, ToastController} from "@ionic/angular"; import moment from 'moment'; import {TitlePartComponent} from "../title-part/title-part.component"; import {firstValueFrom} from "rxjs"; import {PipeComponent} from "../pipe/pipe.component"; addIcons({ 'avatars': peopleOutline, 'ban': banOutline, 'arrow-up-circle-outline': arrowUpCircleOutline, }) @Component({ selector: 'app-group-info', templateUrl: './group-info.component.html', styleUrls: ['./group-info.component.scss'], imports: [ IonicModule, TitlePartComponent, PipeComponent ] }) export class GroupInfoComponent implements OnInit { private groupsService = inject(GroupsService); private toastCtrl = inject(ToastController); private loadCtrl = inject(LoadingController); groupSelected = input.required(); group = signal({}); date = computed(() => moment(this.group().creationDate).format('DD/MM/YYYY')); idGroup = output(); options = [1, 2]; ngOnInit() { this.group.set(this.groupSelected()); } async deleteUser(id: number) { const loading = await this.loadCtrl.create({ message: 'Suppression...', spinner: 'lines-sharp-small' }); await loading.present(); try { await firstValueFrom(this.groupsService.deleteUserFromGroupEndpoint(this.group().id, id)); this.group.update(x => ({ ...x, users: x.users.filter(u => u.id != id) })); const toast = await this.toastCtrl.create({ message: 'Utilisateur retiré du groupe', duration: 2000, color: 'success' }); await toast.present(); } catch (e) { const toast = await this.toastCtrl.create({ message: 'Vous devez être administrateur pour faire cela', duration: 2000, color: 'danger' }); await toast.present(); } await loading.dismiss(); } async leaveGroup() { const loading = await this.loadCtrl.create({ message: 'Chargement...', spinner: 'lines-sharp-small' }); await loading.present(); const oldGroup = this.group().id; try { await firstValueFrom(this.groupsService.leaveGroupEndpoint(oldGroup)); this.idGroup.emit(oldGroup); const toast = await this.toastCtrl.create({ message: 'Vous avez quitter ce groupe', duration: 2000, color: 'success' }); await toast.present(); } catch { const toast = await this.toastCtrl.create({ message: 'Impossible de quitter le groupe', duration: 2000, color: 'danger' }); await toast.present(); } await loading.dismiss(); } addToGroup() { } async deleteGroup() { const loading = await this.loadCtrl.create({ message: 'Suppression...', spinner: 'lines-sharp-small' }); await loading.present(); const oldGroup = this.group().id; try { await firstValueFrom(this.groupsService.deleteGroupEndpoint(oldGroup)); this.idGroup.emit(oldGroup); const toast = await this.toastCtrl.create({ message: 'Groupe supprimé', duration: 2000, color: 'success' }); await toast.present(); } catch (e) { const toast = await this.toastCtrl.create({ message: 'Vous devez être administrateur pour faire cela', duration: 2000, color: 'danger' }); await toast.present(); } await loading.dismiss(); } async promoteUser(id: number) { const loading = await this.loadCtrl.create({ message: 'Promotion...', spinner: 'lines-sharp-small' }); await loading.present(); try { const group = await firstValueFrom(this.groupsService.patchGroupUserRoleEndpoint(this.group().id, id)); this.group.set(group); const toast = await this.toastCtrl.create({ message: 'Utilisateur promu avec succès', duration: 2000, color: 'success' }); await toast.present(); } catch (e) { const toast = await this.toastCtrl.create({ message: 'Vous devez être administrateur pour faire cela', duration: 2000, color: 'danger' }); await toast.present(); } await loading.dismiss(); } }