-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add composite Emails field and forbid creation of Email field type
- Loading branch information
1 parent
ef4f2e4
commit bbaa073
Showing
52 changed files
with
866 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ export type FilterType = | |
| 'TEXT' | ||
| 'PHONE' | ||
| 'EMAIL' | ||
| 'EMAILS' | ||
| 'DATE_TIME' | ||
| 'DATE' | ||
| 'NUMBER' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...c/modules/object-record/record-field/meta-types/display/components/EmailsFieldDisplay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useEmailsField } from '@/object-record/record-field/meta-types/hooks/useEmailsField'; | ||
import { EmailsDisplay } from '@/ui/field/display/components/EmailsDisplay'; | ||
|
||
export const EmailsFieldDisplay = () => { | ||
const { fieldValue } = useEmailsField(); | ||
|
||
return <EmailsDisplay value={fieldValue} />; | ||
}; |
53 changes: 53 additions & 0 deletions
53
...es/twenty-front/src/modules/object-record/record-field/meta-types/hooks/useEmailsField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useContext } from 'react'; | ||
import { useRecoilState, useRecoilValue } from 'recoil'; | ||
|
||
import { usePersistField } from '@/object-record/record-field/hooks/usePersistField'; | ||
import { useRecordFieldInput } from '@/object-record/record-field/hooks/useRecordFieldInput'; | ||
import { FieldEmailsValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { isFieldEmails } from '@/object-record/record-field/types/guards/isFieldEmails'; | ||
import { emailsSchema } from '@/object-record/record-field/types/guards/isFieldEmailsValue'; | ||
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector'; | ||
import { FieldMetadataType } from '~/generated-metadata/graphql'; | ||
|
||
import { FieldContext } from '../../contexts/FieldContext'; | ||
import { assertFieldMetadata } from '../../types/guards/assertFieldMetadata'; | ||
|
||
export const useEmailsField = () => { | ||
const { recordId, fieldDefinition, hotkeyScope } = useContext(FieldContext); | ||
|
||
assertFieldMetadata(FieldMetadataType.Emails, isFieldEmails, fieldDefinition); | ||
|
||
const fieldName = fieldDefinition.metadata.fieldName; | ||
|
||
const [fieldValue, setFieldValue] = useRecoilState<FieldEmailsValue>( | ||
recordStoreFamilySelector({ | ||
recordId, | ||
fieldName: fieldName, | ||
}), | ||
); | ||
|
||
const { setDraftValue, getDraftValueSelector } = | ||
useRecordFieldInput<FieldEmailsValue>(`${recordId}-${fieldName}`); | ||
|
||
const draftValue = useRecoilValue(getDraftValueSelector()); | ||
|
||
const persistField = usePersistField(); | ||
|
||
const persistEmailsField = (nextValue: FieldEmailsValue) => { | ||
try { | ||
persistField(emailsSchema.parse(nextValue)); | ||
} catch { | ||
return; | ||
} | ||
}; | ||
|
||
return { | ||
fieldDefinition, | ||
fieldValue, | ||
draftValue, | ||
setDraftValue, | ||
setFieldValue, | ||
hotkeyScope, | ||
persistEmailsField, | ||
}; | ||
}; |
23 changes: 23 additions & 0 deletions
23
...ty-front/src/modules/object-record/record-field/meta-types/hooks/useEmailsFieldDisplay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { useRecordFieldValue } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext'; | ||
|
||
import { FieldEmailsValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { FieldContext } from '../../contexts/FieldContext'; | ||
|
||
export const useEmailsFieldDisplay = () => { | ||
const { recordId, fieldDefinition, hotkeyScope } = useContext(FieldContext); | ||
|
||
const fieldName = fieldDefinition.metadata.fieldName; | ||
|
||
const fieldValue = useRecordFieldValue<FieldEmailsValue | undefined>( | ||
recordId, | ||
fieldName, | ||
); | ||
|
||
return { | ||
fieldDefinition, | ||
fieldValue, | ||
hotkeyScope, | ||
}; | ||
}; |
57 changes: 57 additions & 0 deletions
57
...t/src/modules/object-record/record-field/meta-types/input/components/EmailsFieldInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useEmailsField } from '@/object-record/record-field/meta-types/hooks/useEmailsField'; | ||
import { EmailsFieldMenuItem } from '@/object-record/record-field/meta-types/input/components/EmailsFieldMenuItem'; | ||
import { useMemo } from 'react'; | ||
import { isDefined } from 'twenty-ui'; | ||
import { MultiItemFieldInput } from './MultiItemFieldInput'; | ||
|
||
type EmailsFieldInputProps = { | ||
onCancel?: () => void; | ||
}; | ||
|
||
export const EmailsFieldInput = ({ onCancel }: EmailsFieldInputProps) => { | ||
const { persistEmailsField, hotkeyScope, fieldValue } = useEmailsField(); | ||
|
||
const emails = useMemo<string[]>( | ||
() => | ||
[ | ||
fieldValue?.primaryEmail ? fieldValue?.primaryEmail : null, | ||
...(fieldValue?.additionalEmails ?? []), | ||
].filter(isDefined), | ||
[fieldValue?.primaryEmail, fieldValue?.additionalEmails], | ||
); | ||
|
||
const handlePersistEmails = (updatedEmails: string[]) => { | ||
const [nextPrimaryEmail, ...nextAdditionalEmails] = updatedEmails; | ||
persistEmailsField({ | ||
primaryEmail: nextPrimaryEmail ?? '', | ||
additionalEmails: nextAdditionalEmails, | ||
}); | ||
}; | ||
|
||
return ( | ||
<MultiItemFieldInput | ||
items={emails} | ||
onPersist={handlePersistEmails} | ||
onCancel={onCancel} | ||
placeholder="Email" | ||
renderItem={({ | ||
value: email, | ||
index, | ||
handleEdit, | ||
handleSetPrimary, | ||
handleDelete, | ||
}) => ( | ||
<EmailsFieldMenuItem | ||
key={index} | ||
dropdownId={`${hotkeyScope}-emails-${index}`} | ||
isPrimary={index === 0} | ||
email={email} | ||
onEdit={handleEdit} | ||
onSetAsPrimary={handleSetPrimary} | ||
onDelete={handleDelete} | ||
/> | ||
)} | ||
hotkeyScope={hotkeyScope} | ||
/> | ||
); | ||
}; |
32 changes: 32 additions & 0 deletions
32
...rc/modules/object-record/record-field/meta-types/input/components/EmailsFieldMenuItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { EmailDisplay } from '@/ui/field/display/components/EmailDisplay'; | ||
import { MultiItemFieldMenuItem } from './MultiItemFieldMenuItem'; | ||
|
||
type EmailsFieldMenuItemProps = { | ||
dropdownId: string; | ||
isPrimary?: boolean; | ||
onEdit?: () => void; | ||
onSetAsPrimary?: () => void; | ||
onDelete?: () => void; | ||
email: string; | ||
}; | ||
|
||
export const EmailsFieldMenuItem = ({ | ||
dropdownId, | ||
isPrimary, | ||
onEdit, | ||
onSetAsPrimary, | ||
onDelete, | ||
email, | ||
}: EmailsFieldMenuItemProps) => { | ||
return ( | ||
<MultiItemFieldMenuItem | ||
dropdownId={dropdownId} | ||
isPrimary={isPrimary} | ||
value={email} | ||
onEdit={onEdit} | ||
onSetAsPrimary={onSetAsPrimary} | ||
onDelete={onDelete} | ||
DisplayComponent={EmailDisplay} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.