Skip to content

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

Set first and last name based on email

François Zaninottoa21d98aec44a
diff --git a/src/contacts/ContactInputs.tsx b/src/contacts/ContactInputs.tsx
index 8474c355..9a643001 100644
--- a/src/contacts/ContactInputs.tsx
+++ b/src/contacts/ContactInputs.tsx
@@ -18,6 +18,9 @@ import {
useGetIdentity,
useNotify,
} from 'react-admin';
+import { useFormContext } from 'react-hook-form';
+import { ClipboardEventHandler } from 'react';
+
import { isLinkedinUrl } from '../misc/isLinkedInUrl';
import { useConfigurationContext } from '../root/ConfigurationContext';
import { Sale } from '../types';
@@ -118,10 +121,27 @@ const ContactPositionInputs = () => {
};
const ContactPersonalInformationInputs = () => {
+ const { getValues, setValue } = useFormContext();
+
+ // set first and last name based on email
+ const handleEmailPaste: ClipboardEventHandler<HTMLDivElement> = e => {
+ const email = e.clipboardData?.getData('text/plain');
+ const { first_name, last_name } = getValues();
+ if (first_name || last_name || !email) return;
+ const [first, last] = email.split('@')[0].split('.');
+ setValue('first_name', first.charAt(0).toUpperCase() + first.slice(1));
+ setValue('last_name', last.charAt(0).toUpperCase() + last.slice(1));
+ };
+
return (
<Stack>
<Typography variant="h6">Personal info</Typography>
- <TextInput source="email" helperText={false} validate={email()} />
+ <TextInput
+ source="email"
+ helperText={false}
+ validate={email()}
+ onPaste={handleEmailPaste}
+ />
<Stack gap={1} flexDirection="row">
<TextInput
source="phone_1_number"