Start integrating the onboarding flow with backend 14
This commit is contained in:
@@ -1,44 +1,47 @@
|
||||
/**
|
||||
* Tenant creation service - Clean, standardized implementation
|
||||
* Tenant creation service - Simplified implementation
|
||||
*/
|
||||
|
||||
import { useCallback } from 'react';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { useRegisterBakery } from '../../../../api';
|
||||
import { useTenantStore } from '../../../../stores/tenant.store';
|
||||
import { useOnboardingStore } from '../core/store';
|
||||
import { createServiceHook } from '../utils/createServiceHook';
|
||||
import type { TenantCreationState } from '../core/types';
|
||||
import type { BakeryRegistration, TenantResponse } from '../../../../api';
|
||||
|
||||
const useTenantCreationService = createServiceHook<TenantCreationState>({
|
||||
initialState: {
|
||||
tenantData: null,
|
||||
},
|
||||
});
|
||||
|
||||
export const useTenantCreation = () => {
|
||||
const service = useTenantCreationService();
|
||||
const registerBakeryMutation = useRegisterBakery();
|
||||
const { setCurrentTenant, loadUserTenants } = useTenantStore();
|
||||
const { setStepData } = useOnboardingStore();
|
||||
|
||||
// Simple, direct state management
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [isSuccess, setIsSuccess] = useState(false);
|
||||
const [tenantData, setTenantData] = useState<TenantResponse | null>(null);
|
||||
|
||||
const createTenant = useCallback(async (bakeryData: BakeryRegistration): Promise<boolean> => {
|
||||
if (!bakeryData) {
|
||||
service.setError('Los datos de la panadería son requeridos');
|
||||
setError('Los datos de la panadería son requeridos');
|
||||
return false;
|
||||
}
|
||||
|
||||
const result = await service.executeAsync(async () => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// Call API to register bakery
|
||||
const tenantResponse: TenantResponse = await registerBakeryMutation.mutateAsync(bakeryData);
|
||||
|
||||
// Update tenant store
|
||||
console.log('useTenantCreation - Setting current tenant:', tenantResponse);
|
||||
setCurrentTenant(tenantResponse);
|
||||
|
||||
// Reload user tenants
|
||||
await loadUserTenants();
|
||||
|
||||
// Update state
|
||||
setTenantData(tenantResponse);
|
||||
setIsSuccess(true);
|
||||
|
||||
// Update onboarding data
|
||||
setStepData('setup', {
|
||||
bakery: {
|
||||
@@ -48,23 +51,39 @@ export const useTenantCreation = () => {
|
||||
} as any,
|
||||
});
|
||||
|
||||
console.log('useTenantCreation - Tenant created and set successfully');
|
||||
return tenantResponse;
|
||||
});
|
||||
console.log('✅ Tenant created successfully');
|
||||
return true;
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Error creating tenant';
|
||||
setError(errorMessage);
|
||||
setIsSuccess(false);
|
||||
return false;
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [registerBakeryMutation, setCurrentTenant, loadUserTenants, setStepData]);
|
||||
|
||||
return result.success;
|
||||
}, [service, registerBakeryMutation, setCurrentTenant, loadUserTenants, setStepData]);
|
||||
const clearError = useCallback(() => {
|
||||
setError(null);
|
||||
}, []);
|
||||
|
||||
const reset = useCallback(() => {
|
||||
setIsLoading(false);
|
||||
setError(null);
|
||||
setIsSuccess(false);
|
||||
setTenantData(null);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
// State
|
||||
isLoading: service.isLoading,
|
||||
error: service.error,
|
||||
isSuccess: service.isSuccess,
|
||||
tenantData: service.tenantData,
|
||||
isLoading,
|
||||
error,
|
||||
isSuccess,
|
||||
tenantData,
|
||||
|
||||
// Actions
|
||||
createTenant,
|
||||
clearError: service.clearError,
|
||||
reset: service.reset,
|
||||
clearError,
|
||||
reset,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user