useFetchEntities

Overview

The useFetchEntities hook is used to fetch a paginated list of entities from the server based on a wide range of filters and sorting options. This hook provides fine-grained control over the query parameters, allowing for highly customizable entity retrieval.

Usage Example

import { useFetchEntities } from "@replyke/react-js";
 
function EntitiesList() {
  const fetchEntities = useFetchEntities();
 
  const handleFetchEntities = async () => {
    try {
      const entities = await fetchEntities({
        page: 1,
        limit: 10,
        followedOnly: true,
        sortBy: "new",
        keywordsFilters: { keywords: ["example", "test"] },
        timeFrame: { from: "2024-01-01", to: "2024-01-31" },
        locationFilters: { latitude: 40.7128, longitude: -74.0060, radius: 10 },
        userId: "user123",
        metadataFilters: { category: "blog" },
        titleFilters: { contains: "My Title" },
        contentFilters: { contains: "My Content" },
        mediaFilters: { hasMedia: true },
      });
 
      console.log("Fetched entities:", entities);
    } catch (error) {
      console.error("Failed to fetch entities:", error.message);
    }
  };
 
  return <button onClick={handleFetchEntities}>Fetch Entities</button>;
}

Parameters & Returns

Parameters

The hook returns a function that accepts an object with the following fields:

ParameterTypeRequiredDescription
pagenumberYesThe page number to retrieve.
limitnumberYesThe number of entities per page.
followedOnlybooleanYesWhether to fetch entities only from followed users.
sortByFeedSortByOptions | nullYesThe sorting criteria for the entities (e.g., “new”, “top”).
keywordsFiltersKeywordsFilters | nullNoFilters for specific keywords associated with the entities.
timeFrameTimeFrame | nullNoA time range for filtering entities.
locationFiltersLocationFilters | nullNoFilters based on geographic location.
userIdstring | nullNoFilter entities created by a specific user.
metadataFiltersMetadataFilters | nullNoFilters based on entity metadata.
titleFiltersTitleFilters | nullNoFilters based on entity titles.
contentFiltersContentFilters | nullNoFilters based on entity content.
mediaFiltersMediaFilters | nullNoFilters based on entity media presence or attributes.

Returns

The function resolves with an array of entities matching the specified filters and criteria:

Return ValueTypeDescription
Entity[]arrayA list of entities that match the query.