diff --git a/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.spec.ts b/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.spec.ts index 7598618624..455cb57bb4 100644 --- a/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.spec.ts +++ b/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.spec.ts @@ -10,6 +10,7 @@ import { FRIENDS, FIRSTFRIEND, SECONDFRIEND } from '@global-user/mocks/friends-m import { MatDialogModule } from '@angular/material/dialog'; import { MatCheckboxModule } from '@angular/material/checkbox'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; +import { HabitService } from '@global-service/habit/habit.service'; describe('HabitInviteFriendsPopUpComponent', () => { let component: HabitInviteFriendsPopUpComponent; @@ -26,11 +27,16 @@ describe('HabitInviteFriendsPopUpComponent', () => { userFriendsServiceMock.inviteFriendsToHabit = jasmine.createSpy('inviteFriendsToHabit').and.returnValue(of({})); userFriendsServiceMock.addedFriends = []; + const mockHabitService = { + getFriendsWithInvitations: jasmine.createSpy('getFriendsWithInvitations').and.returnValue(of({ page: [] })) + }; + beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ declarations: [HabitInviteFriendsPopUpComponent], imports: [HttpClientTestingModule, TranslateModule.forRoot(), MatDialogModule, MatCheckboxModule], providers: [ + { provide: HabitService, useValue: mockHabitService }, { provide: LocalStorageService, useValue: localStorageServiceMock }, { provide: Router, useValue: routerSpy }, { provide: MatSnackBarComponent, useValue: MatSnackBarMock }, @@ -58,24 +64,6 @@ describe('HabitInviteFriendsPopUpComponent', () => { expect(spy2).toHaveBeenCalled(); }); - describe('setFriendDisable', () => { - it('should return true if the friend is in the addedFriends list and invitationSent is false', () => { - const friendId = 1; - userFriendsServiceMock.addedFriends = [FIRSTFRIEND, SECONDFRIEND]; - component.invitationSent = false; - const result = component.setFriendDisable(friendId); - expect(result).toBe(false); - }); - - it('should return true if invitationSent is true, regardless of addedFriends', () => { - const friendId = 1; - userFriendsServiceMock.addedFriends = [FIRSTFRIEND, SECONDFRIEND]; - component.invitationSent = true; - const result = component.setFriendDisable(friendId); - expect(result).toBe(true); - }); - }); - xit('should update allAdd status', () => { component.friends = [FIRSTFRIEND, SECONDFRIEND]; component.updateAllAdd(); diff --git a/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.ts b/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.ts index fbe8f0213e..02364fc1de 100644 --- a/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.ts +++ b/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends-pop-up/habit-invite-friends-pop-up.component.ts @@ -7,6 +7,7 @@ import { Subject } from 'rxjs'; import { searchIcon } from 'src/app/main/image-pathes/places-icons'; import { takeUntil } from 'rxjs/operators'; import { MatSnackBarComponent } from '@global-errors/mat-snack-bar/mat-snack-bar.component'; +import { HabitService } from '@global-service/habit/habit.service'; @Component({ selector: 'app-habit-invite-friends-pop-up', @@ -23,11 +24,11 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy { allAdd = false; searchIcon = searchIcon; habitId: number; - invitationSent = false; constructor( private readonly userFriendsService: UserFriendsService, private readonly localStorageService: LocalStorageService, + private readonly habitService: HabitService, @Inject(MAT_DIALOG_DATA) public data: any, private readonly snackBar: MatSnackBarComponent, private readonly dialogRef: MatDialogRef @@ -48,8 +49,8 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy { } getFriends() { - this.userFriendsService - .getAllFriends() + this.habitService + .getFriendsWithInvitations(this.habitId, 0, 10) .pipe(takeUntil(this.destroyed$)) .subscribe((data: FriendArrayModel) => { this.friends = data.page; @@ -59,7 +60,7 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy { onFriendCheckboxChange(friendId: number, isChecked: boolean) { const friend = this.friends.find((f) => f.id === friendId); - if (friend) { + if (friend && !friend.hasInvitation) { friend.added = isChecked; this.toggleFriendSelection(friendId, isChecked); this.updateAllAdd(); @@ -81,13 +82,6 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy { if (this.habitId && this.selectedFriends.length) { this.userFriendsService.inviteFriendsToHabit(this.habitId, this.selectedFriends).subscribe({ next: () => { - this.invitationSent = true; - const updatedFriends = [ - ...this.data.friends, - ...this.selectedFriends.map((id) => this.friends.find((friend) => friend.id === id)) - ]; - - this.data.onFriendsUpdated(updatedFriends); this.dialogRef.close(); }, error: (error) => { @@ -98,14 +92,12 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy { } setFriendDisable(friendId: number): boolean { - const isAlreadyAdded = this.data.friends?.some(({ id }) => id === friendId); - const isRecentlyAdded = this.userFriendsService.addedFriends?.some(({ id }) => id === friendId); - - return isAlreadyAdded || this.invitationSent || isRecentlyAdded; + const friend = this.friends.find((f) => f.id === friendId); + return friend ? friend.hasInvitation : false; } setAllFriendsDisable(): boolean { - return this.invitationSent || this.userFriendsService.addedFriends?.length === this.friends?.length; + return this.friends.every((friend) => friend.hasInvitation); } updateAllAdd() { @@ -119,7 +111,8 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy { setAll(added: boolean) { this.allAdd = added; this.friends.forEach((friend) => { - if (!this.isFriendAddedAlready(friend.id)) { + if (!this.isFriendAddedAlready(friend.id) && !friend.hasInvitation) { + friend.added = added; this.toggleFriendSelection(friend.id, added); } }); diff --git a/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends.component.ts b/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends.component.ts index 8e0a7412fa..9093bb009e 100644 --- a/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends.component.ts +++ b/src/app/main/component/user/components/habit/add-new-habit/habit-invite-friends/habit-invite-friends.component.ts @@ -45,10 +45,7 @@ export class HabitInviteFriendsComponent implements OnInit, OnDestroy { hasBackdrop: true, data: { habitId: this.habitId, - friends: [...this.friends], - onFriendsUpdated: (newFriends: FriendProfilePicturesArrayModel[]) => { - this.friends = newFriends; - } + friends: [...this.friends] } }); diff --git a/src/app/main/component/user/models/friend.model.ts b/src/app/main/component/user/models/friend.model.ts index 36d858c5ff..4f9a1499a9 100644 --- a/src/app/main/component/user/models/friend.model.ts +++ b/src/app/main/component/user/models/friend.model.ts @@ -14,6 +14,7 @@ export interface FriendModel { chatId?: number; isOnline?: boolean; friendsChatDto?: { chatId?: number }; + hasInvitation?:boolean; } export type FriendStatus = 'FRIEND' | 'REQUEST' | 'REJECTED' | null; diff --git a/src/app/main/service/habit/habit.service.ts b/src/app/main/service/habit/habit.service.ts index 2301e34a4d..26fed39343 100644 --- a/src/app/main/service/habit/habit.service.ts +++ b/src/app/main/service/habit/habit.service.ts @@ -11,6 +11,7 @@ import { ToDoList } from '@global-user/models/to-do-list.interface'; import { CustomHabitDtoRequest, CustomHabit } from '@global-user/components/habit/models/interfaces/custom-habit.interface'; import { FriendProfilePicturesArrayModel } from '@global-user/models/friend.model'; import { FileHandle } from '@eco-news-models/create-news-interface'; +import { FriendArrayModel } from '../../component/user/models/friend.model'; @Injectable({ providedIn: 'root' @@ -80,6 +81,12 @@ export class HabitService { return this.http.get(`${habitLink}/${assignId}/friends/profile-pictures`); } + getFriendsWithInvitations(habitId: number, page: number, size: number): Observable { + return this.http.get( + `${habitLink}/friends?habitId=${habitId}&page=${page}&size=${size}` + ); + } + deleteCustomHabit(id: number): Observable { return this.http.delete(`${habitLink}/delete/${id}`); }