101 lines
2.9 KiB
JavaScript
101 lines
2.9 KiB
JavaScript
import { ref } from 'vue'
|
|
import { useToast } from './useToast'
|
|
import { useApi } from './useApi'
|
|
|
|
const composants = ref([])
|
|
const loading = ref(false)
|
|
|
|
export function useComposants () {
|
|
const { showSuccess, showError, showInfo } = useToast()
|
|
const { get, post, patch, delete: del } = useApi()
|
|
|
|
const loadComposants = async () => {
|
|
loading.value = true
|
|
try {
|
|
const result = await get('/composants')
|
|
if (result.success) {
|
|
composants.value = result.data
|
|
showInfo(`Chargement de ${composants.value.length} composant(s) réussi`)
|
|
}
|
|
} catch (error) {
|
|
console.error('Erreur lors du chargement des composants:', error)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const createComposant = async (composantData) => {
|
|
loading.value = true
|
|
try {
|
|
const result = await post('/composants', composantData)
|
|
if (result.success) {
|
|
composants.value.push(result.data)
|
|
const displayName = result.data?.name
|
|
|| composantData?.definition?.name
|
|
|| composantData?.name
|
|
|| 'Composant'
|
|
showSuccess(`Composant "${displayName}" créé avec succès`)
|
|
}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Erreur lors de la création du composant:', error)
|
|
return { success: false, error: error.message }
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const updateComposantData = async (id, composantData) => {
|
|
loading.value = true
|
|
try {
|
|
const result = await patch(`/composants/${id}`, composantData)
|
|
if (result.success) {
|
|
const updated = result.data
|
|
const index = composants.value.findIndex(comp => comp.id === id)
|
|
if (index !== -1) {
|
|
composants.value[index] = updated
|
|
}
|
|
showSuccess(`Composant "${updated?.name || composantData.name || ''}" mis à jour avec succès`)
|
|
}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Erreur lors de la mise à jour du composant:', error)
|
|
return { success: false, error: error.message }
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const deleteComposant = async (id) => {
|
|
loading.value = true
|
|
try {
|
|
const result = await del(`/composants/${id}`)
|
|
if (result.success) {
|
|
const deletedComposant = composants.value.find(comp => comp.id === id)
|
|
composants.value = composants.value.filter(comp => comp.id !== id)
|
|
showSuccess(`Composant "${deletedComposant?.name || 'inconnu'}" supprimé avec succès`)
|
|
}
|
|
return result
|
|
} catch (error) {
|
|
console.error('Erreur lors de la suppression du composant:', error)
|
|
return { success: false, error: error.message }
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const getComposants = () => composants.value
|
|
const isLoading = () => loading.value
|
|
|
|
return {
|
|
composants,
|
|
loading,
|
|
loadComposants,
|
|
createComposant,
|
|
updateComposant: updateComposantData,
|
|
deleteComposant,
|
|
getComposants,
|
|
isLoading
|
|
}
|
|
}
|