Skip to content

Commit

Permalink
fix: issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kovalsofiia1 committed Nov 8, 2024
1 parent 1c948d2 commit 178517e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ describe('NewsListComponent', () => {
let component: NewsListComponent;
let fixture: ComponentFixture<NewsListComponent>;

const ecoNewsServiceMock: EcoNewsService = jasmine.createSpyObj('EcoNewsService', [
'getAllPresentTags',
'getNewsListByTags',
'getEcoNewsListByPage'
]);
const ecoNewsServiceMock: EcoNewsService = jasmine.createSpyObj('EcoNewsService', ['getAllPresentTags', 'getEcoNewsListByPage']);

ecoNewsServiceMock.getEcoNewsListByPage = () => new Observable();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Breakpoints } from '../../../../config/breakpoints.constants';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ReplaySubject } from 'rxjs';
import { take, takeUntil } from 'rxjs/operators';
import { Observable, of, ReplaySubject } from 'rxjs';
import { map, take, takeUntil } from 'rxjs/operators';
import { EcoNewsModel } from '@eco-news-models/eco-news-model';
import { FilterModel } from '@shared/components/tag-filter/tag-filter.model';
import { UserOwnAuthService } from '@auth-service/user-own-auth.service';
Expand Down Expand Up @@ -133,24 +133,25 @@ export class NewsListComponent implements OnInit, OnDestroy {
this.isSearchVisible = !this.isSearchVisible;
}

private checkAuthentication(): Observable<boolean> {
if (!this.userId) {
this.openAuthModalWindow('sign-in');
return this.dialogRef.afterClosed().pipe(
take(1),
map((result) => !!result)
);
}
return of(true);
}

changeFavoriteStatus(event: Event, data: EcoNewsModel) {
event.preventDefault();
event.stopPropagation();

let isRegistered = !!this.userId;
this.checkAuthentication();

if (!isRegistered) {
this.openAuthModalWindow('sign-in');
this.dialogRef
.afterClosed()
.pipe(take(1))
.subscribe((result) => {
isRegistered = !!result;
});
} else {
const action = ChangeEcoNewsFavoriteStatusAction({ id: data.id, favorite: !data.favorite, isFavoritesPage: this.bookmarkSelected });
this.store.dispatch(action);
}
const action = ChangeEcoNewsFavoriteStatusAction({ id: data.id, favorite: !data.favorite, isFavoritesPage: this.bookmarkSelected });
this.store.dispatch(action);
}

