Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Use autocomplete input with create for company in deal inputs.

Matthieu Hochlanderf8d6e590caca
diff --git a/src/atomic-crm/companies/AutocompleteCompanyInput.tsx b/src/atomic-crm/companies/AutocompleteCompanyInput.tsx
new file mode 100644
index 00000000..9a94f70a
--- /dev/null
+++ b/src/atomic-crm/companies/AutocompleteCompanyInput.tsx
@@ -0,0 +1,38 @@
+import { useCreate, useGetIdentity, useNotify } from "ra-core";
+import { AutocompleteInput } from "@/components/admin";
+
+export const AutocompleteCompanyInput = () => {
+ const [create] = useCreate();
+ const { identity } = useGetIdentity();
+ const notify = useNotify();
+ const handleCreateCompany = async (name?: string) => {
+ if (!name) return;
+ try {
+ const newCompany = await create(
+ "companies",
+ {
+ data: {
+ name,
+ sales_id: identity?.id,
+ created_at: new Date().toISOString(),
+ },
+ },
+ { returnPromise: true },
+ );
+ return newCompany;
+ } catch {
+ notify("An error occurred while creating the company", {
+ type: "error",
+ });
+ }
+ };
+
+ return (
+ <AutocompleteInput
+ optionText="name"
+ helperText={false}
+ onCreate={handleCreateCompany}
+ createItemLabel="Create %{item}"
+ />
+ );
+};
diff --git a/src/atomic-crm/contacts/ContactInputs.tsx b/src/atomic-crm/contacts/ContactInputs.tsx
index 1e62a8dc..556017e4 100644
--- a/src/atomic-crm/contacts/ContactInputs.tsx
+++ b/src/atomic-crm/contacts/ContactInputs.tsx
@@ -1,11 +1,10 @@
import { Separator } from "@/components/ui/separator";
import { useIsMobile } from "@/hooks/use-mobile";
-import { email, required, useGetIdentity, useCreate, useNotify } from "ra-core";
+import { email, required } from "ra-core";
import * as React from "react";
import { useFormContext } from "react-hook-form";
import {
- AutocompleteInput,
BooleanInput,
ReferenceInput,
TextInput,
@@ -18,6 +17,7 @@ import { isLinkedinUrl } from "../misc/isLinkedInUrl";
import { useConfigurationContext } from "../root/ConfigurationContext";
import type { Sale } from "../types";
import { Avatar } from "./Avatar";
+import { AutocompleteCompanyInput } from "@/atomic-crm/companies/AutocompleteCompanyInput.tsx";
export const ContactInputs = () => {
const isMobile = useIsMobile();
@@ -65,42 +65,12 @@ const ContactIdentityInputs = () => {
};
const ContactPositionInputs = () => {
- const [create] = useCreate();
- const { identity } = useGetIdentity();
- const notify = useNotify();
- const handleCreateCompany = async (name?: string) => {
- if (!name) return;
- try {
- const newCompany = await create(
- "companies",
- {
- data: {
- name,
- sales_id: identity?.id,
- created_at: new Date().toISOString(),
- },
- },
- { returnPromise: true },
- );
- return newCompany;
- } catch {
- notify("An error occurred while creating the company", {
- type: "error",
- });
- }
- };
-
return (
<div className="flex flex-col gap-4">
<h6 className="text-lg font-semibold">Position</h6>
<TextInput source="title" helperText={false} />
<ReferenceInput source="company_id" reference="companies">
- <AutocompleteInput
- optionText="name"
- helperText={false}
- onCreate={handleCreateCompany}
- createItemLabel="Create %{item}"
- />
+ <AutocompleteCompanyInput />
</ReferenceInput>
</div>
);
diff --git a/src/atomic-crm/deals/DealInputs.tsx b/src/atomic-crm/deals/DealInputs.tsx
index 09895d08..7eb50b9e 100644
--- a/src/atomic-crm/deals/DealInputs.tsx
+++ b/src/atomic-crm/deals/DealInputs.tsx
@@ -1,6 +1,5 @@
import {
AutocompleteArrayInput,
- AutocompleteInput,
ReferenceArrayInput,
ReferenceInput,
TextInput,
@@ -12,6 +11,7 @@ import { useIsMobile } from "@/hooks/use-mobile";
import { required } from "ra-core";
import { contactOptionText } from "../misc/ContactOption";
import { useConfigurationContext } from "../root/ConfigurationContext";
+import { AutocompleteCompanyInput } from "@/atomic-crm/companies/AutocompleteCompanyInput.tsx";
export const DealInputs = () => {
const isMobile = useIsMobile();
@@ -47,11 +47,7 @@ const DealLinkedToInputs = () => {
<div className="flex flex-col gap-4 flex-1">
<h3 className="text-base font-medium">Linked to</h3>
<ReferenceInput source="company_id" reference="companies">
- <AutocompleteInput
- optionText="name"
- validate={required()}
- helperText={false}
- />
+ <AutocompleteCompanyInput />
</ReferenceInput>
<ReferenceArrayInput source="contact_ids" reference="contacts_summary">