mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-09-07 09:35:46 +00:00
refactor: simplify profile lookup and navigation logic commit
This commit is contained in:
@@ -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<unknown>();
|
||||
const [meTrigger] = useApiMutation<{ id: string }>();
|
||||
const [profileCheckTrigger] = useApiMutation<{ total: number; items: Array<{ id: string; user: { id: string }; isComplete: boolean }> }>();
|
||||
const [meTrigger] = useApiMutation<AuthUser>();
|
||||
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({
|
||||
|
||||
@@ -131,19 +131,16 @@ export function ProfilePage() {
|
||||
|
||||
const [loadedProfile, setLoadedProfile] = useState<ProfileValues | null>(null);
|
||||
const [loadedAddress, setLoadedAddress] = useState<AddressValues | null>(null);
|
||||
const [profileId, setProfileId] = useState<string | null>(null);
|
||||
const [addressId, setAddressId] = useState<string | null>(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(() => {
|
||||
|
||||
Reference in New Issue
Block a user