Skip to content

Commit c3252e8

Browse files
ngrx signal store wip
1 parent f37b36c commit c3252e8

File tree

4 files changed

+35
-38
lines changed

4 files changed

+35
-38
lines changed

src/app/reader/_components/main-page/post/post.component.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@ import {
44
Injector,
55
Input,
66
OnInit,
7-
Signal,
87
inject,
9-
runInInjectionContext,
108
AfterViewInit,
119
ViewContainerRef,
1210
afterNextRender,
1311
} from '@angular/core';
1412
import { ReaderApiService } from '../../../_services/reader-api.service';
1513
import { AsyncPipe, DatePipe } from '@angular/common';
16-
import { Observable, from, map, of } from 'rxjs';
1714
import { CommentsComponent } from './comments/comments.component';
1815
import { AddCommentComponent } from './add-comment/add-comment.component';
19-
import { toSignal } from '@angular/core/rxjs-interop';
20-
import { Comment } from '../../../../shared/_models/comment.inteface';
2116
import { Router } from '@angular/router';
2217
import { DomSanitizer } from '@angular/platform-browser';
2318
import { DynamicDialogService } from '../../../../shared/dynamic-dialog/dynamic-dialog.service';
@@ -33,7 +28,6 @@ import { CodeBlockModalComponent } from './code-block-modal-component/code-block
3328
imports: [AsyncPipe, CommentsComponent, AddCommentComponent, DatePipe],
3429
})
3530
export class PostComponent implements OnInit, AfterViewInit {
36-
//TODO: add resolver
3731
@Input() id!: string;
3832

3933
apiService = inject(ReaderApiService);
@@ -45,16 +39,9 @@ export class PostComponent implements OnInit, AfterViewInit {
4539
private sanitizer = inject(DomSanitizer);
4640
private viewContainerRef = inject(ViewContainerRef);
4741

48-
post$!: Observable<any | null>;
49-
comments$!: Signal<Comment[] | undefined>;
5042
date: string = '';
5143

52-
ngOnInit() {
53-
this.post$ = of(null);
54-
this.comments$! = runInInjectionContext(this.injector, () =>
55-
toSignal(this.apiService.getComments(this.id)),
56-
);
57-
}
44+
ngOnInit() {}
5845

5946
constructor() {
6047
afterNextRender(() => {

src/app/reader/_services/reader-api.service.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,51 @@
11
import { inject, Injectable } from '@angular/core';
22
import { Observable } from 'rxjs';
3-
import { Comment } from '../../shared/_models/comment.inteface';
43
import { SupabaseService } from '../../services/supabase.service';
5-
import { Post } from '../../types/supabase';
4+
import { Comment, Post } from '../../types/supabase';
65

76
@Injectable({ providedIn: 'root' })
87
export class ReaderApiService {
98
supabaseService = inject(SupabaseService);
109

1110
async getPost(id: string): Promise<Post | null> {
12-
return null;
11+
const { data: post, error } = await this.supabaseService.getClient
12+
.from('posts')
13+
.select(
14+
`
15+
*,
16+
author:profiles ( id, username, avatar_url ),
17+
post_tags (
18+
tags ( id, name, color, icon )
19+
)
20+
`,
21+
)
22+
.eq('id', id)
23+
.single();
24+
return error ? null : post;
1325
}
1426

15-
getComments(postId: string): Observable<Comment[]> {
16-
return [] as unknown as Observable<Comment[]>;
27+
async getComments(postId: string): Promise<Comment[]> {
28+
const { data: comments, error } = await this.supabaseService.getClient
29+
.from('comments')
30+
.select('*')
31+
.eq('post_id', postId)
32+
.order('created_at', { ascending: true });
33+
return error ? [] : comments;
1734
}
1835

19-
addComment(postId: string, comment: Comment): void {}
36+
async addComment(postId: string, comment: Comment): Promise<void> {
37+
const { error } = await this.supabaseService.getClient
38+
.from('comments')
39+
.insert({ ...comment, post_id: postId });
40+
}
2041

21-
deleteComment(commentId: string, postId: string): void {}
42+
async deleteComment(commentId: string, postId: string): Promise<void> {
43+
const { error } = await this.supabaseService.getClient
44+
.from('comments')
45+
.delete()
46+
.eq('id', commentId)
47+
.eq('post_id', postId);
48+
}
2249

2350
async getPosts(): Promise<Post[] | null> {
2451
const { data: posts } = await this.supabaseService.getClient

src/app/shared/_models/blog-user.interface.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/app/shared/_models/comment.inteface.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)