Skip to content

Commit

Permalink
Merge pull request #2615 from ita-social-projects/Bugfix_friends_page
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
hnativlyubomyr authored Aug 18, 2023
2 parents 3fb3e7e + 55cb920 commit 1c3eb1f
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,4 @@ describe('ProfileService', () => {
req.flush(places);
});
});

describe('test for method which get six user friends for dashboard', () => {
it('should return six user friends', () => {
const userFriends = {
amountOfFriends: 30,
pagedFriends: {
currentPage: 1,
page: [],
totalElements: 6,
totalPages: 1
}
};

profileService.getUserFriends().subscribe((info) => {
expect(info.pagedFriends.totalElements).toBe(6);
});

const req = httpMock.expectOne(`${backUserLink}user/1111/sixUserFriends/`);
expect(req.request.method).toBe('GET');
req.flush(userFriends);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { LocalStorageService } from '@global-service/localstorage/local-storage.
import { ProfileStatistics } from '@user-models/profile-statistiscs';
import { EditProfileModel } from '@user-models/edit-profile.model';
import { LanguageService } from 'src/app/main/i18n/language.service';
import { UserFriendsInterface } from '../../../../../interface/user/user-friends.interface';
import { mainLink, mainUserLink } from '../../../../../links';

@Injectable({
Expand Down Expand Up @@ -53,11 +52,6 @@ export class ProfileService {
return this.http.get<EcoPlaces[]>(`${mainLink}favorite_place/`);
}

public getUserFriends(): Observable<UserFriendsInterface> {
this.setUserId();
return this.http.get<UserFriendsInterface>(`${mainUserLink}user/${this.userId}/sixUserFriends/`);
}

public getSocialImage(socialNetwork: string): string {
const value = socialNetwork;
let imgPath = this.icons.defaultIcon;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ describe('AllFriendsComponent', () => {
localStorageServiceMock.userIdBehaviourSubject = new BehaviorSubject(1111);
let userFriendsServiceMock: UserFriendsService;

userFriendsServiceMock = jasmine.createSpyObj('UserFriendsService', [
'getAllFriendsAndByName',
'getAllFriends',
'deleteFriend',
'addFriend'
]);
userFriendsServiceMock = jasmine.createSpyObj('UserFriendsService', ['getFriendsByName', 'getAllFriends', 'deleteFriend', 'addFriend']);
userFriendsServiceMock.getAllFriends = () => of(FRIENDS);
userFriendsServiceMock.getAllFriendsAndByName = () => of(FRIENDS);
userFriendsServiceMock.getFriendsByName = () => of(FRIENDS);
userFriendsServiceMock.deleteFriend = (idFriend) => of(FIRSTFRIEND);
userFriendsServiceMock.addFriend = (idFriend) => of(FIRSTFRIEND);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class AllFriendsComponent implements OnInit {
public getAllFriends(currentPage: number) {
this.isFetching = true;
this.userFriendsService
.getAllFriendsAndByName('', currentPage)
.getAllFriends(currentPage)
.pipe(takeUntil(this.destroy$))
.subscribe(
(data: FriendArrayModel) => {
Expand All @@ -60,7 +60,7 @@ export class AllFriendsComponent implements OnInit {
this.searchQuery = value;
this.searchMode = true;
this.userFriendsService
.getAllFriendsAndByName(value)
.getFriendsByName(value)
.pipe(takeUntil(this.destroy$))
.subscribe(
(data: FriendArrayModel) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class FriendProfileDashboardComponent implements OnInit, OnDestroy {
private getMutualFriends(page?: number): void {
this.isFetching = true;
this.userFriendsService
.getNewFriends(page)
.getNewFriends('', page)
.pipe(takeUntil(this.destroy$))
.subscribe((data) => {
this.numberAllMutualFriends = data.totalElements;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<div class="container">
<div>
<div *ngIf="amountOfFriends">
<p>{{ amountOfFriends }} {{ 'profile.friends.found-amount' | translate }}</p>
</div>
<div *ngIf="isPristine">
<div *ngIf="!searchQuery">
<div *ngIf="!searchMode || !isFetching" class="cards-list">
<div class="cards-box">
<div *ngFor="let item of recommendedFriends" class="user-card">
Expand All @@ -18,7 +15,10 @@
</div>
</div>

<div *ngIf="!isPristine">
<div *ngIf="searchQuery">
<div *ngIf="amountOfFriends">
<p>{{ amountOfFriends }} {{ 'profile.friends.found-amount' | translate }}</p>
</div>
<div *ngIf="!searchMode || !isFetching" class="cards-list">
<div class="cards-box">
<div *ngFor="let item of recommendedFriendsBySearch" class="user-card">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class RecommendedFriendsComponent implements OnInit {
public sizePage = 10;
public searchQuery = '';
public searchMode = false;
public isPristine = true;

readonly absent = 'assets/img/noNews.svg';
constructor(
private userFriendsService: UserFriendsService,
Expand All @@ -39,18 +39,17 @@ export class RecommendedFriendsComponent implements OnInit {
}

public findUserByName(value: string) {
this.isPristine = false;
this.searchQuery = value;
this.isFetching = true;
this.searchMode = true;
this.userFriendsService
.getNewFriends(this.currentPage, this.sizePage, value)
.getNewFriends(value)
.pipe(takeUntil(this.destroy$))
.subscribe(
(data: FriendArrayModel) => {
this.emptySearchList = !data.page.length;
this.recommendedFriendsBySearch = data.page;
this.amountOfFriends = data.totalElements;
this.amountOfFriends = this.recommendedFriendsBySearch.length;
this.isFetching = false;
this.searchMode = false;
},
Expand All @@ -70,7 +69,7 @@ export class RecommendedFriendsComponent implements OnInit {
public getNewFriends(currentPage: number) {
this.isFetching = true;
this.userFriendsService
.getNewFriends(currentPage)
.getNewFriends('', currentPage)
.pipe(takeUntil(this.destroy$))
.subscribe(
(data: FriendArrayModel) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { TranslateModule } from '@ngx-translate/core';
import { BehaviorSubject, of, throwError } from 'rxjs';
import { ProfileService } from '@global-user/components/profile/profile-service/profile.service';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { Friend, UserFriendsInterface } from '../../../../../interface/user/user-friends.interface';
import { UserFriendsService } from '@global-user/services/user-friends.service';
import { FriendArrayModel, FriendModel } from '@global-user/models/friend.model';
import { FRIENDS } from '@global-user/mocks/friends-mock';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { LocalStorageService } from '@global-service/localstorage/local-storage.service';
import { CorrectUnitPipe } from 'src/app/shared/correct-unit-pipe/correct-unit.pipe';
import { FirstStringWordPipe } from '@pipe/first-string-word/first-string-word.pipe';
import { Language } from 'src/app/main/i18n/Language';

describe('UsersFriendsComponent', () => {
Expand All @@ -24,27 +27,19 @@ describe('UsersFriendsComponent', () => {
localStorageServiceMock.getCurrentLanguage = () => 'en' as Language;
localStorageServiceMock.languageSubject = of('en');

let profileServiceMock: ProfileService;
const userFriends = {
amountOfFriends: 30,
pagedFriends: {
currentPage: 1,
page: [],
totalElements: 6,
totalPages: 1
}
};
profileServiceMock = jasmine.createSpyObj('ProfileService', ['getUserFriends']);
profileServiceMock.getUserFriends = () => of(userFriends);
let userFriendsServiceMock: UserFriendsService;

userFriendsServiceMock = jasmine.createSpyObj('UserFriendsService', ['getAllFriends']);
userFriendsServiceMock.getAllFriends = () => of(FRIENDS);

beforeEach(async(() => {
TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
declarations: [UsersFriendsComponent, CorrectUnitPipe],
declarations: [UsersFriendsComponent, CorrectUnitPipe, FirstStringWordPipe],
imports: [TranslateModule.forRoot(), HttpClientTestingModule, RouterTestingModule.withRoutes([])],
providers: [
{ provide: LocalStorageService, useValue: localStorageServiceMock },
{ provide: ProfileService, useValue: profileServiceMock }
{ provide: UserFriendsService, useValue: userFriendsServiceMock }
]
}).compileComponents();
}));
Expand Down Expand Up @@ -77,7 +72,7 @@ describe('UsersFriendsComponent', () => {

it('should set message to error message', () => {
const error = 'Error message';
spyOn(profileServiceMock, 'getUserFriends').and.returnValue(throwError(error));
spyOn(userFriendsServiceMock, 'getAllFriends').and.returnValue(throwError(error));
component.showUsersFriends();
expect(component.noFriends).toBe('Error message');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Friend, UserFriendsInterface } from './../../../../../interface/user/user-friends.interface';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { LocalStorageService } from '@global-service/localstorage/local-storage.service';
import { ProfileService } from '@global-user/components/profile/profile-service/profile.service';
import { UserFriendsService } from '@global-user/services/user-friends.service';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { ActivatedRoute, Router } from '@angular/router';
import { FriendArrayModel, FriendModel } from '@global-user/models/friend.model';

@Component({
selector: 'app-users-friends',
Expand All @@ -20,7 +20,7 @@ export class UsersFriendsComponent implements OnInit, OnDestroy {
public currentLang: string;

constructor(
private profileService: ProfileService,
private userFriendsService: UserFriendsService,
private localStorageService: LocalStorageService,
private router: Router,
private route: ActivatedRoute
Expand All @@ -33,13 +33,13 @@ export class UsersFriendsComponent implements OnInit, OnDestroy {
}

public showUsersFriends(): void {
this.profileService
.getUserFriends()
this.userFriendsService
.getAllFriends(0, 6)
.pipe(takeUntil(this.destroy$))
.subscribe(
(item: UserFriendsInterface) => {
this.usersFriends = item.pagedFriends.page;
this.amountOfFriends = item.amountOfFriends;
(item: FriendArrayModel) => {
this.usersFriends = item.page;
this.amountOfFriends = item.page.length;
},
(error) => {
this.noFriends = error;
Expand All @@ -51,7 +51,7 @@ export class UsersFriendsComponent implements OnInit, OnDestroy {
this.localStorageService.userIdBehaviourSubject.pipe(takeUntil(this.destroy$)).subscribe((userId: number) => (this.userId = userId));
}

public showFriendsInfo(friend: Friend): void {
public showFriendsInfo(friend: FriendModel): void {
this.router.navigate(['friends', friend.name, friend.id], { relativeTo: this.route });
}

Expand Down
32 changes: 5 additions & 27 deletions src/app/main/component/user/services/user-friends.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,6 @@ describe('UserFriendsService', () => {
expect(service).toBeTruthy();
});

describe('getSixFriends', () => {
it('should return an SixFriendArrayModel', () => {
const userFriends = {
amountOfFriends: 30,
pagedFriends: {
currentPage: 1,
page: [],
totalElements: 6,
totalPages: 1
}
};

userFriendsService.getSixFriends(4).subscribe((users) => {
expect(users.pagedFriends.page.length).toBe(0);
});

const req = httpMock.expectOne(`${userFriendsService.url}user/4/sixUserFriends/`);
expect(req.request.method).toBe('GET');
req.flush(userFriends);
});
});

describe('getAllFriends', () => {
it('should return an FriendArrayModel', () => {
const recommendedFriends = {
Expand Down Expand Up @@ -97,7 +75,7 @@ describe('UserFriendsService', () => {
}
]
};
userFriendsService.getNewFriends(0).subscribe((users) => {
userFriendsService.getNewFriends('', 0).subscribe((users) => {
expect(users.page.length).toBe(2);
});

Expand Down Expand Up @@ -203,7 +181,7 @@ describe('UserFriendsService', () => {
}
]
};
userFriendsService.getNewFriends(0, 10, friends.page[0].name).subscribe((users) => {
userFriendsService.getNewFriends(friends.page[0].name, 0, 10).subscribe((users) => {
expect(users.page.length).toBeGreaterThanOrEqual(2);
});

Expand All @@ -213,8 +191,8 @@ describe('UserFriendsService', () => {
});
});

describe('getAllFriendsAndByName', () => {
it('should return an object on calling getAllFriendsAndByName', () => {
describe('getFriendsByName', () => {
it('should return an object on calling getFriendsByName', () => {
const friends = {
totalElements: 0,
totalPages: 0,
Expand All @@ -227,7 +205,7 @@ describe('UserFriendsService', () => {
}
]
};
userFriendsService.getAllFriendsAndByName(friends.page[0].name).subscribe((users) => {
userFriendsService.getFriendsByName(friends.page[0].name).subscribe((users) => {
expect(users).toBeTruthy();
});

Expand Down
10 changes: 3 additions & 7 deletions src/app/main/component/user/services/user-friends.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@environment/environment';
import { EditProfileModel } from '@global-user/models/edit-profile.model';
import { FriendArrayModel, FriendModel, SixFriendArrayModel } from '@global-user/models/friend.model';
import { FriendArrayModel, FriendModel } from '@global-user/models/friend.model';
import { ProfileStatistics } from '@global-user/models/profile-statistiscs';
import { Observable } from 'rxjs';

Expand Down Expand Up @@ -38,19 +38,15 @@ export class UserFriendsService {
return this.http.get<FriendArrayModel>(`${this.urlFriend}friends/friendRequests?page=${page}&size=${size}`);
}

public getSixFriends(userId: number): Observable<SixFriendArrayModel> {
return this.http.get<SixFriendArrayModel>(`${this.url}user/${userId}/sixUserFriends/`);
}

public getAllFriends(page = 0, size = this.size): Observable<FriendArrayModel> {
return this.http.get<FriendArrayModel>(`${this.urlFriend}friends?page=${page}&size=${size}`);
}

public getNewFriends(page = 0, size = this.size, name = ''): Observable<FriendArrayModel> {
public getNewFriends(name = '', page = 0, size = this.size): Observable<FriendArrayModel> {
return this.http.get<FriendArrayModel>(`${this.urlFriend}friends/not-friends-yet?name=${name}&page=${page}&size=${size}`);
}

public getAllFriendsAndByName(name = '', page = 0, size = this.size): Observable<FriendArrayModel> {
public getFriendsByName(name: string, page = 0, size = this.size): Observable<FriendArrayModel> {
return this.http.get<FriendArrayModel>(`${this.urlFriend}friends?name=${name}&page=${page}&size=${size}`);
}

Expand Down
4 changes: 2 additions & 2 deletions src/app/main/model/goal/HabitAssignCustomPropertiesDto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export class HabitAssignCustomPropertiesDto {
export interface HabitAssignCustomPropertiesDto {
friendsIdsList: Array<number>;
habitAssignPropertiesDto: HabitAssignPropertiesDto;
}

export class HabitAssignPropertiesDto {
export interface HabitAssignPropertiesDto {
defaultShoppingListItems: Array<number>;
duration: number;
}
2 changes: 1 addition & 1 deletion src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
},
"open": "Open",
"registered": "Anyone registered",
"completed": "Event completed",
"completed": "Closed",
"people-joined": "people are going",
"btn": {
"back-route": "Back to Events",
Expand Down
4 changes: 2 additions & 2 deletions src/assets/i18n/ua.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@
"my-space": {
"tag-title": "Мої події"
},
"open": "Відкрито",
"open": "Відкритий",
"registered": "Є зареєстровані",
"completed": "Подія завершена",
"completed": "Закритий",
"people-joined": "людей приєдналося",
"btn": {
"back-route": "Повернутися",
Expand Down

0 comments on commit 1c3eb1f

Please sign in to comment.