diff --git a/apps/portal/src/app/features/profile/pages/ProfilePage.tsx b/apps/portal/src/app/features/profile/pages/ProfilePage.tsx index 9efa3855e..73d9a0f34 100644 --- a/apps/portal/src/app/features/profile/pages/ProfilePage.tsx +++ b/apps/portal/src/app/features/profile/pages/ProfilePage.tsx @@ -49,7 +49,7 @@ import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { useTranslation } from 'react-i18next'; -import { notify, PageHeader, useErrorHandler, passwordSchema as strongPasswordSchema, PasswordRequirements, getCountryCode, splitPersonName, joinPersonName } from '@ema-platform/ui'; +import { notify, PageHeader, useErrorHandler, passwordSchema as strongPasswordSchema, PasswordRequirements, getCountryCode } from '@ema-platform/ui'; import { useApiMutation, useLocalized } from '@ema-platform/api'; import { PORTAL_PERMISSIONS, setUser, useCurrentProfile, usePermissions } from '@ema-platform/auth'; import { SUPPORTED_LANGUAGES, type AppLanguage } from '../../../i18n/config'; @@ -89,6 +89,15 @@ function getInitials(name: string, fallback: string) { return letters.toUpperCase(); } +function splitProfileName(fullName: string) { + const [firstName = '', middleName = '', ...lastName] = fullName.trim().split(/\s+/); + return { firstName, middleName, lastName: lastName.join(' ') }; +} + +function formatProfileName({ firstName, middleName, lastName }: Pick) { + return [firstName, middleName, lastName].filter(Boolean).join(' '); +} + function normalizeName(name: string) { return name.trim().replace(/\s+/g, ' '); } @@ -196,7 +205,7 @@ export function ProfilePage() { // already holds so the form does not flash empty on a refetch. const currentProfile = resolvedProfile ?? storedProfile; if (currentProfile) { - const accountName = user?.name?.en ? splitPersonName(user.name.en) : null; + const accountName = user?.name?.en ? splitProfileName(user.name.en) : null; setLoadedProfile({ professionId: currentProfile.professionId || currentProfile.profession?.id || '', firstName: accountName?.firstName || currentProfile.firstName || '', @@ -277,7 +286,7 @@ export function ProfilePage() { setIsSavingProfile(true); try { - const profileName = splitPersonName(values.nameEn); + const profileName = splitProfileName(values.nameEn); const saves: Promise[] = [ updateTrigger({ url: '/auth/update-profile', @@ -354,7 +363,7 @@ export function ProfilePage() { const onSaveProfile = async (values: ProfileValues) => { if (!profileId) return; - const fullName = joinPersonName(values); + const fullName = formatProfileName(values); if (user && normalizeName(fullName) !== normalizeName(user.name.en)) { notify.error(t('profile.nameMismatch')); return; diff --git a/libs/auth/src/lib/pages/SignupPage.tsx b/libs/auth/src/lib/pages/SignupPage.tsx index c8d4133ad..90d72b650 100644 --- a/libs/auth/src/lib/pages/SignupPage.tsx +++ b/libs/auth/src/lib/pages/SignupPage.tsx @@ -29,7 +29,7 @@ import { useNavigate, Link } from 'react-router-dom'; import { useDispatch } from 'react-redux'; import { useTranslation } from 'react-i18next'; import { useApiMutation } from '@ema-platform/api'; -import { useErrorHandler, passwordSchema, PasswordRequirements, joinPersonName } from '@ema-platform/ui'; +import { useErrorHandler, passwordSchema, PasswordRequirements } from '@ema-platform/ui'; import { AuthShell } from '../components/AuthShell'; import { loginSuccess, setUser } from '../store/auth.slice'; import type { AuthUser } from '../types/auth.types'; @@ -124,7 +124,7 @@ export function SignupPage() { username: values.username, phoneNumber: values.phoneNumber, userType: values.userType, - name: { en: joinPersonName(values), am: values.nameAm ?? '' }, + name: { en: values.nameEn, am: values.nameAm ?? '' }, password: values.password, confirmPassword: values.confirmPassword, }; @@ -213,38 +213,23 @@ export function SignupPage() {
- - } - error={errors.firstName?.message} - {...register('firstName')} - /> + } - error={errors.middleName?.message} - {...register('middleName')} + error={errors.nameEn?.message} + {...register('nameEn')} /> } - error={errors.lastName?.message} - {...register('lastName')} + error={errors.nameAm?.message} + {...register('nameAm')} /> - } - error={errors.nameAm?.message} - {...register('nameAm')} - /> -