-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display and update fields from fromManyObjects relations in Show card #5801
Changes from 4 commits
c2bf742
393723b
aafc80e
d667907
6bd5034
7350e40
505ff39
6d5937f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { FullNameFieldInput } from '@/object-record/record-field/meta-types/inpu | |
import { LinksFieldInput } from '@/object-record/record-field/meta-types/input/components/LinksFieldInput'; | ||
import { MultiSelectFieldInput } from '@/object-record/record-field/meta-types/input/components/MultiSelectFieldInput'; | ||
import { RawJsonFieldInput } from '@/object-record/record-field/meta-types/input/components/RawJsonFieldInput'; | ||
import { RelationManyFieldInput } from '@/object-record/record-field/meta-types/input/components/RelationManyFieldInput'; | ||
import { SelectFieldInput } from '@/object-record/record-field/meta-types/input/components/SelectFieldInput'; | ||
import { RecordFieldInputScope } from '@/object-record/record-field/scopes/RecordFieldInputScope'; | ||
import { isFieldDate } from '@/object-record/record-field/types/guards/isFieldDate'; | ||
|
@@ -14,6 +15,7 @@ import { isFieldFullName } from '@/object-record/record-field/types/guards/isFie | |
import { isFieldLinks } from '@/object-record/record-field/types/guards/isFieldLinks'; | ||
import { isFieldMultiSelect } from '@/object-record/record-field/types/guards/isFieldMultiSelect'; | ||
import { isFieldRawJson } from '@/object-record/record-field/types/guards/isFieldRawJson'; | ||
import { isFieldRelationFromManyObjects } from '@/object-record/record-field/types/guards/isFieldRelationFromManyObjects'; | ||
import { isFieldSelect } from '@/object-record/record-field/types/guards/isFieldSelect'; | ||
import { getScopeIdFromComponentId } from '@/ui/utilities/recoil-scope/utils/getScopeIdFromComponentId'; | ||
|
||
|
@@ -71,7 +73,13 @@ export const FieldInput = ({ | |
recordFieldInputScopeId={getScopeIdFromComponentId(recordFieldInputdId)} | ||
> | ||
{isFieldRelation(fieldDefinition) ? ( | ||
<RelationFieldInput onSubmit={onSubmit} onCancel={onCancel} /> | ||
isFieldRelationFromManyObjects(fieldDefinition) ? ( | ||
<RelationManyFieldInput | ||
relationPickerScopeId={`relation-picker-${fieldDefinition.metadata.fieldName}`} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we have an unique id, maybe based on the field id? Also I believe you can use the function |
||
/> | ||
) : ( | ||
<RelationFieldInput onSubmit={onSubmit} onCancel={onCancel} /> | ||
) | ||
) : isFieldPhone(fieldDefinition) || | ||
isFieldDisplayedAsPhone(fieldDefinition) ? ( | ||
<PhoneFieldInput | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { useContext } from 'react'; | ||
import { useRecoilCallback } from 'recoil'; | ||
|
||
import { FieldDefinition } from '@/object-record/record-field/types/FieldDefinition'; | ||
import { FieldRelationMetadata } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { isFieldAddress } from '@/object-record/record-field/types/guards/isFieldAddress'; | ||
import { isFieldAddressValue } from '@/object-record/record-field/types/guards/isFieldAddressValue'; | ||
import { isFieldDate } from '@/object-record/record-field/types/guards/isFieldDate'; | ||
|
@@ -13,9 +15,11 @@ import { isFieldMultiSelect } from '@/object-record/record-field/types/guards/is | |
import { isFieldMultiSelectValue } from '@/object-record/record-field/types/guards/isFieldMultiSelectValue'; | ||
import { isFieldRawJson } from '@/object-record/record-field/types/guards/isFieldRawJson'; | ||
import { isFieldRawJsonValue } from '@/object-record/record-field/types/guards/isFieldRawJsonValue'; | ||
import { isFieldRelationFromManyObjects } from '@/object-record/record-field/types/guards/isFieldRelationFromManyObjects'; | ||
import { isFieldSelect } from '@/object-record/record-field/types/guards/isFieldSelect'; | ||
import { isFieldSelectValue } from '@/object-record/record-field/types/guards/isFieldSelectValue'; | ||
import { recordStoreFamilySelector } from '@/object-record/record-store/states/selectors/recordStoreFamilySelector'; | ||
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect'; | ||
|
||
import { FieldContext } from '../contexts/FieldContext'; | ||
import { isFieldBoolean } from '../types/guards/isFieldBoolean'; | ||
|
@@ -55,6 +59,11 @@ export const usePersistField = () => { | |
isFieldRelation(fieldDefinition) && | ||
isFieldRelationValue(valueToPersist); | ||
|
||
const fieldIsRelationFromManyObjects = | ||
isFieldRelationFromManyObjects( | ||
fieldDefinition as FieldDefinition<FieldRelationMetadata>, | ||
) && isFieldRelationValue(valueToPersist); | ||
|
||
const fieldIsText = | ||
isFieldText(fieldDefinition) && isFieldTextValue(valueToPersist); | ||
|
||
|
@@ -137,15 +146,20 @@ export const usePersistField = () => { | |
); | ||
|
||
if (fieldIsRelation) { | ||
updateRecord?.({ | ||
variables: { | ||
where: { id: entityId }, | ||
updateOneRecordInput: { | ||
[fieldName]: valueToPersist, | ||
[`${fieldName}Id`]: valueToPersist?.id ?? null, | ||
if (fieldIsRelationFromManyObjects) { | ||
throw new Error('Cannot update this relation.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are not using usePersistField for FromManyObjects as it requires a specific logic that did not feel right to add here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels weird to throw here. You have a boolean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw an error if an update is attempted on a 'fromManyObjects' relation. |
||
} else { | ||
const value = valueToPersist as EntityForSelect; | ||
updateRecord?.({ | ||
variables: { | ||
where: { id: entityId }, | ||
updateOneRecordInput: { | ||
[fieldName]: value, | ||
[`${fieldName}Id`]: value?.id ?? null, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
} | ||
return; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { ObjectMetadataItemsRelationPickerEffect } from '@/object-metadata/components/ObjectMetadataItemsRelationPickerEffect'; | ||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; | ||
import { useUpdateRelationManyFieldInput } from '@/object-record/record-field/meta-types/input/hooks/useUpdateRelationManyFieldInput'; | ||
import { useInlineCell } from '@/object-record/record-inline-cell/hooks/useInlineCell'; | ||
import { MultiRecordSelect } from '@/object-record/relation-picker/components/MultiRecordSelect'; | ||
import { useRelationPicker } from '@/object-record/relation-picker/hooks/useRelationPicker'; | ||
import { useRelationPickerEntitiesOptions } from '@/object-record/relation-picker/hooks/useRelationPickerEntitiesOptions'; | ||
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect'; | ||
|
||
import { useRelationField } from '../../hooks/useRelationField'; | ||
|
||
export const RelationManyFieldInput = ({ | ||
relationPickerScopeId = 'relation-picker', | ||
}: { | ||
relationPickerScopeId?: string; | ||
}) => { | ||
const { closeInlineCell: closeEditableField } = useInlineCell(); | ||
|
||
const { fieldDefinition, fieldValue } = useRelationField<EntityForSelect[]>(); | ||
const { entities, relationPickerSearchFilter } = | ||
useRelationPickerEntitiesOptions({ | ||
relationObjectNameSingular: | ||
fieldDefinition.metadata.relationObjectMetadataNameSingular, | ||
relationPickerScopeId, | ||
}); | ||
|
||
const { setRelationPickerSearchFilter } = useRelationPicker({ | ||
relationPickerScopeId, | ||
}); | ||
|
||
const { handleChange } = useUpdateRelationManyFieldInput({ entities }); | ||
|
||
const { objectMetadataItem } = useObjectMetadataItem({ | ||
objectNameSingular: | ||
fieldDefinition.metadata.relationObjectMetadataNameSingular, | ||
}); | ||
const allRecords = useMemo( | ||
() => [ | ||
...entities.entitiesToSelect.map((entity) => { | ||
const { record, ...recordIdentifier } = entity; | ||
return { | ||
objectMetadataItem: objectMetadataItem, | ||
record: record, | ||
recordIdentifier: recordIdentifier, | ||
}; | ||
}), | ||
], | ||
[entities.entitiesToSelect, objectMetadataItem], | ||
); | ||
|
||
const selectedRecords = useMemo( | ||
() => | ||
allRecords.filter( | ||
(entity) => | ||
fieldValue?.some((f) => { | ||
return f.id === entity.recordIdentifier.id; | ||
}), | ||
), | ||
[allRecords, fieldValue], | ||
); | ||
|
||
return ( | ||
<> | ||
<ObjectMetadataItemsRelationPickerEffect | ||
relationPickerScopeId={relationPickerScopeId} | ||
/> | ||
<MultiRecordSelect | ||
allRecords={allRecords} | ||
selectedObjectRecords={selectedRecords} | ||
loading={entities.loading} | ||
searchFilter={relationPickerSearchFilter} | ||
setSearchFilter={setRelationPickerSearchFilter} | ||
onSubmit={() => { | ||
closeEditableField(); | ||
}} | ||
onChange={handleChange} | ||
/> | ||
</> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord'; | ||
import { useRelationField } from '@/object-record/record-field/meta-types/hooks/useRelationField'; | ||
import { ObjectRecordForSelect } from '@/object-record/relation-picker/hooks/useMultiObjectSearch'; | ||
import { EntitiesForMultipleEntitySelect } from '@/object-record/relation-picker/types/EntitiesForMultipleEntitySelect'; | ||
import { EntityForSelect } from '@/object-record/relation-picker/types/EntityForSelect'; | ||
import { isDefined } from '~/utils/isDefined'; | ||
|
||
export const useUpdateRelationManyFieldInput = ({ | ||
entities, | ||
}: { | ||
entities: EntitiesForMultipleEntitySelect<EntityForSelect>; | ||
}) => { | ||
const { fieldDefinition, fieldValue, setFieldValue, entityId } = | ||
useRelationField<EntityForSelect[]>(); | ||
|
||
const { updateOneRecord } = useUpdateOneRecord({ | ||
objectNameSingular: | ||
fieldDefinition.metadata.relationObjectMetadataNameSingular, | ||
}); | ||
|
||
const fieldName = fieldDefinition.metadata.targetFieldMetadataName; | ||
|
||
const handleChange = ( | ||
objectRecord: ObjectRecordForSelect | null, | ||
isSelected: boolean, | ||
) => { | ||
const entityToAddOrRemove = entities.entitiesToSelect.find( | ||
(entity) => entity.id === objectRecord?.recordIdentifier.id, | ||
); | ||
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a null check for |
||
|
||
const updatedFieldValue = isSelected | ||
? [...(fieldValue ?? []), entityToAddOrRemove] | ||
: (fieldValue ?? []).filter( | ||
(value) => value.id !== objectRecord?.recordIdentifier.id, | ||
); | ||
Comment on lines
+31
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure |
||
setFieldValue( | ||
updatedFieldValue.filter((value) => | ||
isDefined(value), | ||
) as EntityForSelect[], | ||
); | ||
if (isDefined(objectRecord)) { | ||
updateOneRecord({ | ||
idToUpdate: objectRecord.record?.id, | ||
updateOneRecordInput: { | ||
[`${fieldName}Id`]: isSelected ? entityId : null, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
return { handleChange }; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was not used