Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

☑️ Refacto "Select All/Unselect all" on indexes #5320

Merged
merged 49 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0655efd
☑️ Refacto "Select All/Unselect all" on indexes
gitstart-twenty May 7, 2024
e056193
Merge commit '005045c596d8b080539ec180c83aa75d301bf770' of https://gi…
gitstart-twenty May 8, 2024
40faff3
Fix jest
gitstart-twenty May 8, 2024
1cf0db6
Refactor according to self review
gitstart-twenty May 8, 2024
d6f0675
Merge commit 'fdf10f17e2fa91da60336140fb859ae56e8acfb9' of https://gi…
gitstart-twenty May 16, 2024
29dd4f6
Refactor according to review
gitstart-twenty May 16, 2024
7de431d
Merge main
gitstart-twenty May 23, 2024
c0ca3bd
Merge main
gitstart-twenty May 23, 2024
5a52f64
Merge main
gitstart-twenty May 23, 2024
bd8c53e
Merge main
gitstart-twenty Jun 11, 2024
fb13ca2
Merge branch 'main' into TWNTY-4397
FelixMalfait Jun 14, 2024
7acce56
Merge branch 'main' into TWNTY-4397
lucasbordeau Jun 17, 2024
2477b14
Fixed typing problem
lucasbordeau Jun 17, 2024
856fa81
Fix
lucasbordeau Jun 17, 2024
cb51287
Merge branch 'main' into TWNTY-4397
lucasbordeau Jun 19, 2024
9d7d570
Change download predicate
gitstart-twenty Jun 21, 2024
de563bf
Merge main
gitstart-twenty Jun 21, 2024
52a64c7
Merge commit 'd126b148a19bc32a7cb52e7ed04d2470bf8be6b5' of https://gi…
gitstart-twenty Jun 21, 2024
a43bf6b
Fix Select All checkbox
gitstart-twenty Jun 21, 2024
dfe033d
Merge branch 'main' into TWNTY-4397
lucasbordeau Jun 24, 2024
fe9c465
Fixed bug
lucasbordeau Jun 24, 2024
72a4e70
Refactor according to review
gitstart-twenty Jun 28, 2024
922942d
Revert temporary changes
gitstart-twenty Jun 28, 2024
db93173
Fix linter issues
gitstart-twenty Jun 28, 2024
72aa58c
Fix records count
gitstart-twenty Jun 28, 2024
cf1ac0e
Fix linter
gitstart-twenty Jun 28, 2024
079a412
Merge branch 'main' into TWNTY-4397
lucasbordeau Jul 1, 2024
d91482d
Merge commit '5df0ea6466be885ce85dcb2bb849b8493698ce52' of https://gi…
gitstart-twenty Jul 4, 2024
d5011b3
Fix click outside table
gitstart-twenty Jul 4, 2024
e1343a3
Merge branch 'main' into TWNTY-4397
lucasbordeau Jul 5, 2024
7c7edaa
WIP
lucasbordeau Jul 5, 2024
0c76416
Fix
lucasbordeau Jul 5, 2024
4cefd9c
Fix
lucasbordeau Jul 5, 2024
1bb1d27
removed refetch queries
lucasbordeau Jul 5, 2024
968046d
WIP
lucasbordeau Jul 9, 2024
d5f863a
WIP
lucasbordeau Jul 9, 2024
6e6c7f6
WIP
lucasbordeau Jul 9, 2024
ec1b8be
Merge branch 'main' into TWNTY-4397
lucasbordeau Jul 9, 2024
b676c8b
WIP
lucasbordeau Jul 9, 2024
1ae7a4f
Fixed bug
lucasbordeau Jul 9, 2024
5cf6444
Cleaned
lucasbordeau Jul 9, 2024
c04d308
Fixed favorites
lucasbordeau Jul 9, 2024
e4bd470
Naming fix
lucasbordeau Jul 9, 2024
9714cd6
Fixed typo
lucasbordeau Jul 9, 2024
1156e08
Fxed naming
lucasbordeau Jul 9, 2024
8ffbbdf
Fix
lucasbordeau Jul 9, 2024
0cc7e64
Fixed test
lucasbordeau Jul 9, 2024
2521676
Fixed coverage with test on new useFetchAllRecordIds and other utils
lucasbordeau Jul 10, 2024
80621c6
Fix lint
lucasbordeau Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_MUTATION_BATCH_SIZE = 30;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DELETE_MAX_COUNT = 10000;
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('useDeleteManyRecords', () => {
await act(async () => {
const res = await result.current.deleteManyRecords(people);
expect(res).toBeDefined();
expect(res).toHaveProperty('id');
expect(res[0]).toHaveProperty('id');
});

expect(mocks[0].result).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactNode } from 'react';
import { MockedProvider } from '@apollo/client/testing';
import { renderHook } from '@testing-library/react';
import { ReactNode } from 'react';
import { RecoilRoot, useSetRecoilState } from 'recoil';

import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache';
import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize';
import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation';
import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField';
import { isDefined } from '~/utils/isDefined';
import { sleep } from '~/utils/sleep';
import { capitalize } from '~/utils/string/capitalize';

type useDeleteOneRecordProps = {
Expand All @@ -16,6 +18,7 @@ type useDeleteOneRecordProps = {

type DeleteManyRecordsOptions = {
skipOptimisticEffect?: boolean;
delayInMsBetweenRequests?: number;
};

export const useDeleteManyRecords = ({
Expand Down Expand Up @@ -45,40 +48,62 @@ export const useDeleteManyRecords = ({
idsToDelete: string[],
options?: DeleteManyRecordsOptions,
) => {
const deletedRecords = await apolloClient.mutate({
mutation: deleteManyRecordsMutation,
variables: {
filter: { id: { in: idsToDelete } },
},
optimisticResponse: options?.skipOptimisticEffect
? undefined
: {
[mutationResponseField]: idsToDelete.map((idToDelete) => ({
__typename: capitalize(objectNameSingular),
id: idToDelete,
})),
},
update: options?.skipOptimisticEffect
? undefined
: (cache, { data }) => {
const records = data?.[mutationResponseField];

if (!records?.length) return;

const cachedRecords = records
.map((record) => getRecordFromCache(record.id, cache))
.filter(isDefined);

triggerDeleteRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: cachedRecords,
objectMetadataItems,
});
},
});

return deletedRecords.data?.[mutationResponseField] ?? null;
const numberOfBatches = Math.ceil(
idsToDelete.length / DEFAULT_MUTATION_BATCH_SIZE,
);

const deletedRecords = [];

for (let batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
const batchIds = idsToDelete.slice(
batchIndex * DEFAULT_MUTATION_BATCH_SIZE,
(batchIndex + 1) * DEFAULT_MUTATION_BATCH_SIZE,
);

const deletedRecordsResponse = await apolloClient.mutate({
mutation: deleteManyRecordsMutation,
variables: {
filter: { id: { in: batchIds } },
},
optimisticResponse: options?.skipOptimisticEffect
? undefined
: {
[mutationResponseField]: batchIds.map((idToDelete) => ({
__typename: capitalize(objectNameSingular),
id: idToDelete,
})),
},
update: options?.skipOptimisticEffect
? undefined
: (cache, { data }) => {
const records = data?.[mutationResponseField];

if (!records?.length) return;

const cachedRecords = records
.map((record) => getRecordFromCache(record.id, cache))
.filter(isDefined);

triggerDeleteRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToDelete: cachedRecords,
objectMetadataItems,
});
},
});

const deletedRecordsForThisBatch =
deletedRecordsResponse.data?.[mutationResponseField] ?? [];

deletedRecords.push(...deletedRecordsForThisBatch);

if (isDefined(options?.delayInMsBetweenRequests)) {
await sleep(options.delayInMsBetweenRequests);
}
}

return deletedRecords;
};

return { deleteManyRecords };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { UseFindManyRecordsParams } from '@/object-record/hooks/useFetchMoreRecordsWithPagination';
import { useLazyFindManyRecords } from '@/object-record/hooks/useLazyFindManyRecords';
import { useCallback } from 'react';
import { isDefined } from '~/utils/isDefined';

type UseLazyFetchAllRecordIdsParams<T> = Omit<
UseFindManyRecordsParams<T>,
'skip'
>;

export const useFetchAllRecordIds = <T>({
objectNameSingular,
filter,
orderBy,
}: UseLazyFetchAllRecordIdsParams<T>) => {
const { fetchMore, findManyRecords } = useLazyFindManyRecords({
objectNameSingular,
filter,
orderBy,
recordGqlFields: { id: true },
});

const { objectMetadataItem } = useObjectMetadataItem({
objectNameSingular,
});

const fetchAllRecordIds = useCallback(async () => {
if (!isDefined(findManyRecords)) {
return [];
}

const findManyRecordsDataResult = await findManyRecords();

const firstQueryResult =
findManyRecordsDataResult?.data?.[objectMetadataItem.namePlural];

const totalCount = firstQueryResult?.totalCount ?? 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 1 as default and not 0?


const recordsCount = firstQueryResult?.edges.length ?? 0;

const recordIdSet = new Set(
firstQueryResult?.edges?.map((edge) => edge.node.id) ?? [],
);

const remainingCount = totalCount - recordsCount;

const remainingPages = Math.ceil(remainingCount / 30);

let lastCursor = firstQueryResult?.pageInfo.endCursor ?? '';

for (let i = 0; i < remainingPages; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pageIndex rather than i

const rawResult = await fetchMore?.({
variables: {
lastCursor: lastCursor,
},
});

const fetchMoreResult = rawResult?.data?.[objectMetadataItem.namePlural];

for (const edge of fetchMoreResult.edges) {
recordIdSet.add(edge.node.id);
}

lastCursor = fetchMoreResult.pageInfo.endCursor ?? '';
}

const recordIds = Array.from(recordIdSet);

return recordIds;
}, [fetchMore, findManyRecords, objectMetadataItem.namePlural]);

return {
fetchAllRecordIds,
};
};
Loading
Loading