openAuthModalWindow(page: string): void {
Expand All @@ -165,20 +166,9 @@ export class NewsListComponent implements OnInit, OnDestroy {
}

showSelectedNews(): void {
let isRegistered = !!this.userId;

if (!isRegistered) {
this.openAuthModalWindow('sign-in');
this.dialogRef
.afterClosed()
.pipe(take(1))
.subscribe((result) => {
isRegistered = !!result;
});
} else {
this.bookmarkSelected = !this.bookmarkSelected;
this.dispatchStore(true);
}
this.checkAuthentication();
this.bookmarkSelected = !this.bookmarkSelected;
this.dispatchStore(true);
}

dispatchStore(res: boolean): void {
Expand All @@ -189,13 +179,11 @@ export class NewsListComponent implements OnInit, OnDestroy {
this.noNewsMatch = false;
this.newsTotal = 0;
}
console.log('in2');

if (!this.hasNext || this.loading) {
console.log(this.hasNext, this.loading);
return;
}
console.log('in3');

this.loading = true;
const params = this.ecoNewsService.getNewsHttpParams({
Expand Down
11 changes: 0 additions & 11 deletions src/app/main/component/eco-news/services/eco-news.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,6 @@ describe('EcoNewsService', () => {
req.flush(newsDtoMock);
});

it('should return news list', () => {
const arr = [newsMock];
service.getNewsList().subscribe((data) => {
expect(data).toBe(arr);
});

const req = httpTestingController.expectOne(`${environment.backendLink}eco-news`);
expect(req.request.method).toEqual('GET');
req.flush(arr);
});

it('should return news list by id', () => {
service.getEcoNewsById(13578).subscribe((data) => {
expect(data).toBeDefined();
Expand Down
14 changes: 0 additions & 14 deletions src/app/main/component/eco-news/services/eco-news.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ export class EcoNewsService implements OnDestroy {
return this.http.get<EcoNewsDto>(`${this.backEnd}eco-news?author-id=${authorId}&page=${page}&size=${quantity}`);
}

getNewsList(): Observable<any> {
const headers = new HttpHeaders();
headers.set('Content-type', 'application/json');
return new Observable((observer: Observer<any>) => {
this.http
.get<EcoNewsDto>(`${this.backEnd}eco-news`)
.pipe(take(1))
.subscribe((newsDto: EcoNewsDto) => {
observer.next(newsDto);
});
});
}

getEcoNewsById(id: number): Observable<EcoNewsModel> {
return this.http.get<EcoNewsModel>(`${this.backEnd}eco-news/${id}?lang=${this.language}`);
}
Expand Down Expand Up @@ -86,7 +73,6 @@ export class EcoNewsService implements OnDestroy {
}): HttpParams {
let params = new HttpParams().set('page', parameters.page.toString()).set('size', parameters.size.toString());

console.log(parameters);
const optionalParams = [
parameters.favorite && this.appendIfNotEmpty('user-id', parameters.userId.toString()),
!parameters.favorite && this.appendIfNotEmpty('author-id', parameters.authorId ? parameters?.authorId.toString() : null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { InfiniteScrollModule } from 'ngx-infinite-scroll';
import { LocalStorageService } from '@global-service/localstorage/local-storage.service';
import { EventsService } from 'src/app/main/component/events/services/events.service';
import { NgxPaginationModule } from 'ngx-pagination';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { EventType } from 'src/app/ubs/ubs/services/event-type.enum';
import { mockEvent, mockFavouriteEvents, mockHabitAssign } from '@assets/mocks/events/mock-events';
import { mockEvent, mockFavouriteEvents } from '@assets/mocks/events/mock-events';
import { mockHabits } from '@assets/mocks/habit/mock-habit-calendar';
import { newsMock } from '@assets/mocks/eco-news/mock-news-item';

describe('ProfileDashboardComponent', () => {
let component: ProfileDashboardComponent;
Expand All @@ -33,23 +34,6 @@ describe('ProfileDashboardComponent', () => {
LocalStorageServiceMock.setCurrentPage = () => of('previousPage', '/profile');
LocalStorageServiceMock.getCurrentLanguage = () => of('ua');

const newsMock = {
countComments: 5,
id: 13578,
imagePath: null,
title: '',
text: '',
content: '',
shortInfo: '',
tags: ['News', 'Events'],
tagsEn: ['News'],
tagsUa: ['Новини'],
creationDate: '2021-11-25T22:32:30.555088+02:00',
likes: 0,
source: '',
author: { id: 312, name: 'taqcTestName' }
};

const storeMock = jasmine.createSpyObj('store', ['select', 'dispatch']);
storeMock.select = () => of({ ecoNews: {}, pages: [], pageNumber: 1, error: 'error' });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export class ProfileDashboardComponent implements OnInit, OnDestroy {
eventsList: EventResponse[] = [];
eventsPerPage = 6;
eventsPage = 1;
favoriteEventsPage = 0;
totalEvents = 0;
totalNews = 0;
tagsList: Array<string> = [];
Expand Down
16 changes: 16 additions & 0 deletions src/assets/mocks/eco-news/mock-news-item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const newsMock = {
countComments: 5,
id: 13578,
imagePath: null,
title: '',
text: '',
content: '',
shortInfo: '',
tags: ['News', 'Events'],
tagsEn: ['News'],
tagsUa: ['Новини'],
creationDate: '2021-11-25T22:32:30.555088+02:00',
likes: 0,
source: '',
author: { id: 312, name: 'taqcTestName' }
};

0 comments on commit 178517e

Please sign in to comment.