afaa962758
* Added generic relation cell * Deactivated debug * Added default warning * Put back display component * Removed unused types * wip * Renamed to view field * Use new view field structure to have chip working * Finished * Added a temp feature flag
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import { useRecoilState } from 'recoil';
|
|
|
|
import { CompanyPickerCell } from '@/companies/components/CompanyPickerCell';
|
|
import { useUpdateEntityField } from '@/people/hooks/useUpdateEntityField';
|
|
import { EntityForSelect } from '@/ui/relation-picker/types/EntityForSelect';
|
|
import { Entity } from '@/ui/relation-picker/types/EntityTypeForSelect';
|
|
import { useEditableCell } from '@/ui/table/editable-cell/hooks/useEditableCell';
|
|
import { useCurrentRowEntityId } from '@/ui/table/hooks/useCurrentEntityId';
|
|
import { tableEntityFieldFamilySelector } from '@/ui/table/states/tableEntityFieldFamilySelector';
|
|
import {
|
|
ViewFieldDefinition,
|
|
ViewFieldRelationMetadata,
|
|
} from '@/ui/table/types/ViewField';
|
|
|
|
type OwnProps = {
|
|
viewFieldDefinition: ViewFieldDefinition<ViewFieldRelationMetadata>;
|
|
};
|
|
|
|
export function GenericEditableRelationCellEditMode({
|
|
viewFieldDefinition,
|
|
}: OwnProps) {
|
|
const currentRowEntityId = useCurrentRowEntityId();
|
|
|
|
const { closeEditableCell } = useEditableCell();
|
|
|
|
const [fieldValueEntity] = useRecoilState<any | null>(
|
|
tableEntityFieldFamilySelector({
|
|
entityId: currentRowEntityId ?? '',
|
|
fieldName: viewFieldDefinition.metadata.fieldName,
|
|
}),
|
|
);
|
|
|
|
const updateEntityField = useUpdateEntityField();
|
|
|
|
function handleEntitySubmit(newFieldEntity: EntityForSelect | null) {
|
|
if (
|
|
newFieldEntity?.id !== fieldValueEntity?.id &&
|
|
currentRowEntityId &&
|
|
updateEntityField
|
|
) {
|
|
updateEntityField(
|
|
currentRowEntityId,
|
|
viewFieldDefinition.id,
|
|
newFieldEntity,
|
|
);
|
|
}
|
|
|
|
closeEditableCell();
|
|
}
|
|
|
|
function handleCancel() {
|
|
closeEditableCell();
|
|
}
|
|
|
|
switch (viewFieldDefinition.metadata.relationType) {
|
|
case Entity.Company: {
|
|
return (
|
|
<CompanyPickerCell
|
|
companyId={fieldValueEntity?.id ?? null}
|
|
onSubmit={handleEntitySubmit}
|
|
onCancel={handleCancel}
|
|
/>
|
|
);
|
|
}
|
|
default:
|
|
console.warn(
|
|
`Unknown relation type: "${viewFieldDefinition.metadata.relationType}" in GenericEditableRelationCellEditMode`,
|
|
);
|
|
return <></>;
|
|
}
|
|
}
|