import { Binding, TABLE, LIST, LIBRARY_COMPONENT } from '@adalo/constants' import { IObject } from 'interfaces' import { traverse } from 'utils/treeTraversal' interface HasObjects { id?: string name?: string type?: string objects: O[] dataBindings?: Record } export const isDependencyForObject = (id: string, dependency: any) => dependency?.bindingId === id || dependency?.bindingId?.includes?.(id) || dependency?.bindingIds?.includes?.(id) const getId = (obj: T): string => obj.id /** * * There's two types of dependencies: * * The one's managed by the Screen. They load more that on page end reached. * This is the case for dependencies of Lists and Library Components, and for Table that use external collections. * * The one's managed by the PaginatedFetchComponent. At the moment, that's only a Table for an Adalo collection. */ export const isScreenManagedDependency = ( screen: HasObjects, dependency: any ) => { const tableChildren: IObject[] = [] const listChildren: IObject[] = [] const libraryComponentChildren: IObject[] = [] const paginatedListChildren: IObject[] = [] traverse(screen.objects, obj => { if (obj.type === TABLE) { tableChildren.push(obj) } else if (obj.type === LIST) { if (obj.dataBinding?.options?.manualPagination) { paginatedListChildren.push(obj) } else { listChildren.push(obj) } } else if (obj.type === LIBRARY_COMPONENT) { libraryComponentChildren.push(obj) } }) const tableChildrenIds = tableChildren.map(getId) const listChildrenIds = listChildren.map(getId) const libraryComponentChildrenIds = libraryComponentChildren.map(getId) const paginatedListChildrenIds = paginatedListChildren.map(getId) const isListDependency = listChildrenIds.some(id => isDependencyForObject(id, dependency) ) const isLibraryComponentDependency = libraryComponentChildrenIds.some(id => isDependencyForObject(id, dependency) ) const isPaginatedListDependency = paginatedListChildrenIds.some(id => isDependencyForObject(id, dependency) ) // If a dependency is a binding for a table object, it will have the table objectId in its bindingId // If it's a dependency for an external api, we still load them the old auto paginated way const isTableDependency = tableChildrenIds.some(id => isDependencyForObject(id, dependency) ) return ( !isTableDependency || dependency.isExternalCollection || isListDependency || isLibraryComponentDependency || isPaginatedListDependency ) }