feat: implement complex selection flow

This commit is contained in:
Jose Selesan
2026-04-20 15:47:47 -03:00
parent a5e39c94c5
commit 28fd51f926
17 changed files with 453 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
import { Button } from '@/components/ui/button';
import { Field, FieldError, FieldLabel } from '@/components/ui/field';
import { Input } from '@/components/ui/input';
import { authClient } from '@/lib/api-client';
import { apiClient, authClient } from '@/lib/api-client';
import { useAuth } from '@/lib/auth';
import { zodResolver } from '@hookform/resolvers/zod';
import { Link, useNavigate } from '@tanstack/react-router';
@@ -38,12 +38,30 @@ export function LoginPage({ redirectTo = '/' }: LoginPageProps) {
},
});
async function handlePostLogin() {
const complexes = await apiClient.complexes.listMine();
if (complexes.length === 1) {
await apiClient.complexes.select({ complexId: complexes[0].id });
navigate({ to: redirectTo });
} else if (complexes.length > 1) {
const current = await apiClient.complexes.getCurrent();
if (current) {
navigate({ to: redirectTo });
} else {
navigate({ to: '/select-complex' });
}
} else {
await navigate({ to: redirectTo });
}
}
const onSubmit = async (values: LoginForm) => {
setSubmitError(null);
try {
await signInWithPassword(values);
await navigate({ to: redirectTo });
await handlePostLogin();
} catch {
setSubmitError('No pudimos iniciar sesión. Verificá tus credenciales.');
}
@@ -55,9 +73,7 @@ export function LoginPage({ redirectTo = '/' }: LoginPageProps) {
try {
await authClient.signIn.social({
provider: 'google',
callbackURL: redirectTo.startsWith('http')
? redirectTo
: `${window.location.origin}${redirectTo}`,
callbackURL: `${window.location.origin}/auth-callback`,
});
} catch {
setSubmitError('No pudimos iniciar sesión con Google.');