@@ -12,7 +12,7 @@ import { MarkdownPipe } from '../../../../shared/pipes/markdown.pipe';
1212import { ListErrorsComponent } from '../../../../shared/components/list-errors.component' ;
1313import { ArticleCommentComponent } from '../../components/article-comment.component' ;
1414import { catchError } from 'rxjs/operators' ;
15- import { combineLatest , throwError } from 'rxjs' ;
15+ import { combineLatest , EMPTY } from 'rxjs' ;
1616import { Comment } from '../../models/comment.model' ;
1717import { IfAuthenticatedDirective } from '../../../../core/auth/if-authenticated.directive' ;
1818import { Errors } from '../../../../core/models/errors.model' ;
@@ -41,13 +41,15 @@ import { FollowButtonComponent } from '../../../profile/components/follow-button
4141 changeDetection : ChangeDetectionStrategy . OnPush ,
4242} )
4343export default class ArticleComponent implements OnInit {
44- article = signal < Article > ( null ! ) ;
44+ article = signal < Article | null > ( null ) ;
4545 currentUser = signal < User | null > ( null ) ;
4646 comments = signal < Comment [ ] > ( [ ] ) ;
4747 canModify = signal ( false ) ;
48+ errors = signal < Errors | null > ( null ) ;
4849
4950 commentControl = new FormControl < string > ( '' , { nonNullable : true } ) ;
5051 commentFormErrors = signal < Errors | null > ( null ) ;
52+ deleteCommentErrors = signal < Errors | null > ( null ) ;
5153
5254 isSubmitting = signal ( false ) ;
5355 isDeleting = signal ( false ) ;
@@ -66,8 +68,8 @@ export default class ArticleComponent implements OnInit {
6668 combineLatest ( [ this . articleService . get ( slug ) , this . commentsService . getAll ( slug ) , this . userService . currentUser ] )
6769 . pipe (
6870 catchError ( err => {
69- void this . router . navigate ( [ '/' ] ) ;
70- return throwError ( ( ) => err ) ;
71+ this . errors . set ( err . errors || { error : [ 'Failed to load article' ] } ) ;
72+ return EMPTY ;
7173 } ) ,
7274 takeUntilDestroyed ( this . destroyRef ) ,
7375 )
@@ -80,37 +82,49 @@ export default class ArticleComponent implements OnInit {
8082 }
8183
8284 onToggleFavorite ( favorited : boolean ) : void {
83- this . article . update ( article => ( {
84- ...article ,
85- favorited,
86- favoritesCount : favorited ? article . favoritesCount + 1 : article . favoritesCount - 1 ,
87- } ) ) ;
85+ this . article . update ( article => {
86+ if ( ! article ) return article ;
87+ return {
88+ ...article ,
89+ favorited,
90+ favoritesCount : favorited ? article . favoritesCount + 1 : article . favoritesCount - 1 ,
91+ } ;
92+ } ) ;
8893 }
8994
9095 toggleFollowing ( profile : Profile ) : void {
91- this . article . update ( article => ( {
92- ...article ,
93- author : { ...article . author , following : profile . following } ,
94- } ) ) ;
96+ this . article . update ( article => {
97+ if ( ! article ) return article ;
98+ return {
99+ ...article ,
100+ author : { ...article . author , following : profile . following } ,
101+ } ;
102+ } ) ;
95103 }
96104
97105 deleteArticle ( ) : void {
106+ const article = this . article ( ) ;
107+ if ( ! article ) return ;
108+
98109 this . isDeleting . set ( true ) ;
99110
100111 this . articleService
101- . delete ( this . article ( ) . slug )
112+ . delete ( article . slug )
102113 . pipe ( takeUntilDestroyed ( this . destroyRef ) )
103114 . subscribe ( ( ) => {
104115 void this . router . navigate ( [ '/' ] ) ;
105116 } ) ;
106117 }
107118
108119 addComment ( ) {
120+ const article = this . article ( ) ;
121+ if ( ! article ) return ;
122+
109123 this . isSubmitting . set ( true ) ;
110124 this . commentFormErrors . set ( null ) ;
111125
112126 this . commentsService
113- . add ( this . article ( ) . slug , this . commentControl . value )
127+ . add ( article . slug , this . commentControl . value )
114128 . pipe ( takeUntilDestroyed ( this . destroyRef ) )
115129 . subscribe ( {
116130 next : comment => {
@@ -126,11 +140,20 @@ export default class ArticleComponent implements OnInit {
126140 }
127141
128142 deleteComment ( comment : Comment ) : void {
143+ const article = this . article ( ) ;
144+ if ( ! article ) return ;
145+
146+ this . deleteCommentErrors . set ( null ) ;
129147 this . commentsService
130- . delete ( comment . id , this . article ( ) . slug )
148+ . delete ( comment . id , article . slug )
131149 . pipe ( takeUntilDestroyed ( this . destroyRef ) )
132- . subscribe ( ( ) => {
133- this . comments . update ( comments => comments . filter ( item => item !== comment ) ) ;
150+ . subscribe ( {
151+ next : ( ) => {
152+ this . comments . update ( comments => comments . filter ( item => item !== comment ) ) ;
153+ } ,
154+ error : errors => {
155+ this . deleteCommentErrors . set ( errors ) ;
156+ } ,
134157 } ) ;
135158 }
136159}
0 commit comments