Skip to content

Commit 79cf5cd

Browse files
changed to rest api
1 parent 0e9770c commit 79cf5cd

File tree

6 files changed

+42
-84
lines changed

6 files changed

+42
-84
lines changed

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

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,36 +84,20 @@ export class ReaderApiService {
8484
return this.http.get<Post[]>(this.baseUrl, { headers, params });
8585
}
8686

87-
// async getPosts(): Promise<Post[] | null> {
88-
// const { data: posts } = await this.supabaseService.getClient
89-
// .from('posts')
90-
// .select(
91-
// `
92-
// *,
93-
// author:profiles ( id, username, avatar_url ),
94-
// post_tags (
95-
// tags ( id, name, color, icon )
96-
// )
97-
// `,
98-
// )
99-
// .eq('is_draft', false)
100-
// .order('created_at', { ascending: false });
101-
//
102-
// return posts;
103-
// }
104-
10587
async getProfiles(): Promise<Profile[] | null> {
10688
const { data: profiles, error } = await this.supabaseService.getClient
10789
.from('profiles')
10890
.select('*');
10991
return error ? null : profiles;
11092
}
11193

112-
async getTags(): Promise<Tag[] | null> {
113-
const { data: tags, error } = await this.supabaseService.getClient
114-
.from('tags')
115-
.select('*');
116-
return error ? null : tags;
94+
getTags(): Observable<Tag[] | null> {
95+
const headers = new HttpHeaders({
96+
apikey: this.apiKey,
97+
Authorization: `Bearer ${this.apiKey}`,
98+
Accept: 'application/json',
99+
});
100+
return this.http.get<Tag[]>(this.baseUrl, { headers });
117101
}
118102

119103
async getPostTags(postId: string): Promise<PostTag[] | null> {

src/app/reader/post-list.resolver.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/app/reader/reader.routes.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { Routes } from '@angular/router';
22
import { MainPageComponent } from './_components/main-page/main-page.component';
33
import { PostComponent } from './_components/main-page/post/post.component';
44
import { PostsListComponent } from './_components/main-page/posts-list/posts-list.component';
5-
import { postListResolver } from './post-list.resolver';
6-
import { tagListResolver } from './tag-list.resolver';
75

86
export const readerRoutes: Routes = [
97
{
@@ -13,7 +11,6 @@ export const readerRoutes: Routes = [
1311
{
1412
path: '',
1513
component: PostsListComponent,
16-
// resolve: [postListResolver, tagListResolver],
1714
},
1815
{
1916
path: 'details/:id',

src/app/reader/tag-list.resolver.spec.ts

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

src/app/reader/tag-list.resolver.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,59 @@
1-
import { Tag } from '../../types/supabase';
1+
import { inject } from '@angular/core';
2+
import { tapResponse } from '@ngrx/operators';
23
import {
34
patchState,
45
signalStore,
5-
withComputed,
6-
withHooks,
7-
withMethods,
86
withState,
7+
withMethods,
8+
withHooks,
9+
withComputed
910
} from '@ngrx/signals';
10-
import { computed, inject } from '@angular/core';
11+
import { rxMethod } from '@ngrx/signals/rxjs-interop';
12+
import { pipe, switchMap, tap } from 'rxjs';
13+
14+
import { Tag } from '../../types/supabase';
1115
import { ReaderApiService } from '../../reader/_services/reader-api.service';
1216

1317
type TagsState = {
14-
tags: Tag[];
18+
tags: Tag[] | null;
1519
loading: boolean;
1620
error: string | null;
1721
};
1822

1923
const initialState: TagsState = {
2024
tags: [],
2125
loading: false,
22-
error: null,
26+
error: null
2327
};
2428

2529
export const TagsStore = signalStore(
2630
{ providedIn: 'root' },
2731
withState(initialState),
2832

29-
withMethods((state, tagsService = inject(ReaderApiService)) => ({
30-
async getTags() {
31-
patchState(state, { loading: true, error: null });
32-
try {
33-
const tags = await tagsService.getTags();
34-
if (tags) {
35-
patchState(state, { tags, loading: false });
36-
} else {
37-
patchState(state, { error: 'No tags found', loading: false });
38-
}
39-
} catch (error) {
40-
patchState(state, { error: 'Failed to fetch tags', loading: false });
41-
}
42-
},
33+
withMethods((store, api = inject(ReaderApiService)) => ({
34+
loadTags: rxMethod<void>(
35+
pipe(
36+
tap(() => patchState(store, { loading: true, error: null })),
37+
switchMap(() =>
38+
api.getTags().pipe(
39+
tapResponse({
40+
next: (tags) =>
41+
patchState(store, { tags, loading: false }),
42+
error: (err) =>
43+
patchState(store, {
44+
error: 'Failed to fetch tags',
45+
loading: false
46+
})
47+
})
48+
)
49+
)
50+
)
51+
)
4352
})),
4453

45-
withHooks({
46-
onInit(store) {
47-
store.getTags();
48-
},
49-
}),
54+
withHooks(({ loadTags }) => ({
55+
onInit() {
56+
loadTags();
57+
}
58+
}))
5059
);

0 commit comments

Comments
 (0)