From bac2a0a0bf587d5ee9565eaad0157c2bc23cae0c Mon Sep 17 00:00:00 2001 From: James Date: Wed, 23 Feb 2022 09:41:56 -0500 Subject: [PATCH] chore: cleanup --- .../field-types/Relationship/addOptions.ts | 0 .../field-types/Relationship/getResults.ts | 107 ------------------ 2 files changed, 107 deletions(-) delete mode 100644 src/admin/components/forms/field-types/Relationship/addOptions.ts delete mode 100644 src/admin/components/forms/field-types/Relationship/getResults.ts diff --git a/src/admin/components/forms/field-types/Relationship/addOptions.ts b/src/admin/components/forms/field-types/Relationship/addOptions.ts deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/src/admin/components/forms/field-types/Relationship/getResults.ts b/src/admin/components/forms/field-types/Relationship/getResults.ts deleted file mode 100644 index 0e7aeaab03..0000000000 --- a/src/admin/components/forms/field-types/Relationship/getResults.ts +++ /dev/null @@ -1,107 +0,0 @@ -import qs from 'qs'; -import { Where } from '../../../../../types'; -import { PaginatedDocs } from '../../../../../mongoose/types'; -import { SanitizedCollectionConfig } from '../../../../../collections/config/types'; -import { createRelationMap } from './createRelationMap'; -import { Action } from './types'; - -type GetResults = (args: { - collections: SanitizedCollectionConfig[] - dispatchOptions: (value: Action) => void - errorLoading: string - hasMany: boolean - lastFullyLoadedRelation?: number - lastLoadedPage?: number - relationTo: string | string[] - search: string - sort: boolean - url: string - value: unknown - setErrorLoading: (error: string) => void - setLastLoadedPage: (page: number) => void - setLastFullyLoadedRelation: (relation: number) => void -}) => Promise - -const maxResultsPerRequest = 10; - -export const getResults: GetResults = async ({ - collections, - dispatchOptions, - errorLoading, - hasMany, - lastFullyLoadedRelation: lastFullyLoadedRelationArg, - lastLoadedPage: lastLoadedPageArg, - relationTo, - search, - setErrorLoading, - setLastFullyLoadedRelation, - setLastLoadedPage, - sort, - url, - value, -}) => { - const hasMultipleRelations = Array.isArray(relationTo); - let lastLoadedPageToUse = typeof lastLoadedPageArg !== 'undefined' ? lastLoadedPageArg : 1; - const lastFullyLoadedRelationToUse = typeof lastFullyLoadedRelationArg !== 'undefined' ? lastFullyLoadedRelationArg : -1; - - const relations = Array.isArray(relationTo) ? relationTo : [relationTo]; - const relationsToFetch = lastFullyLoadedRelationToUse === -1 ? relations : relations.slice(lastFullyLoadedRelationToUse + 1); - - let resultsFetched = 0; - const relationMap = createRelationMap({ hasMany, relationTo, value }); - - if (!errorLoading) { - relationsToFetch.reduce(async (priorRelation, relation) => { - await priorRelation; - - if (resultsFetched < 10) { - const collection = collections.find((coll) => coll.slug === relation); - const fieldToSearch = collection?.admin?.useAsTitle || 'id'; - - const query: { - [key: string]: unknown - where: Where - } = { - where: {}, - limit: maxResultsPerRequest, - page: lastLoadedPageToUse, - sort: fieldToSearch, - depth: 0, - }; - - if (search) { - query.where[fieldToSearch] = { - like: search, - }; - } else { - query.where.id = { - not_in: relationMap[relation], - }; - } - - const response = await fetch(`${url}/${relation}?${qs.stringify(query)}`); - - if (response.ok) { - const data: PaginatedDocs = await response.json(); - if (data.docs.length > 0) { - resultsFetched += data.docs.length; - dispatchOptions({ type: 'ADD', data, relation, hasMultipleRelations, collection, sort }); - setLastLoadedPage(data.page); - - if (!data.nextPage) { - setLastFullyLoadedRelation(relations.indexOf(relation)); - - // If there are more relations to search, need to reset lastLoadedPage to 1 - // both locally within function and state - if (relations.indexOf(relation) + 1 < relations.length) { - lastLoadedPageToUse = 1; - } - } - } - } else { - setErrorLoading('An error has occurred.'); - } - } - }, Promise.resolve()); - } -};