Skip to content

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

code improvement

ThieryMichel403fa6e02614
diff --git a/src/components/atomic-crm/contacts/ContactCreate.tsx b/src/components/atomic-crm/contacts/ContactCreate.tsx
index 95c027ab..056aab71 100644
--- a/src/components/atomic-crm/contacts/ContactCreate.tsx
+++ b/src/components/atomic-crm/contacts/ContactCreate.tsx
@@ -1,33 +1,24 @@
import { CreateBase, Form, useGetIdentity } from "ra-core";
import { Card, CardContent } from "@/components/ui/card";
-import type { Contact } from "../types";
import { ContactInputs } from "./ContactInputs";
import { FormToolbar } from "../layout/FormToolbar";
-
-const transformContact = (data: Contact) => {
- const cleanedEmailJsonb =
- data.email_jsonb?.filter((e) => e.email !== null) || [];
- const cleanedPhoneJsonb =
- data.phone_jsonb?.filter((p) => p.number !== null) || [];
- return {
- ...data,
- first_seen: new Date().toISOString(),
- last_seen: new Date().toISOString(),
- tags: [],
- phone_jsonb: cleanedPhoneJsonb.length > 0 ? cleanedPhoneJsonb : null,
- email_jsonb: cleanedEmailJsonb.length > 0 ? cleanedEmailJsonb : null,
- };
-};
+import { cleanupContactForCreate, defaultEmailJsonb } from "./cleanupContact";
export const ContactCreate = () => {
const { identity } = useGetIdentity();
return (
- <CreateBase redirect="show" transform={transformContact}>
+ <CreateBase redirect="show" transform={cleanupContactForCreate}>
<div className="mt-2 flex lg:mr-72">
<div className="flex-1">
- <Form defaultValues={{ sales_id: identity?.id }}>
+ <Form
+ defaultValues={{
+ sales_id: identity?.id,
+ email_jsonb: defaultEmailJsonb,
+ phone_jsonb: defaultEmailJsonb,
+ }}
+ >
<Card>
<CardContent>
<ContactInputs />
diff --git a/src/components/atomic-crm/contacts/ContactEdit.tsx b/src/components/atomic-crm/contacts/ContactEdit.tsx
index a03164cb..7948a913 100644
--- a/src/components/atomic-crm/contacts/ContactEdit.tsx
+++ b/src/components/atomic-crm/contacts/ContactEdit.tsx
@@ -5,21 +5,14 @@ import type { Contact } from "../types";
import { ContactAside } from "./ContactAside";
import { ContactInputs } from "./ContactInputs";
import { FormToolbar } from "../layout/FormToolbar";
-
-const transformContact = (data: Contact) => {
- const cleanedEmailJsonb =
- data.email_jsonb?.filter((e) => e.email !== null) || [];
- const cleanedPhoneJsonb =
- data.phone_jsonb?.filter((p) => p.number !== null) || [];
- return {
- ...data,
- phone_jsonb: cleanedPhoneJsonb.length > 0 ? cleanedPhoneJsonb : null,
- email_jsonb: cleanedEmailJsonb.length > 0 ? cleanedEmailJsonb : null,
- };
-};
+import {
+ cleanupContactForEdit,
+ defaultEmailJsonb,
+ defaultPhoneJsonb,
+} from "./cleanupContact";
export const ContactEdit = () => (
- <EditBase redirect="show" transform={transformContact}>
+ <EditBase redirect="show" transform={cleanupContactForEdit}>
<ContactEditContent />
</EditBase>
);
@@ -29,11 +22,11 @@ const normalizeContactArrayFields = (record: Contact) => ({
email_jsonb:
record.email_jsonb && record.email_jsonb.length > 0
? record.email_jsonb
- : [{ email: null, type: null }],
+ : defaultEmailJsonb,
phone_jsonb:
record.phone_jsonb && record.phone_jsonb.length > 0
? record.phone_jsonb
- : [{ number: null, type: null }],
+ : defaultPhoneJsonb,
});
const ContactEditContent = () => {
diff --git a/src/components/atomic-crm/contacts/ContactInputs.tsx b/src/components/atomic-crm/contacts/ContactInputs.tsx
index 87bcbab3..0a7fadb7 100644
--- a/src/components/atomic-crm/contacts/ContactInputs.tsx
+++ b/src/components/atomic-crm/contacts/ContactInputs.tsx
@@ -134,11 +134,7 @@ const ContactPersonalInformationInputs = () => {
<h6 className="text-lg font-semibold">
{translate("resources.contacts.field_categories.personal_info")}
</h6>
- <ArrayInput
- source="email_jsonb"
- helperText={false}
- defaultValue={[{ email: null, type: null }]}
- >
+ <ArrayInput source="email_jsonb" helperText={false}>
<SimpleFormIterator
inline
disableReordering
@@ -166,11 +162,7 @@ const ContactPersonalInformationInputs = () => {
/>
</SimpleFormIterator>
</ArrayInput>
- <ArrayInput
- source="phone_jsonb"
- helperText={false}
- defaultValue={[{ number: null, type: null }]}
- >
+ <ArrayInput source="phone_jsonb" helperText={false}>
<SimpleFormIterator
inline
disableReordering
diff --git a/src/components/atomic-crm/contacts/cleanupContact.ts b/src/components/atomic-crm/contacts/cleanupContact.ts
new file mode 100644
index 00000000..5345043f
--- /dev/null
+++ b/src/components/atomic-crm/contacts/cleanupContact.ts
@@ -0,0 +1,27 @@
+import type { Contact } from "../types";
+
+export const defaultEmailJsonb = [{ email: null, type: null }];
+export const defaultPhoneJsonb = [{ number: null, type: null }];
+
+const cleanContactArrayFields = (data: Contact) => {
+ const cleanedEmailJsonb =
+ data.email_jsonb?.filter((e) => e.email != null) || [];
+ const cleanedPhoneJsonb =
+ data.phone_jsonb?.filter((p) => p.number != null) || [];
+ return {
+ ...data,
+ phone_jsonb: cleanedPhoneJsonb.length > 0 ? cleanedPhoneJsonb : null,
+ email_jsonb: cleanedEmailJsonb.length > 0 ? cleanedEmailJsonb : null,
+ };
+};
+
+export const cleanupContactForCreate = (data: Contact) => {
+ return cleanContactArrayFields({
+ ...data,
+ first_seen: new Date().toISOString(),
+ last_seen: new Date().toISOString(),
+ tags: [],
+ });
+};
+
+export const cleanupContactForEdit = cleanContactArrayFields;