File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change 1
1
import { ref } from "vue" ;
2
2
import { useLoading } from "./loading.composable" ;
3
3
import PostsServices from "@/services/posts.services" ;
4
+ import StorageService from "@/services/storage.service" ;
4
5
5
6
export default function useFetchPost ( ) {
6
7
const { endLoading, isLoading, startLoading } = useLoading ( ) ;
7
8
8
9
const post = ref ( null ) ;
9
10
10
11
function fetchPost ( id ) {
11
- startLoading ( )
12
- return PostsServices . getOneById ( id ) . then ( ( response ) => {
13
- post . value = response . data
14
- return response
15
- } ) . finally ( ( ) => {
16
- endLoading ( )
17
- } ) ;
12
+ const cachedPosts = StorageService . get ( "cached-posts" ) || { } ;
13
+ if ( cachedPosts [ `post-${ id } ` ] ) return cachedPosts [ `post-${ id } ` ] ;
14
+ startLoading ( ) ;
15
+ return PostsServices . getOneById ( id )
16
+ . then ( ( response ) => {
17
+ post . value = response . data ;
18
+ StorageService . set ( 'cached-posts' , { ...cachedPosts , [ `post-${ id } ` ] : response . data } )
19
+ return response ;
20
+ } )
21
+ . finally ( ( ) => {
22
+ endLoading ( ) ;
23
+ } ) ;
18
24
}
19
25
20
26
return {
21
27
postIsLoading : isLoading ,
22
28
post,
23
- fetchPost
29
+ fetchPost,
24
30
} ;
25
31
}
You can’t perform that action at this time.
0 commit comments