Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] #7874 When user opens pop up, the friends he already invited to habit are disabled #3494

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 '../../../../../../../service/habit/habit.service';

describe('HabitInviteFriendsPopUpComponent', () => {
let component: HabitInviteFriendsPopUpComponent;
Expand All @@ -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 },
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 '../../../../../../../service/habit/habit.service';

@Component({
selector: 'app-habit-invite-friends-pop-up',
Expand All @@ -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<HabitInviteFriendsPopUpComponent>
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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) => {
Expand All @@ -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() {
Expand All @@ -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);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
});

Expand Down
1 change: 1 addition & 0 deletions src/app/main/component/user/models/friend.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface FriendModel {
chatId?: number;
isOnline?: boolean;
friendsChatDto?: { chatId?: number };
hasInvitation?:boolean;
}

export type FriendStatus = 'FRIEND' | 'REQUEST' | 'REJECTED' | null;
Expand Down
7 changes: 7 additions & 0 deletions src/app/main/service/habit/habit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -80,6 +81,12 @@ export class HabitService {
return this.http.get<FriendProfilePicturesArrayModel[]>(`${habitLink}/${assignId}/friends/profile-pictures`);
}

getFriendsWithInvitations(habitId: number, page: number, size: number): Observable<FriendArrayModel> {
return this.http.get<FriendArrayModel>(
`${habitLink}/friends?habitId=${habitId}&page=${page}&size=${size}`
);
}

deleteCustomHabit(id: number): Observable<CustomHabitDtoRequest> {
return this.http.delete<CustomHabitDtoRequest>(`${habitLink}/delete/${id}`);
}
Expand Down
Loading