Start integrating the onboarding flow with backend 10
This commit is contained in:
@@ -20,35 +20,6 @@ import type {
|
||||
} from './types';
|
||||
import type { BakeryRegistration } from '../../../api';
|
||||
|
||||
interface OnboardingActions {
|
||||
// Navigation
|
||||
nextStep: () => boolean;
|
||||
previousStep: () => boolean;
|
||||
goToStep: (stepIndex: number) => boolean;
|
||||
|
||||
// Data Management
|
||||
updateStepData: (stepId: string, data: Partial<OnboardingData>) => void;
|
||||
validateCurrentStep: () => string | null;
|
||||
|
||||
// Step-specific Actions
|
||||
createTenant: (bakeryData: BakeryRegistration) => Promise<boolean>;
|
||||
processSalesFile: (file: File, onProgress?: ProgressCallback) => Promise<boolean>;
|
||||
generateInventorySuggestions: (productList: string[]) => Promise<ProductSuggestionResponse[] | null>;
|
||||
createInventoryFromSuggestions: (suggestions: ProductSuggestionResponse[]) => Promise<boolean>;
|
||||
importSalesData: (salesData: any, inventoryMapping: { [productName: string]: string }) => Promise<boolean>;
|
||||
startTraining: (options?: {
|
||||
products?: string[];
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
}) => Promise<boolean>;
|
||||
|
||||
// Completion
|
||||
completeOnboarding: () => Promise<boolean>;
|
||||
|
||||
// Utilities
|
||||
clearError: () => void;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
export const useOnboarding = () => {
|
||||
const navigate = useNavigate();
|
||||
@@ -63,28 +34,6 @@ export const useOnboarding = () => {
|
||||
const inventorySetup = useInventorySetup();
|
||||
const trainingOrchestration = useTrainingOrchestration();
|
||||
|
||||
// Navigation actions
|
||||
const nextStep = useCallback((): boolean => {
|
||||
const validation = validateCurrentStep();
|
||||
if (validation) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (flow.nextStep()) {
|
||||
flow.markStepCompleted(flow.currentStep - 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}, [flow]);
|
||||
|
||||
const previousStep = useCallback((): boolean => {
|
||||
return flow.previousStep();
|
||||
}, [flow]);
|
||||
|
||||
const goToStep = useCallback((stepIndex: number): boolean => {
|
||||
return flow.goToStep(stepIndex);
|
||||
}, [flow]);
|
||||
|
||||
// Data management
|
||||
const updateStepData = useCallback((stepId: string, stepData: Partial<OnboardingData>) => {
|
||||
data.updateStepData(stepId, stepData);
|
||||
@@ -104,10 +53,69 @@ export const useOnboarding = () => {
|
||||
const createTenant = useCallback(async (bakeryData: BakeryRegistration): Promise<boolean> => {
|
||||
const success = await tenantCreation.createTenant(bakeryData);
|
||||
if (success) {
|
||||
updateStepData('setup', { bakery: bakeryData });
|
||||
// Store the bakery data with tenant creation success flag and tenant ID
|
||||
updateStepData('setup', {
|
||||
bakery: {
|
||||
...bakeryData,
|
||||
tenantCreated: true,
|
||||
tenant_id: currentTenant?.id || 'created'
|
||||
} as any // Type assertion to allow the additional properties
|
||||
});
|
||||
console.log('useOnboarding - Tenant created successfully, updated step data with tenant ID:', currentTenant?.id);
|
||||
}
|
||||
return success;
|
||||
}, [tenantCreation, updateStepData]);
|
||||
}, [tenantCreation, updateStepData, currentTenant]);
|
||||
|
||||
// Navigation actions
|
||||
const nextStep = useCallback(async (): Promise<boolean> => {
|
||||
try {
|
||||
const currentStep = flow.getCurrentStep();
|
||||
console.log('useOnboarding - nextStep called from step:', currentStep.id);
|
||||
|
||||
const validation = validateCurrentStep();
|
||||
if (validation) {
|
||||
console.log('useOnboarding - Validation failed:', validation);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle step-specific actions before moving to next step
|
||||
if (currentStep.id === 'setup') {
|
||||
console.log('useOnboarding - Creating tenant before leaving setup step');
|
||||
const allStepData = data.getAllStepData();
|
||||
const bakeryData = allStepData?.setup?.bakery;
|
||||
|
||||
if (bakeryData && !tenantCreation.isSuccess) {
|
||||
console.log('useOnboarding - Tenant data found, creating tenant:', bakeryData);
|
||||
const tenantSuccess = await createTenant(bakeryData);
|
||||
console.log('useOnboarding - Tenant creation result:', tenantSuccess);
|
||||
|
||||
if (!tenantSuccess) {
|
||||
console.log('useOnboarding - Tenant creation failed, stopping navigation');
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
console.log('useOnboarding - No tenant data found or tenant already created');
|
||||
}
|
||||
}
|
||||
|
||||
if (flow.nextStep()) {
|
||||
flow.markStepCompleted(flow.currentStep - 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error('useOnboarding - Error in nextStep:', error);
|
||||
return false;
|
||||
}
|
||||
}, [flow, validateCurrentStep, data, createTenant, tenantCreation]);
|
||||
|
||||
const previousStep = useCallback((): boolean => {
|
||||
return flow.previousStep();
|
||||
}, [flow]);
|
||||
|
||||
const goToStep = useCallback((stepIndex: number): boolean => {
|
||||
return flow.goToStep(stepIndex);
|
||||
}, [flow]);
|
||||
|
||||
const processSalesFile = useCallback(async (
|
||||
file: File,
|
||||
@@ -299,8 +307,5 @@ export const useOnboarding = () => {
|
||||
completeOnboarding,
|
||||
clearError,
|
||||
reset,
|
||||
} satisfies ReturnType<typeof useOnboardingFlow> &
|
||||
ReturnType<typeof useOnboardingData> &
|
||||
{ [key: string]: any } &
|
||||
OnboardingActions;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user