Add useGroupBy hook + update calendar view to use groupBy (#15439)
## Context Took inspiration from findMany + kanban => Implemented a hook for the new groupBy API endpoints Fixed an issue when DnD to an empty day
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
import { type Reference } from '@apollo/client';
|
||||
|
||||
import { encodeCursor } from '@/apollo/utils/encodeCursor';
|
||||
import { type ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { type RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
|
||||
import { type ToReferenceFunction } from '@apollo/client/cache/core/types/common';
|
||||
|
||||
import { createCacheEdgeWithRecordRef } from '../createCacheEdgeWithRecordRef';
|
||||
|
||||
describe('createCacheEdgeWithRecordRef', () => {
|
||||
it('should create an edge with reference when toReference returns a reference', () => {
|
||||
// Given
|
||||
const record: RecordGqlNode = {
|
||||
__typename: 'Person',
|
||||
id: '123',
|
||||
};
|
||||
|
||||
const objectMetadataItem = {
|
||||
nameSingular: 'person',
|
||||
} as ObjectMetadataItem;
|
||||
|
||||
const mockReference: Reference = {
|
||||
__ref: 'Person:123',
|
||||
};
|
||||
|
||||
const toReference: ToReferenceFunction = jest.fn(() => mockReference);
|
||||
|
||||
// When
|
||||
const result = createCacheEdgeWithRecordRef({
|
||||
record,
|
||||
objectMetadataItem,
|
||||
toReference,
|
||||
});
|
||||
|
||||
// Then
|
||||
expect(result).not.toBeNull();
|
||||
expect(result).toEqual({
|
||||
__typename: 'PersonEdge',
|
||||
node: mockReference,
|
||||
cursor: encodeCursor(record),
|
||||
});
|
||||
expect(toReference).toHaveBeenCalledWith(record);
|
||||
});
|
||||
|
||||
it('should return null when toReference returns undefined', () => {
|
||||
// Given
|
||||
const record: RecordGqlNode = {
|
||||
__typename: 'Person',
|
||||
id: '123',
|
||||
};
|
||||
|
||||
const objectMetadataItem = {
|
||||
nameSingular: 'person',
|
||||
} as ObjectMetadataItem;
|
||||
|
||||
const toReference: ToReferenceFunction = jest.fn(() => undefined);
|
||||
|
||||
// When
|
||||
const result = createCacheEdgeWithRecordRef({
|
||||
record,
|
||||
objectMetadataItem,
|
||||
toReference,
|
||||
});
|
||||
|
||||
// Then
|
||||
expect(result).toBeNull();
|
||||
expect(toReference).toHaveBeenCalledWith(record);
|
||||
});
|
||||
});
|
||||
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
import { encodeCursor } from '@/apollo/utils/encodeCursor';
|
||||
import { type ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
|
||||
import { type RecordGqlRefEdge } from '@/object-record/cache/types/RecordGqlRefEdge';
|
||||
import { getEdgeTypename } from '@/object-record/cache/utils/getEdgeTypename';
|
||||
import { type RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
|
||||
import { type ToReferenceFunction } from '@apollo/client/cache/core/types/common';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
type CreateCacheEdgeWithRecordRefParams = {
|
||||
record: RecordGqlNode;
|
||||
objectMetadataItem: ObjectMetadataItem;
|
||||
toReference: ToReferenceFunction;
|
||||
};
|
||||
|
||||
export const createCacheEdgeWithRecordRef = ({
|
||||
record,
|
||||
objectMetadataItem,
|
||||
toReference,
|
||||
}: CreateCacheEdgeWithRecordRefParams): RecordGqlRefEdge | null => {
|
||||
const reference = toReference(record);
|
||||
if (!isDefined(reference)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
__typename: getEdgeTypename(objectMetadataItem.nameSingular),
|
||||
node: reference,
|
||||
cursor: encodeCursor(record),
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user