Skip to content

Commit a280ddd

Browse files
committed
✨ save news and subscribe websites added
1 parent f734f5a commit a280ddd

29 files changed

+541
-261
lines changed

src/app/core/components/components.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33
import { SidebarComponent } from './sidebar/sidebar.component';
4+
import { RouterModule } from '@angular/router';
45

56
@NgModule({
67
declarations: [
78
SidebarComponent
89
],
910
imports: [
10-
CommonModule
11+
CommonModule,
12+
RouterModule
1113
],
1214
exports: [
1315
SidebarComponent

src/app/core/components/sidebar/sidebar.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<span class="tooltip">Explore</span>
2020
</li>
2121
<li>
22-
<a href="#">
22+
<a routerLink="/feed/saved">
2323
<i class='bx bx-bookmark'></i>
2424
<span class="links_name">Saved</span>
2525
</a>

src/app/core/guards/is-auth.guard.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
3+
import { Observable, tap } from 'rxjs';
4+
5+
import { AuthService } from '@services/auth.service';
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class IsAuthGuard implements CanActivate {
11+
constructor(private authService: AuthService,
12+
private router: Router) {}
13+
14+
canActivate(
15+
route: ActivatedRouteSnapshot,
16+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
17+
return this.authService.validateToken().pipe(tap(isAuthenticated => {
18+
if (!isAuthenticated) {
19+
this.router.navigateByUrl('/');
20+
}
21+
}));
22+
}
23+
24+
}

src/app/core/models/feed.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export class Feed {
1010
public link: string,
1111
public image: string,
1212
public website: Website,
13+
public inUser?: boolean,
1314
) {};
1415
};

src/app/core/models/user.model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export class User {
1010
public lastName?: string,
1111
public image?: string,
1212
public password?: string,
13-
public subscriptions?: Website[],
14-
public readFeeds?: Website[],
15-
public savedFeeds?: Website[],
13+
public subscriptions?: string[],
14+
public readFeeds?: string[],
15+
public savedFeeds?: string[],
1616
) {}
1717
}

src/app/core/models/website.model.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ export class Website {
55
public description: string,
66
public link: string,
77
public linkFeed: string,
8+
public _id: string,
9+
public inUser?: boolean,
810
) {};
911
};

src/app/core/services/auth.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class AuthService {
5656
const url = `${base_url}/auth/renew`;
5757
return this.http.get<IResponseLogin>(url, this.headers).pipe(map(resp => {
5858
this.setUserActiveInfo(resp);
59+
console.log(resp);
5960
return true;
6061
}), catchError(err => of(false)));
6162
}

src/app/core/services/feed.service.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { map, Observable, Subject } from 'rxjs';
66
import { IResponseFeed } from '@interfaces/response.interface';
77
import { Feed } from '@models/feed.model';
88
import Storage from '@utils/storage.util';
9+
import { AuthService } from './auth.service';
910

1011
const base_url = environment.base_url;
1112

@@ -17,7 +18,10 @@ export class FeedService {
1718
private recentFeeds: Feed[];
1819
public changeNews: Subject<boolean> = new Subject();
1920

20-
constructor(private http: HttpClient) { }
21+
constructor(
22+
private http: HttpClient,
23+
private authService: AuthService,
24+
) { }
2125

2226
public changeToExplore(value: boolean): void{
2327
this.changeNews.next(value);
@@ -39,20 +43,33 @@ export class FeedService {
3943
this.recentFeeds = feeds;
4044
}
4145

42-
getFeeds(skip = 0, limit = 10, isAuthenticated: boolean = false): Observable<IResponseFeed> {
43-
let url: string;
44-
if(!isAuthenticated) {
45-
url = `${base_url}/feed?skip=${skip}&limit=${limit}`;
46-
return this.http.get<IResponseFeed>(url);
47-
} else {
48-
url = `${base_url}/feed/byUser/subscription`;
49-
return this.http.get<IResponseFeed>(url, this.headers);
50-
}
46+
getFeeds(skip = 0, limit = 10, isAuthenticated: boolean = false): Observable<Feed[]> {
47+
48+
const url = (!isAuthenticated) ?
49+
`${base_url}/feed?skip=${skip}&limit=${limit}` :
50+
`${base_url}/feed/byUser/subscription?skip=${skip}&limit=${limit}`;
51+
const headers = !isAuthenticated ? {} : this.headers;
52+
return this.http.get<IResponseFeed>(url, headers).pipe(map(resp => this.mapInUserResource(resp.feeds)));
53+
5154
}
5255

5356
getFeed(id: string): Observable<Feed> {
5457
const url = `${base_url}/feed/${id}`;
5558
return this.http.get<IResponseFeed>(url).pipe(map((resp) => resp.feed));
5659
}
5760

61+
getSavedFeeds(skip = 0, limit = 10): Observable<Feed[]> {
62+
const url = `${base_url}/feed/byUser/saved?skip=${skip}&limit=${limit}`;
63+
return this.http.get<IResponseFeed>(url, this.headers).pipe(map((resp) => this.mapInUserResource(resp.feeds)));
64+
}
65+
66+
private mapInUserResource(feeds: Feed[]): Feed[] {
67+
const userActive = this.authService.getUserActive;
68+
if(userActive) {
69+
const { savedFeeds } = userActive;
70+
feeds.map(feed => feed.inUser = savedFeeds?.includes(feed._id));
71+
}
72+
return feeds;
73+
}
74+
5875
}

src/app/core/services/user.service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { HttpClient } from '@angular/common/http';
2+
import { Injectable } from '@angular/core';
3+
import { environment } from 'environments/environment';
4+
5+
import Storage from '@utils/storage.util';
6+
7+
const base_url = environment.base_url;
8+
9+
@Injectable({
10+
providedIn: 'root'
11+
})
12+
export class UserService {
13+
14+
get headers() {
15+
return {
16+
headers: {
17+
'x-token': Storage.getItem('x-token'),
18+
},
19+
};
20+
}
21+
22+
constructor(
23+
private http: HttpClient
24+
) { }
25+
26+
modifyPreferences(resourceID: string, filter: 'subscription' | 'read' | 'saved') {
27+
const url = `${base_url}/user/${filter}/${resourceID}`;
28+
return this.http.patch(url, {}, this.headers);
29+
}
30+
31+
}

src/app/core/services/website.service.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@ import { environment } from 'environments/environment';
66
import { Website } from '@models/website.model';
77
import { IResponseWebsite } from '@interfaces/response.interface';
88

9+
import { AuthService } from './auth.service';
10+
911
const base_url = environment.base_url;
1012

1113
@Injectable({
1214
providedIn: 'root'
1315
})
1416
export class WebsiteService {
1517

16-
constructor(private http: HttpClient) { }
18+
constructor(private http: HttpClient,
19+
private authService: AuthService) { }
1720

1821
getWebsites(all: boolean = false, skip: number = 0, limit: number = 5): Observable<Website[]> {
1922
const url = `${base_url}/website?all=${all}&limit=${limit}&skip=${skip}`;
20-
return this.http.get<IResponseWebsite>(url).pipe(map(resp => resp.websites));
23+
return this.http.get<IResponseWebsite>(url).pipe(map(resp => {
24+
const { websites } = resp;
25+
const userActive = this.authService.getUserActive;
26+
if(userActive) {
27+
const { subscriptions } = userActive;
28+
websites.map(website => website.inUser = subscriptions?.includes(website._id));
29+
}
30+
return resp.websites
31+
}));
2132
}
2233

2334
}

0 commit comments

Comments
 (0)