Simplify the onboardinf flow components
This commit is contained in:
@@ -289,4 +289,77 @@ export const useValidateAndImportFile = () => {
|
||||
isLoading: validateCsv.isPending || validateJson.isPending || importCsv.isPending || importJson.isPending,
|
||||
error: validateCsv.error || validateJson.error || importCsv.error || importJson.error,
|
||||
};
|
||||
};
|
||||
|
||||
// Import-only hook (for when validation has already been done)
|
||||
export const useImportFileOnly = () => {
|
||||
const importCsv = useImportCsvFile();
|
||||
const importJson = useImportJsonData();
|
||||
|
||||
const importFile = async (
|
||||
tenantId: string,
|
||||
file: File,
|
||||
options?: {
|
||||
chunkSize?: number;
|
||||
onProgress?: (stage: string, progress: number, message: string) => void;
|
||||
}
|
||||
): Promise<{
|
||||
importResult?: ImportProcessResponse;
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}> => {
|
||||
try {
|
||||
options?.onProgress?.('importing', 10, 'Iniciando importación de datos...');
|
||||
|
||||
const fileExtension = file.name.split('.').pop()?.toLowerCase();
|
||||
let importResult: ImportProcessResponse;
|
||||
|
||||
if (fileExtension === 'csv') {
|
||||
importResult = await importCsv.mutateAsync({
|
||||
tenantId,
|
||||
file,
|
||||
options: {
|
||||
skip_validation: true, // Skip validation since already done
|
||||
chunk_size: options?.chunkSize
|
||||
}
|
||||
});
|
||||
} else if (fileExtension === 'json') {
|
||||
const jsonData = await file.text().then(text => JSON.parse(text));
|
||||
importResult = await importJson.mutateAsync({
|
||||
tenantId,
|
||||
data: jsonData,
|
||||
options: {
|
||||
skip_validation: true, // Skip validation since already done
|
||||
chunk_size: options?.chunkSize
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw new Error('Formato de archivo no soportado. Use CSV o JSON.');
|
||||
}
|
||||
|
||||
options?.onProgress?.('completed', 100,
|
||||
`Importación completada: ${importResult.records_processed} registros procesados`
|
||||
);
|
||||
|
||||
return {
|
||||
importResult,
|
||||
success: importResult.success,
|
||||
error: importResult.success ? undefined : (importResult.errors?.join(', ') || 'Error en la importación'),
|
||||
};
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Error importando archivo';
|
||||
options?.onProgress?.('error', 0, errorMessage);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
error: errorMessage,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
importFile,
|
||||
isImporting: importCsv.isPending || importJson.isPending,
|
||||
error: importCsv.error || importJson.error,
|
||||
};
|
||||
};
|
||||
@@ -4,19 +4,12 @@
|
||||
|
||||
export interface BakeryRegistration {
|
||||
name: string;
|
||||
business_type?: string;
|
||||
description?: string;
|
||||
address?: string;
|
||||
address: string;
|
||||
postal_code: string;
|
||||
phone: string;
|
||||
city?: string;
|
||||
state?: string;
|
||||
country?: string;
|
||||
postal_code?: string;
|
||||
phone?: string;
|
||||
email?: string;
|
||||
website?: string;
|
||||
subdomain?: string;
|
||||
latitude?: number;
|
||||
longitude?: number;
|
||||
business_type?: string;
|
||||
business_model?: string;
|
||||
}
|
||||
|
||||
export interface TenantResponse {
|
||||
|
||||
Reference in New Issue
Block a user