You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
front/src/app/components/share-modal/share-modal.component.ts

147 lines
3.8 KiB

import { CommonModule } from '@angular/common';
import {
Component,
EventEmitter,
Input,
OnDestroy,
OnInit,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Subject, Subscription } from 'rxjs';
import { FriendsService } from '../../services/friends/friends.service';
import { ModalService } from '../../services/modal/modal.service';
import { PinService } from '../../services/pin/pin.service';
@Component({
selector: 'app-share-modal',
standalone: true,
imports: [CommonModule, FormsModule],
templateUrl: './share-modal.component.html',
})
export class ShareModalComponent implements OnInit, OnDestroy {
modalId: string = 'share-modal';
isShareModalOpen = false;
private modalSub!: Subscription;
isFriendModalOpen: boolean = false;
hasAcceptedFriends: boolean = false;
hasPendingFriends: boolean = false;
searchTerm: string = '';
searchTermChanged = new Subject<string>();
listUser: any[] = [];
listFriend: any[] = [];
pinShares: any[] = [];
@Input() pinId!: string;
@Input() pinOpened!: EventEmitter<void>;
constructor(
private modalService: ModalService,
private friendService: FriendsService,
private pinService: PinService
) {}
ngOnInit() {
this.modalId = 'share-modal-' + this.pinId;
this.modalSub = this.modalService
.getModalState(this.modalId)
.subscribe((open) => {
this.isShareModalOpen = open;
if (open) {
this.getFriend();
}
});
this.pinOpened.subscribe(() => {
this.moveModalToBody();
});
}
ngOnDestroy() {
this.modalSub?.unsubscribe();
}
openShareModal() {
this.modalService.openModal(this.modalId);
}
closeShareModal() {
this.modalService.closeModal(this.modalId);
}
private moveModalToBody(): void {
const modal = document.getElementById(this.modalId);
if (modal && modal.parentElement !== document.body) {
document.body.appendChild(modal);
}
}
onSearchTermChange(value: string): void {
if (!this.listFriend) return;
if (value.trim() === '') {
this.listUser = [...this.listFriend];
} else {
this.listUser = this.listFriend.filter((friend) =>
friend.username?.toLowerCase().includes(value.toLowerCase())
);
}
}
protected getFriend() {
// Récupérer d'abord les partages du pin
this.pinService.getPinShares(this.pinId).subscribe((response: any) => {
this.pinShares = response.shares || [];
// Ensuite récupérer les amis
this.friendService.getFriend().subscribe((friends: any[]) => {
this.listFriend = [];
this.listUser = [];
// Récupérer les détails de chaque ami
friends.forEach((friend) => {
if (friend.status === 'accepted') {
this.friendService
.getFriendById(friend.friend_user_id)
.subscribe((userDetails: any) => {
const friendWithDetails = {
...friend,
username: userDetails.username,
isShared: this.pinShares.some(
(share) => share.user_id === friend.friend_user_id
),
};
this.listFriend.push(friendWithDetails);
this.listUser.push(friendWithDetails);
});
}
});
});
});
}
sharePin(friendId: string) {
if (!this.pinId) {
console.error('No pin ID available');
return;
}
this.pinService.sharePin(this.pinId, friendId).subscribe((data: any) => {
this.closeShareModal();
});
}
unsharePin(friendId: string) {
if (!this.pinId) {
console.error('No pin ID available');
return;
}
this.pinService.deletePinShare(this.pinId, friendId).subscribe(() => {
// Mettre à jour la liste des amis après la suppression
this.getFriend();
});
}
}