Refresh comments threads and count on new comment (#276)

* Refresh comments threads and count on new comment

* Fix tests
This commit is contained in:
Charles Bochet
2023-06-12 19:32:18 +02:00
committed by GitHub
parent 3341539eb2
commit 16e1b862d9
17 changed files with 74 additions and 181 deletions

View File

@ -70,8 +70,8 @@ export function CommentThread({ commentThread }: OwnProps) {
// Also it cannot refetch queries than are not in the cache
refetchQueries: [
'GetCommentThreadsByTargets',
'GetPeopleCommentsCount',
'GetCompanyCommentsCount',
'GetCompanies',
'GetPeople',
],
onError: (error) => {
logError(

View File

@ -6,6 +6,7 @@ import { currentUserState } from '@/auth/states/currentUserState';
import { commentableEntityArrayState } from '@/comments/states/commentableEntityArrayState';
import { createdCommentThreadIdState } from '@/comments/states/createdCommentThreadIdState';
import { AutosizeTextInput } from '@/ui/components/inputs/AutosizeTextInput';
import { useOpenRightDrawer } from '@/ui/layout/right-drawer/hooks/useOpenRightDrawer';
import { logError } from '@/utils/logs/logError';
import { isDefined } from '@/utils/type-guards/isDefined';
import { isNonEmptyString } from '@/utils/type-guards/isNonEmptyString';
@ -49,6 +50,8 @@ export function CommentThreadCreateMode() {
createdCommentThreadIdState,
);
const openRightDrawer = useOpenRightDrawer();
const [createCommentMutation] = useCreateCommentMutation();
const [createCommentThreadWithComment] =
@ -96,9 +99,10 @@ export function CommentThreadCreateMode() {
}),
),
},
refetchQueries: ['GetCommentThread'],
refetchQueries: ['GetCommentThread', 'GetCompanies', 'GetPeople'],
onCompleted(data) {
setCreatedCommentThreadId(data.createOneCommentThread.id);
openRightDrawer('comments');
},
});
} else {
@ -111,11 +115,7 @@ export function CommentThreadCreateMode() {
createdAt: new Date().toISOString(),
},
// TODO: find a way to have this configuration dynamic and typed
refetchQueries: [
'GetCommentThread',
'GetPeopleCommentsCount',
'GetCompanyCommentsCount',
],
refetchQueries: ['GetCommentThread'],
onError: (error) => {
logError(
`In handleCreateCommentThread, createCommentMutation onError, error: ${error}`,

View File

@ -4,7 +4,10 @@ import { CommentThreadForDrawer } from '@/comments/types/CommentThreadForDrawer'
import { RightDrawerBody } from '@/ui/layout/right-drawer/components/RightDrawerBody';
import { RightDrawerPage } from '@/ui/layout/right-drawer/components/RightDrawerPage';
import { RightDrawerTopBar } from '@/ui/layout/right-drawer/components/RightDrawerTopBar';
import { useGetCommentThreadsByTargetsQuery } from '~/generated/graphql';
import {
SortOrder,
useGetCommentThreadsByTargetsQuery,
} from '~/generated/graphql';
import { commentableEntityArrayState } from '../../states/commentableEntityArrayState';
@ -18,6 +21,11 @@ export function RightDrawerComments() {
commentThreadTargetIds: commentableEntityArray.map(
(commentableEntity) => commentableEntity.id,
),
orderBy: [
{
createdAt: SortOrder.Desc,
},
],
},
});

View File

@ -1,43 +1,12 @@
import { gql } from '@apollo/client';
import {
useGetCompanyCommentsCountQuery,
useGetPeopleCommentsCountQuery,
} from '../../../generated/graphql';
export const GET_COMPANY_COMMENT_COUNT = gql`
query GetCompanyCommentsCount($where: CompanyWhereInput) {
companies: findManyCompany(where: $where) {
commentsCount: _commentCount
}
}
`;
export const useCompanyCommentsCountQuery = (companyId: string) => {
const { data, ...rest } = useGetCompanyCommentsCountQuery({
variables: { where: { id: { equals: companyId } } },
});
return { ...rest, data: data?.companies[0].commentsCount };
};
export const GET_PEOPLE_COMMENT_COUNT = gql`
query GetPeopleCommentsCount($where: PersonWhereInput) {
people: findManyPerson(where: $where) {
commentsCount: _commentCount
}
}
`;
export const usePeopleCommentsCountQuery = (personId: string) => {
const { data, ...rest } = useGetPeopleCommentsCountQuery({
variables: { where: { id: { equals: personId } } },
});
return { ...rest, data: data?.people[0].commentsCount };
};
export const GET_COMMENT_THREADS_BY_TARGETS = gql`
query GetCommentThreadsByTargets($commentThreadTargetIds: [String!]!) {
query GetCommentThreadsByTargets(
$commentThreadTargetIds: [String!]!
$orderBy: [CommentThreadOrderByWithRelationInput!]
) {
findManyCommentThreads(
orderBy: $orderBy
where: {
commentThreadTargets: {
some: { commentableId: { in: $commentThreadTargetIds } }