Refactor API response handling across multiple components to ensure consistency with the unified format. Update error handling and response validation in ValidationCard, usePersistentToggle, Status, Prompts, and api.ts to improve user feedback and maintain compatibility with the new API standards.

This commit is contained in:
IGNY8 VPS (Salman)
2025-11-16 00:19:01 +00:00
parent 5908115686
commit 7665b8c6e7
5 changed files with 140 additions and 87 deletions

View File

@@ -168,23 +168,21 @@ export function usePersistentToggle(
const endpoint = saveEndpoint.replace('{id}', resourceId);
const payload = buildPayload(data || {}, newEnabled);
const result = await fetchAPI(endpoint, {
// fetchAPI extracts data from unified format {success: true, data: {...}}
// If no error is thrown, assume success
await fetchAPI(endpoint, {
method: 'POST',
body: JSON.stringify(payload),
});
if (result.success) {
// Update local state
const updatedData = { ...(data || {}), enabled: newEnabled };
setData(updatedData);
setEnabled(newEnabled);
// Call success callback - pass both enabled state and full config data
if (onToggleSuccess) {
onToggleSuccess(newEnabled, updatedData);
}
} else {
throw new Error(result.error || 'Failed to save state');
// Update local state
const updatedData = { ...(data || {}), enabled: newEnabled };
setData(updatedData);
setEnabled(newEnabled);
// Call success callback - pass both enabled state and full config data
if (onToggleSuccess) {
onToggleSuccess(newEnabled, updatedData);
}
} catch (err: any) {
const error = err instanceof Error ? err : new Error(String(err));