@@ -3,28 +3,28 @@ import { apiClient } from '$lib/utils/axios';
3
3
import { goto } from '$app/navigation' ;
4
4
5
5
export interface Post {
6
- id : string ;
7
- text : string ;
8
- images : string [ ] ;
9
- author : {
10
- id : string ;
11
- handle : string ;
12
- name : string ;
13
- avatarUrl : string ;
14
- } ;
15
- createdAt : string ;
16
- likedBy : string [ ] ;
17
- comments : {
18
- id : string ;
19
- text : string ;
20
- author : {
21
- id : string ;
22
- handle : string ;
23
- name : string ;
24
- avatarUrl : string ;
25
- } ;
26
- createdAt : string ;
27
- } [ ] ;
6
+ id : string ;
7
+ text : string ;
8
+ images : string [ ] ;
9
+ author : {
10
+ id : string ;
11
+ handle : string ;
12
+ name : string ;
13
+ avatarUrl : string ;
14
+ } ;
15
+ createdAt : string ;
16
+ likedBy : string [ ] ;
17
+ comments : {
18
+ id : string ;
19
+ text : string ;
20
+ author : {
21
+ id : string ;
22
+ handle : string ;
23
+ name : string ;
24
+ avatarUrl : string ;
25
+ } ;
26
+ createdAt : string ;
27
+ } [ ] ;
28
28
}
29
29
30
30
export const posts = writable < Post [ ] > ( [ ] ) ;
@@ -36,41 +36,41 @@ export const openCreatePostModal = () => isCreatePostModalOpen.set(true);
36
36
export const closeCreatePostModal = ( ) => isCreatePostModalOpen . set ( false ) ;
37
37
38
38
export const fetchFeed = async ( page = 1 , limit = 10_000 ) => {
39
- try {
40
- isLoading . set ( true ) ;
41
- error . set ( null ) ;
42
- const response = await apiClient . get ( `/api/posts/feed?page=${ page } &limit=${ limit } ` ) ;
43
- posts . set ( response . data ) ;
44
- } catch ( err ) {
45
- error . set ( err instanceof Error ? err . message : 'Failed to fetch feed' ) ;
46
- } finally {
47
- isLoading . set ( false ) ;
48
- }
39
+ try {
40
+ isLoading . set ( true ) ;
41
+ error . set ( null ) ;
42
+ const response = await apiClient . get ( `/api/posts/feed?page=${ page } &limit=${ limit } ` ) ;
43
+ posts . set ( response . data ) ;
44
+ } catch ( err ) {
45
+ error . set ( err instanceof Error ? err . message : 'Failed to fetch feed' ) ;
46
+ } finally {
47
+ isLoading . set ( false ) ;
48
+ }
49
49
} ;
50
50
51
51
export const createPost = async ( text : string , images : string [ ] ) => {
52
- try {
53
- isLoading . set ( true ) ;
54
- error . set ( null ) ;
55
- const response = await apiClient . post ( '/api/posts' , {
56
- text,
57
- images : images . map ( ( img ) => img )
58
- } ) ;
59
- await fetchFeed ( 1 ) ;
60
- return response . data ;
61
- } catch ( err ) {
62
- error . set ( err instanceof Error ? err . message : 'Failed to create post' ) ;
63
- throw err ;
64
- } finally {
65
- isLoading . set ( false ) ;
66
- }
52
+ try {
53
+ isLoading . set ( true ) ;
54
+ error . set ( null ) ;
55
+ const response = await apiClient . post ( '/api/posts' , {
56
+ text,
57
+ images : images . map ( ( img ) => img )
58
+ } ) ;
59
+ await fetchFeed ( 1 ) ;
60
+ return response . data ;
61
+ } catch ( err ) {
62
+ error . set ( err instanceof Error ? err . message : 'Failed to create post' ) ;
63
+ throw err ;
64
+ } finally {
65
+ isLoading . set ( false ) ;
66
+ }
67
67
} ;
68
68
69
69
export const toggleLike = async ( postId : string ) => {
70
- try {
71
- const response = await apiClient . post ( `/api/posts/${ postId } /like` ) ;
72
- return response . data ;
73
- } catch ( err ) {
74
- throw new Error ( err instanceof Error ? err . message : 'Failed to toggle like' ) ;
75
- }
70
+ try {
71
+ const response = await apiClient . post ( `/api/posts/${ postId } /like` ) ;
72
+ return response . data ;
73
+ } catch ( err ) {
74
+ throw new Error ( err instanceof Error ? err . message : 'Failed to toggle like' ) ;
75
+ }
76
76
} ;
0 commit comments