Skip to content

Commit 6ab6426

Browse files
authored
Merge pull request #20
Filtering by subscription
2 parents 17299d4 + 66368be commit 6ab6426

9 files changed

Lines changed: 82 additions & 13 deletions

File tree

src/app/app.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const routes: Routes = [
2626
{
2727
path: 'subscriptions',
2828
loadComponent: async () => {
29-
const c = await import('./pages/subscriptions/subscriptions-page.component')
29+
const c = await import('./pages/subscriptions-page/subscriptions-page')
3030
return c.SubscriptionsPage
3131
},
3232
data: { title: 'Subscriptions' },

src/app/components/nav/nav.component.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ mat-sidenav-content {
3434
.page-content {
3535
overflow: auto;
3636
}
37+
38+
.title {
39+
display: flex;
40+
flex-direction: row;
41+
align-items: center;
42+
justify-content: space-between;
43+
width: 100%;
44+
}
45+
46+
.title h2 {
47+
font-size: smaller;
48+
}

src/app/components/nav/nav.component.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
4444
</button>
4545
}
46-
<h1>{{ currentTitle() }}</h1>
46+
<div class="title">
47+
<h1>{{ currentTitle() }}</h1>
48+
<h2>{{ currentSubtitle() }}</h2>
49+
</div>
4750
</mat-toolbar>
4851
<div class="page-content">
4952
<ng-content select="router-outlet"/>

src/app/components/nav/nav.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Component, DestroyRef, inject, OnInit, signal, viewChild } from '@angular/core'
1+
import {
2+
Component,
3+
DestroyRef,
4+
inject,
5+
OnInit,
6+
signal,
7+
viewChild
8+
} from '@angular/core'
29
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'
310
import { AsyncPipe } from '@angular/common'
411
import { MatToolbarModule } from '@angular/material/toolbar'
@@ -35,6 +42,7 @@ export class NavComponent implements OnInit {
3542
private sideNav = viewChild<MatSidenav>('drawer')
3643

3744
currentTitle = toSignal(this.titleService.$currentTitle)
45+
currentSubtitle = toSignal(this.titleService.$currentSubtitle)
3846

3947
isHandset = signal<boolean>(false)
4048

src/app/pages/home/home-page.component.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { MatToolbarModule } from '@angular/material/toolbar'
99
import { MatButtonToggleModule } from '@angular/material/button-toggle'
1010
import { MatIconModule } from '@angular/material/icon'
1111
import { RowSpacer } from '../../components/row-spacer/row-spacer'
12-
import { Router } from '@angular/router'
12+
import { ActivatedRoute, Router } from '@angular/router'
1313
import { Article } from '../../entities/article/article.types'
1414
import { MatPaginatorModule } from '@angular/material/paginator'
1515
import { TagService } from '../../services/tag-service'
@@ -44,6 +44,7 @@ import { SortOrder } from '../../entities/base/base.enums'
4444
export class HomePage implements OnInit {
4545
feedService = inject(FeedService)
4646
router = inject(Router)
47+
route = inject(ActivatedRoute)
4748
destroyRef = inject(DestroyRef)
4849
tagService = inject(TagService)
4950
titleService = inject(TitleService)
@@ -54,6 +55,7 @@ export class HomePage implements OnInit {
5455

5556
$readFilter = new BehaviorSubject(true)
5657
$favFilter = new BehaviorSubject(false)
58+
$subscriptionFilter = new BehaviorSubject<string | null>(null)
5759
$dateOrder = new BehaviorSubject(SortOrder.Desc)
5860

5961
favTagId = signal<string>('')
@@ -93,9 +95,10 @@ export class HomePage implements OnInit {
9395
this.pageService.$currentPage,
9496
this.$favFilter,
9597
this.$readFilter,
98+
this.$subscriptionFilter,
9699
this.$dateOrder,
97100
]).pipe(
98-
switchMap(([perPage, pageNumber, fav, read, dateSort]) => {
101+
switchMap(([perPage, pageNumber, fav, read, subscription, dateSort]) => {
99102
const filters: Record<string, string | boolean> = {}
100103

101104
if (read) {
@@ -106,6 +109,10 @@ export class HomePage implements OnInit {
106109
filters['tags'] = favTag
107110
}
108111

112+
if (subscription) {
113+
filters['subscription'] = subscription
114+
}
115+
109116
return this.feedService.getAllArticles({
110117
pagination: {
111118
perPage,
@@ -124,9 +131,31 @@ export class HomePage implements OnInit {
124131
if (result) {
125132
this.articles.set(result.result)
126133
this.pageService.setTotalResults(result.total)
127-
this.titleService.setTitle(`News: ${result.total} articles`)
134+
this.titleService.setTitle(`Articles: ${result.total} articles`)
128135
} else {
129-
this.titleService.setTitle('News')
136+
this.titleService.setTitle('Articles')
137+
}
138+
})
139+
140+
this.route.queryParams
141+
.pipe(
142+
takeUntilDestroyed(this.destroyRef),
143+
switchMap((params) => {
144+
const subscriptionId: string = params['subscription']
145+
if (!subscriptionId) {
146+
return of(null)
147+
}
148+
return this.feedService.getOneSubscription({ subscriptionId })
149+
}),
150+
catchError((e) => {
151+
console.error(e)
152+
return of(null)
153+
}),
154+
)
155+
.subscribe((feed) => {
156+
if (feed) {
157+
this.titleService.setSubtitle(feed.title)
158+
this.$subscriptionFilter.next(feed._id)
130159
}
131160
})
132161
}
@@ -165,9 +194,9 @@ export class HomePage implements OnInit {
165194
if (filter === 'read') {
166195
this.$readFilter.next(!this.$readFilter.value)
167196
} else {
168-
this.pageService.setCurrentPage(1)
197+
this.$favFilter.next(!this.$favFilter.value)
169198
}
170-
this.$favFilter.next(!this.$favFilter.value)
199+
this.pageService.setCurrentPage(1)
171200
}
172201

173202
orderHandler(param: 'date') {

src/app/pages/subscriptions/subscriptions-page.component.css renamed to src/app/pages/subscriptions-page/subscriptions-page.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
:host .mat-mdc-paginator {
1010
background: transparent;
1111
}
12+
13+
.card {
14+
cursor: pointer;
15+
}

src/app/pages/subscriptions/subscriptions-page.component.html renamed to src/app/pages/subscriptions-page/subscriptions-page.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
} @else {
2424
@defer {
2525
@for (f of feeds(); track f._id) {
26-
<mat-card>
26+
<mat-card
27+
class="card"
28+
routerLink="/home"
29+
[queryParams]="{ subscription: f._id }"
30+
>
2731
<mat-card-header>
2832
<mat-card-title>{{ f.title }}</mat-card-title>
2933
<app-row-spacer/>

src/app/pages/subscriptions/subscriptions-page.component.ts renamed to src/app/pages/subscriptions-page/subscriptions-page.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import { Paginator } from '../../components/paginator/paginator'
1818
import { PageService } from '../../services/page-service'
1919
import { TitleService } from '../../services/title-service'
2020
import { scrollUp } from '../../../utils'
21+
import { RouterLink } from '@angular/router'
2122

2223
@Component({
23-
selector: 'app-subscriptions',
24+
selector: 'app-subscriptions-page',
2425
imports: [
2526
MatCardModule,
2627
MatToolbarModule,
@@ -32,9 +33,10 @@ import { scrollUp } from '../../../utils'
3233
MatPaginatorModule,
3334
LinkTrimPipe,
3435
Paginator,
36+
RouterLink,
3537
],
36-
templateUrl: './subscriptions-page.component.html',
37-
styleUrl: './subscriptions-page.component.css',
38+
templateUrl: './subscriptions-page.html',
39+
styleUrl: './subscriptions-page.css',
3840
})
3941
export class SubscriptionsPage implements OnInit {
4042
constructor() {

src/app/services/title-service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ export class TitleService {
1111
private $$currentTitle = new BehaviorSubject<string>('')
1212
$currentTitle = this.$$currentTitle.asObservable()
1313

14+
private $$currentSubtitle = new BehaviorSubject<string | null>(null)
15+
$currentSubtitle = this.$$currentSubtitle.asObservable()
16+
1417
setTitle(title: string) {
1518
this.$$currentTitle.next(title)
1619
this.pageTitleService.setTitle(title)
1720
}
21+
22+
setSubtitle(subtitle: string | null) {
23+
this.$$currentSubtitle.next(subtitle)
24+
}
1825
}

0 commit comments

Comments
 (0)