Added generic relation cell (#969)
* Added generic relation cell * Deactivated debug * Added default warning * Put back display component * Removed unused types
This commit is contained in:
65
front/src/modules/people/hooks/useUpdateEntityField.ts
Normal file
65
front/src/modules/people/hooks/useUpdateEntityField.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import { useContext } from 'react';
|
||||
import { useRecoilValue } from 'recoil';
|
||||
|
||||
import { EntityForSelect } from '@/ui/relation-picker/types/EntityForSelect';
|
||||
import { entityFieldMetadataArrayState } from '@/ui/table/states/entityFieldMetadataArrayState';
|
||||
import { EntityUpdateMutationHookContext } from '@/ui/table/states/EntityUpdateMutationHookContext';
|
||||
|
||||
export function useUpdateEntityField() {
|
||||
const useUpdateEntityMutation = useContext(EntityUpdateMutationHookContext);
|
||||
|
||||
const [updateEntity] = useUpdateEntityMutation();
|
||||
|
||||
const entityFieldMetadataArray = useRecoilValue(
|
||||
entityFieldMetadataArrayState,
|
||||
);
|
||||
|
||||
return function updatePeopleField(
|
||||
currentEntityId: string,
|
||||
fieldName: string,
|
||||
newFieldValue: unknown,
|
||||
) {
|
||||
const fieldMetadata = entityFieldMetadataArray.find(
|
||||
(metadata) => metadata.fieldName === fieldName,
|
||||
);
|
||||
|
||||
if (!fieldMetadata) {
|
||||
throw new Error(`Field metadata not found for field ${fieldName}`);
|
||||
}
|
||||
|
||||
if (fieldMetadata.type === 'relation') {
|
||||
const newSelectedEntity = newFieldValue as EntityForSelect | null;
|
||||
|
||||
if (!newSelectedEntity) {
|
||||
updateEntity({
|
||||
variables: {
|
||||
where: { id: currentEntityId },
|
||||
data: {
|
||||
[fieldName]: {
|
||||
disconnect: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
} else {
|
||||
updateEntity({
|
||||
variables: {
|
||||
where: { id: currentEntityId },
|
||||
data: {
|
||||
[fieldName]: {
|
||||
connect: { id: newSelectedEntity.id },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
updateEntity({
|
||||
variables: {
|
||||
where: { id: currentEntityId },
|
||||
data: { [fieldName]: newFieldValue },
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user