feat: themes controller
This commit is contained in:
		
							parent
							
								
									b4fceeb958
								
							
						
					
					
						commit
						d71f2deb89
					
				
					 4 changed files with 37 additions and 2 deletions
				
			
		
							
								
								
									
										1
									
								
								frontend/config.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								frontend/config.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | ||||||
|  | export const API_BASE = "http://localhost:3000/api"; | ||||||
|  | @ -3,6 +3,7 @@ | ||||||
|     import { ref, onMounted, watch } from "vue"; |     import { ref, onMounted, watch } from "vue"; | ||||||
|     import { useI18n } from "vue-i18n"; |     import { useI18n } from "vue-i18n"; | ||||||
|     import {AGE_TO_THEMES, THEMESITEMS} from "@/utils/constants.ts"; |     import {AGE_TO_THEMES, THEMESITEMS} from "@/utils/constants.ts"; | ||||||
|  |     import {getAllThemes} from "@/controller/themes.ts"; | ||||||
| 
 | 
 | ||||||
|     // Receive the selectedTheme and selectedAge from the parent component |     // Receive the selectedTheme and selectedAge from the parent component | ||||||
|     const props = defineProps({ |     const props = defineProps({ | ||||||
|  | @ -26,10 +27,9 @@ | ||||||
|         try { |         try { | ||||||
|             // Get the current selected language |             // Get the current selected language | ||||||
|             const language = locale.value; |             const language = locale.value; | ||||||
|             const response = await fetch(`http://localhost:3000/api/theme?language=${language}`); |  | ||||||
| 
 | 
 | ||||||
|             // Update the cards value with the fetched themes |             // Update the cards value with the fetched themes | ||||||
|             allCards.value = await response.json(); |             allCards.value = await getAllThemes(language); | ||||||
|             cards.value = allCards.value; |             cards.value = allCards.value; | ||||||
|         } catch (error) { |         } catch (error) { | ||||||
|             console.error("Error fetching themes:", error); |             console.error("Error fetching themes:", error); | ||||||
|  |  | ||||||
							
								
								
									
										20
									
								
								frontend/src/controller/fetch.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								frontend/src/controller/fetch.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | ||||||
|  | export const fetchJson = async <T = any>(url: string, init?: RequestInit): Promise<T> => { | ||||||
|  |     const response = await fetch(url, init); | ||||||
|  | 
 | ||||||
|  |     if (!response.ok) { | ||||||
|  |         let errorMessage = `Error ${response.status} ${response.statusText}`; | ||||||
|  | 
 | ||||||
|  |         try { | ||||||
|  |             const errorData = await response.json(); | ||||||
|  |             if (errorData?.error) { | ||||||
|  |                 errorMessage = errorData.error; | ||||||
|  |             } | ||||||
|  |         } catch { | ||||||
|  |             // No valid JSON or error property
 | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         throw new Error(errorMessage); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return await response.json(); | ||||||
|  | }; | ||||||
							
								
								
									
										14
									
								
								frontend/src/controller/themes.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								frontend/src/controller/themes.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | ||||||
|  | import {fetchJson} from "@/controller/fetch.ts"; | ||||||
|  | import {API_BASE} from "../../config.ts"; | ||||||
|  | 
 | ||||||
|  | export const getAllThemes = async (language: string | null = null) => { | ||||||
|  |     const url = language | ||||||
|  |         ? `${API_BASE}/theme?language=${encodeURIComponent(language)}` | ||||||
|  |         : `${API_BASE}/theme`; | ||||||
|  |     return await fetchJson(url); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export const getHruidsByTheme = async (theme: string) => { | ||||||
|  |     const url = `${API_BASE}/theme/${encodeURIComponent(theme)}`; | ||||||
|  |     return await fetchJson(url); | ||||||
|  | }; | ||||||
		Reference in a new issue
	
	 Gabriellvl
						Gabriellvl