refactor: remove unused application query and simplify application creation logic

This commit is contained in:
Nati
2026-08-19 05:55:53 +00:00
parent 629b02fd26
commit d70b2e76cd

View File

@@ -45,7 +45,6 @@ import {
useGetApplicationQuery, useGetApplicationQuery,
useGetAttachmentsQuery, useGetAttachmentsQuery,
useGetLicenseTypeRequirementsQuery, useGetLicenseTypeRequirementsQuery,
useGetMyApplicationsQuery,
useGetMyVesselsQuery, useGetMyVesselsQuery,
usePatchSectionMutation, usePatchSectionMutation,
useRemoveStaffMutation, useRemoveStaffMutation,
@@ -104,42 +103,25 @@ export function LicenseApplicationPage() {
const { data: vessels } = useGetMyVesselsQuery(); const { data: vessels } = useGetMyVesselsQuery();
const [createApplication] = useCreateApplicationMutation(); const [createApplication] = useCreateApplicationMutation();
const [appId, setAppId] = useState<string | undefined>(applicationId); const [appId, setAppId] = useState<string | undefined>(applicationId);
// Only fetched to recover from the 409 below — a fresh visit never needs
// the applicant's whole application list, so this stays lazy.
const { data: myApplications, refetch: fetchMyApplications } =
useGetMyApplicationsQuery(undefined, { skip: true });
// Create (or resume) the draft up front, so uploads have a real owner to // Create (or resume) the draft up front, so uploads have a real owner to
// attach to and nothing is lost if the browser is closed mid-wizard. // attach to and nothing is lost if the browser is closed mid-wizard.
// For a one-shot registration (e.g. seafarer) already submitted or further
// along, the API returns that existing application instead of a new draft —
// "Apply" reopens it rather than erroring, the same way it reopens a DRAFT.
useEffect(() => { useEffect(() => {
if (appId || !config) return; if (appId || !config) return;
createApplication({ licenseType: typeCode }) createApplication({ licenseType: typeCode })
.unwrap() .unwrap()
.then((app) => setAppId(app.id)) .then((app) => setAppId(app.id))
.catch(async (err) => { .catch((err) =>
// A one-shot registration (e.g. seafarer) already has a submitted (or
// further along) application — the backend refuses a second one
// rather than silently resuming it, unlike an unfinished DRAFT. The
// applicant's intent was still "open my registration", so find the
// existing one and load it instead of leaving the page stuck on this
// toast with nothing to fetch.
if (err?.status === 409) {
const mine = myApplications ?? (await fetchMyApplications().unwrap());
const existing = mine.items.find(
(a) => a.licenseTypeId === config.licenseType.id,
);
if (existing) {
setAppId(existing.id);
return;
}
}
notifications.show({ notifications.show({
color: "red", color: "red",
title: "Could not start application", title: "Could not start application",
message: extractErrorMessage(err), message: extractErrorMessage(err),
}); }),
}); );
}, [appId, config, createApplication, typeCode, myApplications, fetchMyApplications]); }, [appId, config, createApplication, typeCode]);
const { data: detail, refetch } = useGetApplicationQuery(appId as string, { const { data: detail, refetch } = useGetApplicationQuery(appId as string, {
skip: !appId, skip: !appId,