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:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user