A full-featured CRM built with React, shadcn/ui, and Supabase.
move all non component contact logic into contactModel
ThieryMichelacf499804a1e
diff --git a/registry.json b/registry.jsonindex 806a7a49..1825eed7 100644--- a/registry.json+++ b/registry.json@@ -646,15 +646,7 @@"type": "registry:component"},{- "path": "src/components/atomic-crm/contacts/exportToVCard.ts",- "type": "registry:component"- },- {- "path": "src/components/atomic-crm/contacts/contactGender.ts",- "type": "registry:component"- },- {- "path": "src/components/atomic-crm/contacts/cleanupContact.ts",+ "path": "src/components/atomic-crm/contacts/contactModel.ts","type": "registry:component"},{diff --git a/src/components/atomic-crm/contacts/ContactCreate.tsx b/src/components/atomic-crm/contacts/ContactCreate.tsxindex cdc8e1a4..d8623140 100644--- a/src/components/atomic-crm/contacts/ContactCreate.tsx+++ b/src/components/atomic-crm/contacts/ContactCreate.tsx@@ -7,7 +7,7 @@ import {cleanupContactForCreate,defaultEmailJsonb,defaultPhoneJsonb,-} from "./cleanupContact";+} from "./contactModel";export const ContactCreate = ({mutationMode,diff --git a/src/components/atomic-crm/contacts/ContactCreateSheet.tsx b/src/components/atomic-crm/contacts/ContactCreateSheet.tsxindex 0c9eecae..c24e8646 100644--- a/src/components/atomic-crm/contacts/ContactCreateSheet.tsx+++ b/src/components/atomic-crm/contacts/ContactCreateSheet.tsx@@ -1,7 +1,7 @@import { useGetIdentity, useTranslate } from "ra-core";import { CreateSheet } from "../misc/CreateSheet";import { ContactInputs } from "./ContactInputs";-import type { Contact } from "../types";+import { cleanupContactForCreate } from "./contactModel";export interface ContactCreateSheetProps {open: boolean;@@ -19,12 +19,7 @@ export const ContactCreateSheet = ({resource="contacts"title={translate("resources.contacts.action.new")}defaultValues={{ sales_id: identity?.id }}- transform={(data: Contact) => ({- ...data,- first_seen: new Date().toISOString(),- last_seen: new Date().toISOString(),- tags: [],- })}+ transform={cleanupContactForCreate}open={open}onOpenChange={onOpenChange}>diff --git a/src/components/atomic-crm/contacts/ContactEdit.tsx b/src/components/atomic-crm/contacts/ContactEdit.tsxindex 97a943c8..1994c092 100644--- a/src/components/atomic-crm/contacts/ContactEdit.tsx+++ b/src/components/atomic-crm/contacts/ContactEdit.tsx@@ -9,7 +9,7 @@ import {cleanupContactForEdit,defaultEmailJsonb,defaultPhoneJsonb,-} from "./cleanupContact";+} from "./contactModel";export const ContactEdit = ({mutationMode,diff --git a/src/components/atomic-crm/contacts/ContactEditSheet.tsx b/src/components/atomic-crm/contacts/ContactEditSheet.tsxindex bcc9fe77..2fe67840 100644--- a/src/components/atomic-crm/contacts/ContactEditSheet.tsx+++ b/src/components/atomic-crm/contacts/ContactEditSheet.tsx@@ -1,6 +1,7 @@import type { Identifier } from "ra-core";import { EditSheet } from "../misc/EditSheet";import { ContactInputs } from "./ContactInputs";+import { cleanupContactForEdit } from "./contactModel";export interface ContactEditSheetProps {open: boolean;@@ -19,6 +20,7 @@ export const ContactEditSheet = ({id={contactId}open={open}onOpenChange={onOpenChange}+ transform={cleanupContactForEdit}><ContactInputs /></EditSheet>diff --git a/src/components/atomic-crm/contacts/ContactInputs.tsx b/src/components/atomic-crm/contacts/ContactInputs.tsxindex 0a7fadb7..94a8b585 100644--- a/src/components/atomic-crm/contacts/ContactInputs.tsx+++ b/src/components/atomic-crm/contacts/ContactInputs.tsx@@ -1,5 +1,5 @@import { email, required, useTranslate } from "ra-core";-import { type FocusEvent, type ClipboardEventHandler } from "react";+import type { FocusEvent, ClipboardEventHandler } from "react";import { useFormContext } from "react-hook-form";import { Separator } from "@/components/ui/separator";import { useIsMobile } from "@/hooks/use-mobile";@@ -12,14 +12,14 @@ import { ArrayInput } from "@/components/admin/array-input";import { SimpleFormIterator } from "@/components/admin/simple-form-iterator";import { isLinkedinUrl } from "../misc/isLinkedInUrl";+import type { Sale } from "../types";+import { Avatar } from "./Avatar";+import { AutocompleteCompanyInput } from "../companies/AutocompleteCompanyInput.tsx";import {contactGender,translateContactGenderLabel,translatePersonalInfoTypeLabel,-} from "./contactGender";-import type { Sale } from "../types";-import { Avatar } from "./Avatar";-import { AutocompleteCompanyInput } from "../companies/AutocompleteCompanyInput.tsx";+} from "./contactModel.ts";export const ContactInputs = () => {const isMobile = useIsMobile();diff --git a/src/components/atomic-crm/contacts/ContactPersonalInfo.tsx b/src/components/atomic-crm/contacts/ContactPersonalInfo.tsxindex 802c2e3c..2bb377da 100644--- a/src/components/atomic-crm/contacts/ContactPersonalInfo.tsx+++ b/src/components/atomic-crm/contacts/ContactPersonalInfo.tsx@@ -10,7 +10,7 @@ import {contactGender,translateContactGenderLabel,translatePersonalInfoTypeLabel,-} from "./contactGender";+} from "./contactModel";import type { Contact } from "../types";export const ContactPersonalInfo = () => {diff --git a/src/components/atomic-crm/contacts/ExportVCardButton.tsx b/src/components/atomic-crm/contacts/ExportVCardButton.tsxindex 78389d0d..89d28517 100644--- a/src/components/atomic-crm/contacts/ExportVCardButton.tsx+++ b/src/components/atomic-crm/contacts/ExportVCardButton.tsx@@ -2,7 +2,7 @@ import { Download } from "lucide-react";import { useGetOne, useRecordContext, useTranslate } from "ra-core";import { Button } from "@/components/ui/button";import type { Contact, Company } from "../types";-import { exportToVCard } from "./exportToVCard";+import { exportToVCard } from "./contactModel";export const ExportVCardButton = () => {const contact = useRecordContext<Contact>();diff --git a/src/components/atomic-crm/contacts/cleanupContact.ts b/src/components/atomic-crm/contacts/cleanupContact.tsdeleted file mode 100644index 5345043f..00000000--- a/src/components/atomic-crm/contacts/cleanupContact.ts+++ /dev/null@@ -1,27 +0,0 @@-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;diff --git a/src/components/atomic-crm/contacts/contactGender.ts b/src/components/atomic-crm/contacts/contactGender.tsdeleted file mode 100644index f0ed6ca2..00000000--- a/src/components/atomic-crm/contacts/contactGender.ts+++ /dev/null@@ -1,54 +0,0 @@-import { Mars, NonBinary, Venus } from "lucide-react";--import type { ContactGender } from "../types";--type TranslateFn = (key: string, options?: { [key: string]: any }) => string;--export const contactGenderDefaultLabels: Record<string, string> = {- male: "He/Him",- female: "She/Her",- nonbinary: "They/Them",-};--const personalInfoTypeMap: Record<string, string> = {- Work: "work",- Home: "home",- Other: "other",-};--export const contactGender: ContactGender[] = [- {- value: "male",- label: "resources.contacts.inputs.genders.male",- icon: Mars,- },- {- value: "female",- label: "resources.contacts.inputs.genders.female",- icon: Venus,- },- {- value: "nonbinary",- label: "resources.contacts.inputs.genders.nonbinary",- icon: NonBinary,- },-];--export const translateContactGenderLabel = (- gender: { value: string; label: string },- translate: TranslateFn,-) =>- translate(gender.label, {- _: contactGenderDefaultLabels[gender.value] ?? gender.label,- });--export const translatePersonalInfoTypeLabel = (- type: string,- translate: TranslateFn,-) =>- translate(- `resources.contacts.inputs.personal_info_types.${personalInfoTypeMap[type] ?? type.toLowerCase()}`,- {- _: type,- },- );diff --git a/src/components/atomic-crm/contacts/exportToVCard.ts b/src/components/atomic-crm/contacts/contactModel.tssimilarity index 57%rename from src/components/atomic-crm/contacts/exportToVCard.tsrename to src/components/atomic-crm/contacts/contactModel.tsindex 6ef75ab9..157d898e 100644--- a/src/components/atomic-crm/contacts/exportToVCard.ts+++ b/src/components/atomic-crm/contacts/contactModel.ts@@ -1,4 +1,83 @@-import type { Contact, Company } from "../types";+import { Mars, NonBinary, Venus } from "lucide-react";++import type { Company, Contact, ContactGender } 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;++type TranslateFn = (key: string, options?: { [key: string]: any }) => string;++export const contactGenderDefaultLabels: Record<string, string> = {+ male: "He/Him",+ female: "She/Her",+ nonbinary: "They/Them",+};++const personalInfoTypeMap: Record<string, string> = {+ Work: "work",+ Home: "home",+ Other: "other",+};++export const contactGender: ContactGender[] = [+ {+ value: "male",+ label: "resources.contacts.inputs.genders.male",+ icon: Mars,+ },+ {+ value: "female",+ label: "resources.contacts.inputs.genders.female",+ icon: Venus,+ },+ {+ value: "nonbinary",+ label: "resources.contacts.inputs.genders.nonbinary",+ icon: NonBinary,+ },+];++export const translateContactGenderLabel = (+ gender: { value: string; label: string },+ translate: TranslateFn,+) =>+ translate(gender.label, {+ _: contactGenderDefaultLabels[gender.value] ?? gender.label,+ });++export const translatePersonalInfoTypeLabel = (+ type: string,+ translate: TranslateFn,+) =>+ translate(+ `resources.contacts.inputs.personal_info_types.${personalInfoTypeMap[type] ?? type.toLowerCase()}`,+ {+ _: type,+ },+ );/*** Folds a long line according to vCard specification (max 75 chars per line)diff --git a/src/components/atomic-crm/misc/useImportFromJson.ts b/src/components/atomic-crm/misc/useImportFromJson.tsindex b58dfe86..4b636e30 100644--- a/src/components/atomic-crm/misc/useImportFromJson.ts+++ b/src/components/atomic-crm/misc/useImportFromJson.ts@@ -12,7 +12,7 @@ import type { CrmDataProvider } from "../providers/types";import type { RAFile, Tag } from "../types";import { colors } from "../tags/colors";import { useConfigurationContext } from "../root/ConfigurationContext";-import { contactGender } from "../contacts/contactGender";+import { contactGender } from "../contacts/contactModel";export type ImportFromJsonStats = {sales: number;diff --git a/src/components/atomic-crm/providers/fakerest/dataGenerator/contacts.ts b/src/components/atomic-crm/providers/fakerest/dataGenerator/contacts.tsindex 92f2f536..8c5bd0f9 100644--- a/src/components/atomic-crm/providers/fakerest/dataGenerator/contacts.ts+++ b/src/components/atomic-crm/providers/fakerest/dataGenerator/contacts.ts@@ -8,7 +8,7 @@ import {} from "faker/locale/en_US";import { defaultNoteStatuses } from "../../../root/defaultConfiguration";-import { contactGender } from "../../../contacts/contactGender";+import { contactGender } from "../../../contacts/contactModel";import type { Company, Contact } from "../../../types";import type { Db } from "./types";import { randomDate, weightedBoolean } from "./utils";