diff --git a/src/components/settings/ClientSettings.tsx b/src/components/settings/ClientSettings.tsx index 1dc6bd1..fb07180 100644 --- a/src/components/settings/ClientSettings.tsx +++ b/src/components/settings/ClientSettings.tsx @@ -27,6 +27,7 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) { const router = useRouter(); const { toast } = useToast(); const [isLoading, setIsLoading] = useState(false); + const [isSaving, setIsSaving] = useState(false); const [isCacheClearing, setIsCacheClearing] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); @@ -123,9 +124,11 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) { } }; - const handleSave = (event: React.FormEvent) => { + const handleSave = async (event: React.FormEvent) => { event.preventDefault(); setSuccess(null); + setError(null); + setIsSaving(true); const formData = new FormData(event.currentTarget); const serverUrl = formData.get("serverUrl") as string; @@ -138,36 +141,53 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) { password, }; - const komgaConfig = { - serverUrl: newConfig.serverUrl, - credentials: { - username: newConfig.username, - password: newConfig.password, - }, - }; + try { + const response = await fetch("/api/komga/config", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + url: newConfig.serverUrl, + username: newConfig.username, + password: newConfig.password, + }), + }); - fetch("/api/komga/config", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - url: newConfig.serverUrl, - username: newConfig.username, - password: newConfig.password, - }), - }); + if (!response.ok) { + const data = await response.json(); + throw new Error(data.error || "Erreur lors de la sauvegarde de la configuration"); + } - setConfig(newConfig); + const komgaConfig = { + serverUrl: newConfig.serverUrl, + credentials: { + username: newConfig.username, + password: newConfig.password, + }, + }; - // Émettre un événement pour notifier les autres composants - const configChangeEvent = new CustomEvent("komga-config-changed", { detail: komgaConfig }); - window.dispatchEvent(configChangeEvent); + setConfig(newConfig); - toast({ - title: "Configuration sauvegardée", - description: "La configuration a été sauvegardée avec succès", - }); + // Émettre un événement pour notifier les autres composants + const configChangeEvent = new CustomEvent("komga-config-changed", { detail: komgaConfig }); + window.dispatchEvent(configChangeEvent); + + toast({ + title: "Configuration sauvegardée", + description: "La configuration a été sauvegardée avec succès", + }); + } catch (error) { + console.error("Erreur lors de la sauvegarde:", error); + toast({ + variant: "destructive", + title: "Erreur", + description: + error instanceof Error ? error.message : "Une erreur est survenue lors de la sauvegarde", + }); + } finally { + setIsSaving(false); + } }; const handleInputChange = (event: React.ChangeEvent) => { @@ -278,9 +298,17 @@ export function ClientSettings({ initialConfig }: ClientSettingsProps) {