Fix/company picker v2 (#2535)

Fixed company picker V2
Fixed picker search hook filters / where clause
Fixed OrderBy / SortOrder type
Fixed set relation to null
This commit is contained in:
Lucas Bordeau
2023-11-16 12:34:23 +01:00
committed by GitHub
parent e026b2b6e9
commit 4acb7f41e1
9 changed files with 98 additions and 60 deletions

View File

@ -1,9 +1,10 @@
import { QueryHookOptions, QueryResult } from '@apollo/client';
import { isNonEmptyString } from '@sniptt/guards';
import { mapPaginatedObjectsToObjects } from '@/object-record/utils/mapPaginatedObjectsToObjects';
import { EntitiesForMultipleEntitySelect } from '@/ui/input/relation-picker/components/MultipleEntitySelect';
import { EntityForSelect } from '@/ui/input/relation-picker/types/EntityForSelect';
import { QueryMode, SortOrder } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
type SelectStringKeys<T> = NonNullable<
{
@ -23,7 +24,13 @@ type ExtractEntityTypeFromQueryResponse<T> = T extends {
type SearchFilter = { fieldNames: string[]; filter: string | number };
const DEFAULT_SEARCH_REQUEST_LIMIT = 10;
export type OrderBy =
| 'AscNullsLast'
| 'DescNullsLast'
| 'AscNullsFirst'
| 'DescNullsFirst';
const DEFAULT_SEARCH_REQUEST_LIMIT = 30;
// TODO: use this for all search queries, because we need selectedEntities and entitiesToSelect each time we want to search
// Filtered entities to select are
@ -31,7 +38,7 @@ export const useFilteredSearchEntityQueryV2 = ({
queryHook,
orderByField,
filters,
sortOrder = SortOrder.Asc,
sortOrder = 'AscNullsLast',
selectedIds,
mappingFunction,
limit,
@ -43,7 +50,7 @@ export const useFilteredSearchEntityQueryV2 = ({
) => QueryResult<any, any>;
orderByField: string;
filters: SearchFilter[];
sortOrder?: SortOrder;
sortOrder?: OrderBy;
selectedIds: string[];
mappingFunction: (entity: any) => EntityForSelect;
limit?: number;
@ -53,7 +60,7 @@ export const useFilteredSearchEntityQueryV2 = ({
const { loading: selectedEntitiesLoading, data: selectedEntitiesData } =
queryHook({
variables: {
where: {
filter: {
id: {
in: selectedIds,
},
@ -64,30 +71,39 @@ export const useFilteredSearchEntityQueryV2 = ({
} as any,
});
const searchFilter = filters.map(({ fieldNames, filter }) => {
return {
OR: fieldNames.map((fieldName) => ({
[fieldName]: {
startsWith: `%${filter}%`,
mode: QueryMode.Insensitive,
},
})),
};
});
const searchFilter = filters
.map(({ fieldNames, filter }) => {
if (!isNonEmptyString(filter)) {
return undefined;
}
return {
or: fieldNames.map((fieldName) => ({
[fieldName]: {
like: `%${filter}%`,
// TODO: fix mode
// mode: QueryMode.Insensitive,
},
})),
};
})
.filter(isDefined);
const {
loading: filteredSelectedEntitiesLoading,
data: filteredSelectedEntitiesData,
} = queryHook({
variables: {
where: {
AND: [
filter: {
and: [
{
AND: searchFilter,
and: searchFilter,
},
{
id: {
in: selectedIds,
not: {
id: {
in: selectedIds,
},
},
},
],
@ -101,14 +117,16 @@ export const useFilteredSearchEntityQueryV2 = ({
const { loading: entitiesToSelectLoading, data: entitiesToSelectData } =
queryHook({
variables: {
where: {
AND: [
filter: {
and: [
{
AND: searchFilter,
and: searchFilter,
},
{
id: {
notIn: [...selectedIds, ...excludeEntityIds],
not: {
id: {
in: [...selectedIds, ...excludeEntityIds],
},
},
},
],
@ -120,14 +138,6 @@ export const useFilteredSearchEntityQueryV2 = ({
} as any,
});
console.log({
selectedEntitiesData,
test: mapPaginatedObjectsToObjects({
objectNamePlural: objectNamePlural,
pagedObjects: selectedEntitiesData,
}),
});
return {
selectedEntities: mapPaginatedObjectsToObjects({
objectNamePlural: objectNamePlural,