mirror of
https://github.com/Tria-plc/emaui.git
synced 2026-08-26 13:02:50 +00:00
feat(seafarer-registration): update profile handling and address submission logic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useRef, useState } from 'react';
|
||||
import { useApiMutation } from '@ema-platform/api';
|
||||
import { useAppSelector } from '../../../store/hooks';
|
||||
import { useCurrentProfile, useUpdateMyProfileMutation } from '@ema-platform/auth';
|
||||
import { useSaveMyAddressMutation } from '../../profile/api/address-api';
|
||||
import {
|
||||
Alert,
|
||||
Badge,
|
||||
@@ -249,12 +249,15 @@ function DocCard({
|
||||
// ---------------------------------------------------------------------------
|
||||
export function SeafarerRegistrationPage() {
|
||||
const navigate = useNavigate();
|
||||
const user = useAppSelector((state) => state.auth.user);
|
||||
// Every signed-in user already has a profile (`/profiles/me` provisions
|
||||
// one), so registering fills that row in rather than creating a second —
|
||||
// POST /profiles trips the unique user_id constraint.
|
||||
const { profileId } = useCurrentProfile();
|
||||
const [active, setActive] = useState(0);
|
||||
const [completed, setCompleted] = useState<number[]>([]);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [profileTrigger] = useApiMutation<{ id: string }>();
|
||||
const [addressTrigger] = useApiMutation<unknown>();
|
||||
const [updateProfile] = useUpdateMyProfileMutation();
|
||||
const [saveAddress] = useSaveMyAddressMutation();
|
||||
|
||||
// Step 1 — Personal Information
|
||||
const [firstName, setFirstName] = useState<BilingualValue>({ en: '', am: '' });
|
||||
@@ -301,13 +304,12 @@ export function SeafarerRegistrationPage() {
|
||||
const prev = () => setActive((c) => c - 1);
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!profileId) return;
|
||||
setSubmitting(true);
|
||||
try {
|
||||
const profileResult = await profileTrigger({
|
||||
url: '/profiles',
|
||||
method: 'POST',
|
||||
await updateProfile({
|
||||
id: profileId,
|
||||
body: {
|
||||
userId: user?.id,
|
||||
type: 'SEAFARER',
|
||||
firstName: firstName.en,
|
||||
middleName: middleName.en || undefined,
|
||||
@@ -319,15 +321,15 @@ export function SeafarerRegistrationPage() {
|
||||
},
|
||||
}).unwrap();
|
||||
|
||||
await addressTrigger({
|
||||
url: `/addresss/profile/${profileResult.id}`,
|
||||
method: 'POST',
|
||||
await saveAddress({
|
||||
profileId,
|
||||
body: {
|
||||
idType: 'NID',
|
||||
idNumber: nationalIdNumber,
|
||||
nationality: nationality ?? 'Ethiopian',
|
||||
primaryPhoneNumber: mobile,
|
||||
email: email || undefined,
|
||||
website: null,
|
||||
streetAddress: permanentAddress || undefined,
|
||||
emergencyContactName: emergencyName || undefined,
|
||||
emergencyContactPhone: emergencyPhone || undefined,
|
||||
@@ -335,7 +337,7 @@ export function SeafarerRegistrationPage() {
|
||||
},
|
||||
}).unwrap();
|
||||
|
||||
notify.success(`Registration submitted! Profile ID: ${profileResult.id.slice(0, 8).toUpperCase()}`);
|
||||
notify.success(`Registration submitted! Profile ID: ${profileId.slice(0, 8).toUpperCase()}`);
|
||||
navigate('/seafarer-registry');
|
||||
} catch {
|
||||
notify.error('Submission failed. Please try again.');
|
||||
|
||||
Reference in New Issue
Block a user