Files
twenty/front/src/modules/comments/hooks/useHandleCheckableCommentThreadTargetChange.ts
Lucas Bordeau d13ceb98fa 306 implement multi relation picker for person and try to factorize relation picker (#319)
* Removed useless folder

* First working version

* Refactored MultipleEntitySelect and splitted into 2 components

* Added TODO

* Removed useless Query

* Fixed refetch

* Fixed naming

* Fix tests

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2023-06-17 10:13:30 +02:00

56 lines
1.6 KiB
TypeScript

import { v4 } from 'uuid';
import {
useAddCommentThreadTargetOnCommentThreadMutation,
useRemoveCommentThreadTargetOnCommentThreadMutation,
} from '~/generated/graphql';
import { EntityForSelect } from '../components/MultipleEntitySelect';
import { CommentThreadForDrawer } from '../types/CommentThreadForDrawer';
export function useHandleCheckableCommentThreadTargetChange({
commentThread,
}: {
commentThread: CommentThreadForDrawer;
}) {
const [addCommentThreadTargetOnCommentThread] =
useAddCommentThreadTargetOnCommentThreadMutation({
refetchQueries: ['GetCompanies', 'GetPeople'],
});
const [removeCommentThreadTargetOnCommentThread] =
useRemoveCommentThreadTargetOnCommentThreadMutation({
refetchQueries: ['GetCompanies', 'GetPeople'],
});
return function handleCheckItemChange(
newCheckedValue: boolean,
entity: EntityForSelect,
) {
if (newCheckedValue) {
addCommentThreadTargetOnCommentThread({
variables: {
commentableEntityId: entity.id,
commentableEntityType: entity.entityType,
commentThreadId: commentThread.id,
commentThreadTargetCreationDate: new Date().toISOString(),
commentThreadTargetId: v4(),
},
});
} else {
const foundCorrespondingTarget = commentThread.commentThreadTargets?.find(
(target) => target.commentableId === entity.id,
);
if (foundCorrespondingTarget) {
removeCommentThreadTargetOnCommentThread({
variables: {
commentThreadId: commentThread.id,
commentThreadTargetId: foundCorrespondingTarget.id,
},
});
}
}
};
}