@@ -5,6 +5,7 @@ import { getGitHubThemeData } from "../services/themeService";
5
5
import { Theme } from "../interfaces/Theme" ;
6
6
import { Placeholders } from "../constants/Placeholders" ;
7
7
import { galleryApiFetch } from "../services/apiService" ;
8
+ import { Endpoints } from "@/constants/Endpoints" ;
8
9
9
10
/**
10
11
* Fetches themes from the backend api.
@@ -25,37 +26,21 @@ const useFetchThemes = (
25
26
const [ error , setError ] = useState < Error | null > ( null ) ;
26
27
27
28
useEffect ( ( ) => {
28
- const fetchData = async ( ) => {
29
- setLoading ( true ) ;
30
- try {
31
- let apiThemes = null ;
32
- if ( url . startsWith ( "http" ) ) {
33
- let finalUrl = `${ url } ?pageSize=${ pageSize } &pageNum=${ pageNum } ` ;
34
- if ( searchQuery ) {
35
- finalUrl += `&searchQuery=${ encodeURIComponent ( searchQuery ) } ` ;
36
- }
37
-
38
- const response = await galleryApiFetch ( finalUrl ) ;
39
- const result = await response . json ( ) ;
40
- apiThemes = result . data ;
41
- } else {
42
- apiThemes = Placeholders . themes ;
43
- }
44
-
45
- if ( apiThemes ) {
46
- const themes = await fetchThemesFromGitHub ( apiThemes ) ;
47
- setThemes ( themes ) ;
48
- } else {
49
- setError ( Error ( "Failed to fetch theme." ) ) ;
50
- }
51
- } catch ( err : unknown ) {
52
- setError ( err as Error ) ;
53
- } finally {
54
- setLoading ( false ) ;
55
- }
56
- } ;
29
+ setLoading ( true ) ;
30
+ fetchThemesFromApi ( url , pageSize , pageNum , searchQuery )
31
+
32
+ . then ( themes => {
33
+ setThemes ( themes )
34
+ } )
35
+
36
+ . catch ( reason => {
37
+ setError ( reason )
38
+ } )
39
+
40
+ . finally ( ( ) => {
41
+ setLoading ( false )
42
+ } )
57
43
58
- fetchData ( ) ;
59
44
} , [ url , pageSize , pageNum , searchQuery ] ) ;
60
45
61
46
return { themes, loading, error } ;
@@ -69,4 +54,44 @@ const fetchThemesFromGitHub = async (apiThemes: ApiTheme[]): Promise<Theme[]> =>
69
54
return await Promise . all ( apiThemes . map ( apiTheme => getGitHubThemeData ( apiTheme ) ) ) ;
70
55
}
71
56
57
+ export const fetchThemesFromApi = async (
58
+ url : string = Endpoints . fetchApiThemes ,
59
+ pageSize : number ,
60
+ pageNum : number ,
61
+ searchQuery ?: string
62
+ ) => {
63
+
64
+ try {
65
+ let apiThemes = null ;
66
+
67
+
68
+ if ( url . startsWith ( "http" ) ) {
69
+ let finalUrl = `${ url } ?pageSize=${ pageSize } &pageNum=${ pageNum } ` ;
70
+ if ( searchQuery ) {
71
+ finalUrl += `&searchQuery=${ encodeURIComponent ( searchQuery ) } ` ;
72
+ }
73
+
74
+ // first fetch the list of themes as per the input parameters
75
+ const response = await galleryApiFetch ( finalUrl ) ;
76
+ const result = await response . json ( ) ;
77
+ apiThemes = result . data ;
78
+
79
+ } else {
80
+ apiThemes = Placeholders . themes ;
81
+ }
82
+
83
+ //now fetch the details of each theme
84
+ if ( apiThemes ) {
85
+ return fetchThemesFromGitHub ( apiThemes ) ;
86
+ } else {
87
+ throw new Error ( 'couldnt fetch themes' )
88
+ }
89
+ } catch ( err : any ) {
90
+ throw new Error ( err )
91
+ }
92
+
93
+
94
+
95
+ }
96
+
72
97
export default useFetchThemes ;
0 commit comments