UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

133 lines 6.6 kB
import { baseApi } from "./baseApi"; // Extended API with collections endpoints export const collectionsApi = baseApi.injectEndpoints({ endpoints: (builder) => ({ // Fetch root collection fetchRootCollection: builder.query({ query: ({ projectId }) => ({ url: `/${projectId}/collections/root`, method: "GET", }), providesTags: (result, error, { projectId }) => [ { type: "Collection", id: `${projectId}-ROOT` }, ...(result ? [{ type: "Collection", id: result.id }] : []), ], }), // Fetch sub-collections for a parent collection fetchSubCollections: builder.query({ query: ({ projectId, collectionId }) => ({ url: `/${projectId}/collections/${collectionId}/sub-collections`, method: "GET", }), providesTags: (result, error, { projectId, collectionId }) => [ { type: "Collection", id: `${projectId}-${collectionId}-SUBS` }, ...(result?.map(({ id }) => ({ type: "Collection", id, })) ?? []), ], }), // Fetch paginated entities in a collection fetchCollectionEntities: builder.query({ query: ({ projectId, collectionId, page, limit, sortBy, sortDir, include }) => ({ url: `/${projectId}/collections/${collectionId}/entities`, method: "GET", params: { page, limit, sortBy, sortDir, include: Array.isArray(include) ? include.join(',') : include, }, }), providesTags: (result, error, { collectionId }) => [ { type: "CollectionEntities", id: collectionId }, ], }), // Create a new sub-collection createCollection: builder.mutation({ query: ({ projectId, parentCollectionId, collectionName }) => ({ url: `/${projectId}/collections/${parentCollectionId}/sub-collections`, method: "POST", body: { collectionName }, }), invalidatesTags: (result, error, { projectId, parentCollectionId }) => [ { type: "Collection", id: `${projectId}-${parentCollectionId}-SUBS` }, ...(result ? [{ type: "Collection", id: result.id }] : []), ], }), // Update collection properties (flat structure - matches space update pattern) updateCollection: builder.mutation({ query: ({ projectId, collectionId, update }) => ({ url: `/${projectId}/collections/${collectionId}`, method: "PATCH", body: update, }), // Optimistically update the cache async onQueryStarted({ projectId, collectionId, update }, { dispatch, queryFulfilled }) { // Update in all relevant queries const patches = []; // Update root collection if it's the one being updated patches.push(dispatch(collectionsApi.util.updateQueryData("fetchRootCollection", { projectId }, (draft) => { if (draft && draft.id === collectionId) { if (update.name !== undefined) { draft.name = update.name; } } }))); // Update in sub-collections queries (we need to find which parent contains this collection) // This is a simplified approach - in a real app you might want more sophisticated cache management try { await queryFulfilled; } catch { // Revert optimistic update on failure patches.forEach((patch) => patch.undo()); } }, invalidatesTags: (result, error, { collectionId }) => [ { type: "Collection", id: collectionId }, ], }), // Delete a collection deleteCollection: builder.mutation({ query: ({ projectId, collectionId }) => ({ url: `/${projectId}/collections/${collectionId}`, method: "DELETE", }), invalidatesTags: (result, error, { projectId, collectionId }) => [ { type: "Collection", id: collectionId }, // Invalidate all sub-collections queries since we don't know which parent this belonged to { type: "Collection", id: `${projectId}-SUBS-ALL` }, ], }), // Add entity to collection (POST - create relationship) addToCollection: builder.mutation({ query: ({ projectId, collectionId, entityId }) => ({ url: `/${projectId}/collections/${collectionId}/entities`, method: "POST", body: { entityId }, }), invalidatesTags: (result, error, { collectionId }) => [ { type: "Collection", id: collectionId }, { type: "CollectionEntities", id: collectionId }, ], }), // Remove entity from collection (DELETE - remove relationship) removeFromCollection: builder.mutation({ query: ({ projectId, collectionId, entityId }) => ({ url: `/${projectId}/collections/${collectionId}/entities/${entityId}`, method: "DELETE", }), invalidatesTags: (result, error, { collectionId }) => [ { type: "Collection", id: collectionId }, { type: "CollectionEntities", id: collectionId }, ], }), }), }); // Export hooks for use in components export const { useFetchRootCollectionQuery, useLazyFetchRootCollectionQuery, useFetchSubCollectionsQuery, useLazyFetchSubCollectionsQuery, useFetchCollectionEntitiesQuery, useLazyFetchCollectionEntitiesQuery, useCreateCollectionMutation, useUpdateCollectionMutation, useDeleteCollectionMutation, useAddToCollectionMutation, useRemoveFromCollectionMutation, } = collectionsApi; // Export for manual cache management export const { fetchRootCollection, fetchSubCollections, fetchCollectionEntities, createCollection, updateCollection, deleteCollection, addToCollection, removeFromCollection, } = collectionsApi.endpoints; //# sourceMappingURL=collectionsApi.js.map