Init du front
This commit is contained in:
148
src/services/textService.js
Normal file
148
src/services/textService.js
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* Service pour communiquer avec l'API backend des textes en patois
|
||||
*/
|
||||
|
||||
const API_BASE_URL = 'http://localhost:3001/api'
|
||||
|
||||
export class TextService {
|
||||
constructor() {
|
||||
this.cache = new Map()
|
||||
this.CACHE_DURATION = 5 * 60 * 1000 // 5 minutes
|
||||
}
|
||||
|
||||
/**
|
||||
* Effectue une requête HTTP vers l'API
|
||||
*/
|
||||
async fetchAPI(endpoint, options = {}) {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers
|
||||
},
|
||||
...options
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}))
|
||||
throw new Error(errorData.error || `Erreur HTTP: ${response.status}`)
|
||||
}
|
||||
|
||||
return await response.json()
|
||||
} catch (error) {
|
||||
console.error(`Erreur API ${endpoint}:`, error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge un texte spécifique par son ID
|
||||
*/
|
||||
async loadText(textId) {
|
||||
const cacheKey = `text-${textId}`
|
||||
|
||||
// Vérifier le cache
|
||||
if (this.cache.has(cacheKey)) {
|
||||
const cached = this.cache.get(cacheKey)
|
||||
if (Date.now() - cached.timestamp < this.CACHE_DURATION) {
|
||||
return cached.data
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const textData = await this.fetchAPI(`/texts/${textId}`)
|
||||
|
||||
// Mettre en cache
|
||||
this.cache.set(cacheKey, {
|
||||
data: textData,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
|
||||
return textData
|
||||
} catch (error) {
|
||||
throw new Error(`Impossible de charger le texte "${textId}": ${error.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
ni
|
||||
|
||||
/**
|
||||
* Recherche dans les textes
|
||||
const cacheKey = 'all-texts'
|
||||
|
||||
// Vérifier le cache
|
||||
if (this.cache.has(cacheKey)) {
|
||||
const cached = this.cache.get(cacheKey)
|
||||
if (Date.now() - cached.timestamp < this.CACHE_DURATION) {
|
||||
return cached.data
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
const texts = await this.fetchAPI('/texts')
|
||||
|
||||
// Mettre en cache
|
||||
this.cache.set(cacheKey, {
|
||||
data: texts,
|
||||
timestamp: Date.now()
|
||||
})
|
||||
text.metadata.titre_pt?.toLowerCase().includes(searchTerm) ||
|
||||
text.frenchText?.toLowerCase().includes(searchTerm) ||
|
||||
)
|
||||
throw new Error(`Impossible de charger la liste des textes: ${error.message}`)
|
||||
|
||||
// Filtres
|
||||
if (filters.category) {
|
||||
results = results.filter(text => text.metadata.categorie === filters.category)
|
||||
* Recherche dans les textes avec filtres
|
||||
|
||||
if (filters.difficulty) {
|
||||
try {
|
||||
const params = new URLSearchParams()
|
||||
|
||||
if (query && query.trim()) {
|
||||
params.append('search', query.trim())
|
||||
}
|
||||
|
||||
if (filters.category) {
|
||||
params.append('category', filters.category)
|
||||
}
|
||||
|
||||
if (filters.difficulty) {
|
||||
params.append('difficulty', filters.difficulty)
|
||||
}
|
||||
|
||||
if (filters.onlyWithAudio) {
|
||||
params.append('onlyWithAudio', 'true')
|
||||
}
|
||||
}
|
||||
const endpoint = params.toString() ? `/texts?${params.toString()}` : '/texts'
|
||||
return await this.fetchAPI(endpoint)
|
||||
} catch (error) {
|
||||
throw new Error(`Erreur lors de la recherche: ${error.message}`)
|
||||
}
|
||||
const authors = new Set()
|
||||
const categories = new Set()
|
||||
let withAudio = 0
|
||||
|
||||
for (const text of this.textsList) {
|
||||
if (text.metadata.auteur) authors.add(text.metadata.auteur)
|
||||
try {
|
||||
return await this.fetchAPI('/random')
|
||||
} catch (error) {
|
||||
throw new Error(`Impossible d'obtenir un texte aléatoire: ${error.message}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Instance singleton
|
||||
export const textService = new TextService()
|
||||
|
||||
const cacheKey = 'stats'
|
||||
|
||||
// Vérifier le cache
|
||||
if (this.cache.has(cacheKey)) {
|
||||
const cached = this.cache.get(cacheKey)
|
||||
if (Date.now() - cached.timestamp < this.CACHE_DURATION) {
|
||||
return cached.data
|
||||
}
|
||||
Reference in New Issue
Block a user