From 1a472a6db399f2f28ab6d7d4807863b4a8494ebb Mon Sep 17 00:00:00 2001 From: mengstabketemaw Date: Sat, 27 Jun 2026 09:37:29 +0300 Subject: [PATCH] refactor: simplify profile lookup and navigation logic commit --- .../profile-setup/pages/ProfileSetupPage.tsx | 36 ++--------------- .../features/profile/pages/ProfilePage.tsx | 13 +++--- libs/auth/src/lib/pages/LoginPage.tsx | 40 ++++++++++--------- 3 files changed, 30 insertions(+), 59 deletions(-) diff --git a/apps/portal/src/app/features/profile-setup/pages/ProfileSetupPage.tsx b/apps/portal/src/app/features/profile-setup/pages/ProfileSetupPage.tsx index bed449c23..f89f8a878 100644 --- a/apps/portal/src/app/features/profile-setup/pages/ProfileSetupPage.tsx +++ b/apps/portal/src/app/features/profile-setup/pages/ProfileSetupPage.tsx @@ -24,7 +24,7 @@ import { zodResolver } from '@hookform/resolvers/zod'; import { useNavigate } from 'react-router-dom'; import { useApiMutation } from '@ema-platform/api'; import { notify } from '@ema-platform/ui'; -import { authStorage, setUser, logout } from '@ema-platform/auth'; +import { authStorage, setUser, logout, type AuthUser } from '@ema-platform/auth'; import { useAppDispatch, useAppSelector } from '../../../store/hooks'; import { ProfileFormContent, @@ -109,12 +109,6 @@ function StepIndicator({ active, completed }: { active: number; completed: numbe ); } -function inferUserType(professionName: string): string { - const name = professionName.toLowerCase(); - if (name.includes('seafarer')) return 'SEAFARER'; - return 'EMPLOYEE'; -} - export function ProfileSetupPage() { const navigate = useNavigate(); const dispatch = useAppDispatch(); @@ -126,13 +120,10 @@ export function ProfileSetupPage() { const [professionsLoading, setProfessionsLoading] = useState(true); const [profileTrigger] = useApiMutation<{ id: string }>(); const [addressTrigger] = useApiMutation(); - const [meTrigger] = useApiMutation<{ id: string }>(); - const [profileCheckTrigger] = useApiMutation<{ total: number; items: Array<{ id: string; user: { id: string }; isComplete: boolean }> }>(); + const [meTrigger] = useApiMutation(); const [fetchProfessions] = useApiMutation<{ count: number; items: Array<{ id: string; name: { en: string } }> }>(); const fetched = useRef(false); - const checkedExistingProfile = useRef(false); - useEffect(() => { if (fetched.current) return; fetched.current = true; @@ -143,21 +134,6 @@ export function ProfileSetupPage() { .finally(() => setProfessionsLoading(false)); }, [fetchProfessions]); - useEffect(() => { - if (!user || checkedExistingProfile.current) return; - checkedExistingProfile.current = true; - profileCheckTrigger({ url: `/profiles?q=${encodeURIComponent('i=user')}`, method: 'GET' }) - .unwrap() - .then((data) => { - const existing = data.items?.find((p) => p.user?.id === user.id); - if (existing?.isComplete) { - authStorage.setProfileId(existing.id); - navigate('/dashboard', { replace: true }); - } - }) - .catch(() => {}); - }, [user, navigate, profileCheckTrigger]); - const professionOptions = useMemo( () => professions.map((p) => ({ value: p.id, label: p.name.en })), [professions], @@ -248,7 +224,7 @@ export function ProfileSetupPage() { method: 'POST', body: { userId: user?.id, - type: inferUserType(selectedProfessionName), + type: 'SEAFARER', professionId: pv.professionId, firstName: pv.firstName, middleName: pv.middleName, @@ -259,12 +235,6 @@ export function ProfileSetupPage() { maritalStatus: pv.maritalStatus, }, }).unwrap(); - - await profileTrigger({ - url: `/profiles/${profileResult.id}`, - method: 'PUT', - body: { isComplete: true }, - }).unwrap(); authStorage.setProfileId(profileResult.id); await addressTrigger({ diff --git a/apps/portal/src/app/features/profile/pages/ProfilePage.tsx b/apps/portal/src/app/features/profile/pages/ProfilePage.tsx index 16c967533..c4361492a 100644 --- a/apps/portal/src/app/features/profile/pages/ProfilePage.tsx +++ b/apps/portal/src/app/features/profile/pages/ProfilePage.tsx @@ -131,19 +131,16 @@ export function ProfilePage() { const [loadedProfile, setLoadedProfile] = useState(null); const [loadedAddress, setLoadedAddress] = useState(null); + const [profileId, setProfileId] = useState(null); const [addressId, setAddressId] = useState(null); const [dataLoading, setDataLoading] = useState(true); - const profileId = authStorage.getProfileId(); - useEffect(() => { - if (!profileId) { - setDataLoading(false); - return; - } - fetchProfile({ url: `/profiles/${profileId}?i=address,profession`, method: 'GET' }) + if (!user) return; + fetchProfile({ url: `/profiles/by-user/${user.id}`, method: 'GET' }) .unwrap() .then((data) => { + setProfileId(data.id); setLoadedProfile({ professionId: data.professionId || data.profession?.id || '', firstName: data.firstName || '', @@ -181,7 +178,7 @@ export function ProfilePage() { .catch(() => { setDataLoading(false); }); - }, [profileId, fetchProfile]); + }, [user, fetchProfile]); // Load the latest user from the server on mount useEffect(() => { diff --git a/libs/auth/src/lib/pages/LoginPage.tsx b/libs/auth/src/lib/pages/LoginPage.tsx index 7f998bb4c..5defbc028 100644 --- a/libs/auth/src/lib/pages/LoginPage.tsx +++ b/libs/auth/src/lib/pages/LoginPage.tsx @@ -46,7 +46,7 @@ export function LoginPage() { const [rememberMe, setRememberMe] = useState(true); const [loginTrigger] = useApiMutation(); const [meTrigger] = useApiMutation(); - const [profileCheckTrigger] = useApiMutation<{ total: number; items: Array<{ id: string; user: { id: string }; isComplete: boolean }> }>(); + const [profileCheckTrigger] = useApiMutation<{ id: string }>(); const { register, @@ -72,31 +72,35 @@ export function LoginPage() { }).unwrap(); dispatch(setUser(me)); + let hasProfile = false; try { - const profiles = await profileCheckTrigger({ - url: `/profiles?q=${encodeURIComponent('i=user')}`, + const profile = await profileCheckTrigger({ + url: `/profiles/by-user/${me.id}`, method: 'GET', }).unwrap(); - const userProfile = profiles.items?.find((p) => p.user?.id === me.id); - - if (userProfile?.isComplete) { - authStorage.setProfileId(userProfile.id); - } else { - navigate('/profile-setup'); - return; - } + authStorage.setProfileId(profile.id); + hasProfile = true; } catch { + // profile not found — redirect to setup + } + + if (!me.isPhoneNumberVerified) { + navigate('/otp-verify', { + state: { + email: me.email, + phoneNumber: me.phoneNumber, + needsProfile: !hasProfile, + }, + }); + return; + } + + if (!hasProfile) { navigate('/profile-setup'); return; } - if (me.isPhoneNumberVerified) { - navigate(loginRedirectPath); - } else { - navigate('/otp-verify', { - state: { email: me.email, phoneNumber: me.phoneNumber }, - }); - } + navigate(loginRedirectPath); } catch { notify.error('Invalid email or password'); } finally {