Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"date-fns": "^4.1.0",
"email-validator": "^2.0.4",
"i18next": "^25.3.2",
"need4deed-sdk": "0.0.107",
"need4deed-sdk": "0.0.110",
"next": "15.3.8",
"react": "^19.0.0",
"react-day-picker": "^9.13.0",
Expand Down
37 changes: 30 additions & 7 deletions src/components/Dashboard/NewOpportunity/NewOpportunity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FormInput } from "@/components/core/common";
import { apiPathOpportunity, DashboardRoutes } from "@/config/constants";
import { useMutationQuery } from "@/hooks";
import { useGetCurrentAgent } from "@/hooks/useGetCurrentAgent";
import axios from "axios";
import { ApiOpportunityGet } from "need4deed-sdk";
import i18next from "i18next";
import { useRouter } from "next/navigation";
Expand All @@ -24,14 +25,19 @@ import {
Wrapper,
} from "./styled";

type CreateOpportunityBody = {
type CreateOpportunityResponse = { message: string; data: ApiOpportunityGet };

// The create endpoint (`OpportunityFormDataWithAgentSubmitter`) only accepts the
// opportunity itself + `agent_id`; contact details are persisted via a follow-up
// PATCH (`ApiOpportunityPatch.contact`) once we have the new opportunity id.
type CreateOpportunityVariables = {
title: string;
agent_id?: number;
contact: {
name: string;
phone: string;
email: string;
};
agentId?: number;
};

export function NewOpportunity() {
Expand All @@ -58,9 +64,24 @@ export function NewOpportunity() {
mutate: createOpportunity,
isPending,
error,
} = useMutationQuery<CreateOpportunityBody, { message: string; data: ApiOpportunityGet }>({
apiPath: `${apiPathOpportunity}/`,
method: "post",
} = useMutationQuery<CreateOpportunityVariables, CreateOpportunityResponse>({
mutationFn: async ({ title: oppTitle, agent_id, contact }) => {
const { data: created } = await axios.post<CreateOpportunityResponse>(`${apiPathOpportunity}/`, {
title: oppTitle,
agent_id,
});
const id = created?.data?.id;
if (id) {
// Contact is best-effort: the opportunity already exists and contact is
// editable on its profile, so a failed PATCH must not fail the create.
try {
await axios.patch(`${apiPathOpportunity}/${id}`, { contact });
} catch {
// swallow β€” agent can set contact from the opportunity profile
}
}
return created;
},
onSuccessCallback: (response) => {
const id = response?.data?.id;
if (id) {
Expand All @@ -83,14 +104,16 @@ export function NewOpportunity() {

const handleSubmit = () => {
if (!validate()) return;
// Don't create an unlinked opportunity: the agent must be loaded first.
if (!agent?.id) return;
createOpportunity({
title: title.trim(),
agent_id: agent.id,
contact: {
name: contactName.trim(),
phone: contactPhone.trim(),
email: contactEmail.trim(),
},
agentId: agent?.id,
});
};

Expand Down Expand Up @@ -174,7 +197,7 @@ export function NewOpportunity() {
backgroundcolor="var(--color-aubergine)"
textColor="var(--color-white)"
onClick={handleSubmit}
disabled={isPending}
disabled={isPending || agentLoading || !agent}
/>
</Card>
</Wrapper>
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2412,10 +2412,10 @@ natural-compare@^1.4.0:
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz"
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==

need4deed-sdk@0.0.107:
version "0.0.107"
resolved "https://registry.yarnpkg.com/need4deed-sdk/-/need4deed-sdk-0.0.107.tgz#3b9e0b42970a9ef6a21045cf522bbca1cdeee902"
integrity sha512-UrHmAnlCg2jfBdM6oVYMESfg5H/U9dAAvIN14zWJBHP82NNtScFEqvojdZD9GlCgUxtDqW1chcWsRY/Zzm7Jvg==
need4deed-sdk@0.0.110:
version "0.0.110"
resolved "https://registry.yarnpkg.com/need4deed-sdk/-/need4deed-sdk-0.0.110.tgz#4428170ca81fddd35ec80f7a72825c3e6b29d8e7"
integrity sha512-kHa6B13x3CcHMhexam+9CBfRX9AV9/8K1RrUTDSAVdRcJ0PecNFs3JSscwBkJ7UWqjSKV89xaqVkN6N3qGQnAA==

next@15.3.8:
version "15.3.8"
Expand Down