Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d44401
commit 9d05fd8
Showing
9 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { User } from "./user"; | ||
|
||
export interface Comment { | ||
type_of: string; | ||
id_code: string; | ||
created_at: Date; | ||
body_html: string; | ||
user: User; | ||
children: Comment[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 13 additions & 1 deletion
14
src/app/pages/article-detail/article/comments/comments.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
<p>comments works!</p> | ||
<header class="flex justify-between"> | ||
<h2>Discussion <span>(4)</span></h2> | ||
<button class="subscribe-btn btn" type="button">Subscribe</button> | ||
</header> | ||
<div *rxLet="comments$;let comments" class="comments"> | ||
<details *ngFor="let comment of comments" class="comments-wrapper" open=""> | ||
<summary> | ||
{{comment.user.name}} | ||
</summary> | ||
<div [innerHtml]="comment.body_html"> | ||
</div> | ||
</details> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
:host { | ||
display: block; | ||
border-width: 0; | ||
border-top-width: 1px; | ||
border-style: solid; | ||
border-color: #eef0f1; | ||
padding: 2rem 4rem; | ||
} | ||
.subscribe-btn { | ||
border-radius: 5px; | ||
padding: calc(0.5rem - 2px) calc(1rem - 2px); | ||
border: 2px solid #d2d6db; | ||
font-size: 1rem; | ||
font-weight: 500; | ||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); | ||
color: #363d44; | ||
&:hover { | ||
background-color: rgba(0, 0, 0, 0.035); | ||
border-color: #99a3ad; | ||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05); | ||
color: #08090a; | ||
z-index: 1; | ||
} | ||
} |
8 changes: 6 additions & 2 deletions
8
src/app/pages/article-detail/article/comments/comments.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { CommentsStore } from '../services/comments.store'; | ||
|
||
@Component({ | ||
selector: 'app-comments', | ||
templateUrl: './comments.component.html', | ||
styleUrls: ['./comments.component.scss'], | ||
}) | ||
export class CommentsComponent { | ||
constructor() {} | ||
export class CommentsComponent implements OnInit { | ||
comments$ = this.commentStore.comments$; | ||
constructor(private commentStore: CommentsStore) {} | ||
|
||
ngOnInit(): void {} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/app/pages/article-detail/article/services/comments-api.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { CommentsApiService } from './comments-api.service'; | ||
|
||
describe('CommentsApiService', () => { | ||
let service: CommentsApiService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(CommentsApiService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
src/app/pages/article-detail/article/services/comments-api.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { Comment } from 'src/app/models/comment'; | ||
import { environment } from 'src/environments/environment'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CommentsApiService { | ||
constructor(private http: HttpClient) {} | ||
|
||
getComments(id: number): Observable<Comment[]> { | ||
return this.http.get<Comment[]>( | ||
`${environment.baseApi}/comments?a_id=${id}` | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/app/pages/article-detail/article/services/comments.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { CommentsStore } from './comments.store'; | ||
|
||
describe('CommentsStore', () => { | ||
let service: CommentsStore; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(CommentsStore); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
src/app/pages/article-detail/article/services/comments.store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ComponentStore, tapResponse } from '@ngrx/component-store'; | ||
import { Observable } from 'rxjs'; | ||
import { switchMap } from 'rxjs/operators'; | ||
import { Comment } from 'src/app/models/comment'; | ||
import { CommentsApiService } from './comments-api.service'; | ||
|
||
interface CommentsState { | ||
comments: Comment[]; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CommentsStore extends ComponentStore<CommentsState> { | ||
readonly comments$ = this.select((state) => state.comments); | ||
readonly setComments = this.updater( | ||
(state: CommentsState, comments: Comment[]) => ({ | ||
...state, | ||
comments, | ||
}) | ||
); | ||
|
||
readonly getComments = this.effect((id: Observable<number>) => | ||
id.pipe( | ||
switchMap((id) => | ||
this.commetsApiS.getComments(id).pipe( | ||
tapResponse( | ||
(comments) => this.setComments(comments), | ||
(error) => this.logError(error) | ||
) | ||
) | ||
) | ||
) | ||
); | ||
|
||
constructor(private commetsApiS: CommentsApiService) { | ||
super({ comments: [] }); | ||
} | ||
|
||
private logError(error: unknown) { | ||
console.error(error); | ||
} | ||
} |