fixes of broken fucntions
This commit is contained in:
@@ -134,12 +134,13 @@ export function usePersistentToggle(
|
||||
|
||||
try {
|
||||
const endpoint = getEndpoint.replace('{id}', resourceId);
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So result IS the data object, not wrapped
|
||||
const result = await fetchAPI(endpoint);
|
||||
|
||||
if (result.success && result.data) {
|
||||
const apiData = result.data;
|
||||
setData(apiData);
|
||||
const newEnabled = extractEnabled(apiData);
|
||||
if (result && typeof result === 'object') {
|
||||
setData(result);
|
||||
const newEnabled = extractEnabled(result);
|
||||
setEnabled(newEnabled);
|
||||
} else {
|
||||
// No data yet - use initial state
|
||||
|
||||
@@ -333,6 +333,9 @@ export default function Integration() {
|
||||
}
|
||||
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// But test endpoint may return {success: true, ...} directly (not wrapped)
|
||||
// So data could be either the extracted data object or the full response
|
||||
const data = await fetchAPI(`/v1/system/settings/integrations/${selectedIntegration}/test/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
@@ -341,24 +344,51 @@ export default function Integration() {
|
||||
}),
|
||||
});
|
||||
|
||||
if (data.success) {
|
||||
toast.success(data.message || 'API connection test successful!');
|
||||
if (data.response) {
|
||||
toast.info(`Response: ${data.response}`);
|
||||
}
|
||||
if (data.tokens_used) {
|
||||
toast.info(`Tokens used: ${data.tokens_used}`);
|
||||
}
|
||||
|
||||
// Update validation status to success
|
||||
if (selectedIntegration) {
|
||||
setValidationStatuses(prev => ({
|
||||
...prev,
|
||||
[selectedIntegration]: 'success',
|
||||
}));
|
||||
// Handle both unified format (extracted) and direct format
|
||||
// If data has success field, it's the direct response (not extracted)
|
||||
// If data doesn't have success but has other fields, it's extracted data (successful)
|
||||
if (data && typeof data === 'object') {
|
||||
if (data.success === true || data.success === false) {
|
||||
// Direct response format (not extracted by fetchAPI)
|
||||
if (data.success) {
|
||||
toast.success(data.message || 'API connection test successful!');
|
||||
if (data.response) {
|
||||
toast.info(`Response: ${data.response}`);
|
||||
}
|
||||
if (data.tokens_used) {
|
||||
toast.info(`Tokens used: ${data.tokens_used}`);
|
||||
}
|
||||
|
||||
// Update validation status to success
|
||||
if (selectedIntegration) {
|
||||
setValidationStatuses(prev => ({
|
||||
...prev,
|
||||
[selectedIntegration]: 'success',
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
throw new Error(data.error || data.message || 'Connection test failed');
|
||||
}
|
||||
} else {
|
||||
// Extracted data format (successful response)
|
||||
toast.success('API connection test successful!');
|
||||
if (data.response) {
|
||||
toast.info(`Response: ${data.response}`);
|
||||
}
|
||||
if (data.tokens_used) {
|
||||
toast.info(`Tokens used: ${data.tokens_used}`);
|
||||
}
|
||||
|
||||
// Update validation status to success
|
||||
if (selectedIntegration) {
|
||||
setValidationStatuses(prev => ({
|
||||
...prev,
|
||||
[selectedIntegration]: 'success',
|
||||
}));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new Error(data.error || 'Connection test failed');
|
||||
throw new Error('Invalid response format');
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error('Error testing connection:', error);
|
||||
|
||||
@@ -640,34 +640,24 @@ export async function autoClusterKeywords(keywordIds: number[], sectorId?: numbe
|
||||
const requestBody = { ids: keywordIds, sector_id: sectorId };
|
||||
|
||||
try {
|
||||
// fetchAPI will automatically extract data from unified format
|
||||
// For action endpoints, response is {success: true, data: {...}}
|
||||
// fetchAPI extracts and returns the data field, so response should already be the data object
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
// After fetchAPI processing, response should be the data object (not wrapped in success/data)
|
||||
// But check if it's still wrapped (shouldn't happen, but for safety)
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
if ('success' in response && response.success === false) {
|
||||
// Error response - return as-is
|
||||
return response as any;
|
||||
}
|
||||
// If response has data field, extract it
|
||||
if ('data' in response && response.data) {
|
||||
return { success: true, ...response.data } as any;
|
||||
}
|
||||
// Response is already the data object (after fetchAPI extraction)
|
||||
// Ensure it has success: true
|
||||
if (!('success' in response)) {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return response as any;
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -677,13 +667,24 @@ export async function autoGenerateIdeas(clusterIds: number[]): Promise<{ success
|
||||
const requestBody = { ids: clusterIds };
|
||||
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
return response;
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -693,13 +694,24 @@ export async function generateSingleIdea(ideaId: string | number, clusterId: num
|
||||
const requestBody = { cluster_id: clusterId };
|
||||
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
return response;
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -1000,13 +1012,24 @@ export async function autoGenerateContent(ids: number[]): Promise<{ success: boo
|
||||
const requestBody = { ids };
|
||||
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
return response;
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -1016,13 +1039,24 @@ export async function autoGenerateImages(taskIds: number[]): Promise<{ success:
|
||||
const requestBody = { task_ids: taskIds };
|
||||
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
return response;
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user