@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
137 lines • 8.32 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeFromCollection = exports.addToCollection = exports.deleteCollection = exports.updateCollection = exports.createCollection = exports.fetchCollectionEntities = exports.fetchSubCollections = exports.fetchRootCollection = exports.useRemoveFromCollectionMutation = exports.useAddToCollectionMutation = exports.useDeleteCollectionMutation = exports.useUpdateCollectionMutation = exports.useCreateCollectionMutation = exports.useLazyFetchCollectionEntitiesQuery = exports.useFetchCollectionEntitiesQuery = exports.useLazyFetchSubCollectionsQuery = exports.useFetchSubCollectionsQuery = exports.useLazyFetchRootCollectionQuery = exports.useFetchRootCollectionQuery = exports.collectionsApi = void 0;
const baseApi_1 = require("./baseApi");
// Extended API with collections endpoints
exports.collectionsApi = baseApi_1.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(exports.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
exports.useFetchRootCollectionQuery = exports.collectionsApi.useFetchRootCollectionQuery, exports.useLazyFetchRootCollectionQuery = exports.collectionsApi.useLazyFetchRootCollectionQuery, exports.useFetchSubCollectionsQuery = exports.collectionsApi.useFetchSubCollectionsQuery, exports.useLazyFetchSubCollectionsQuery = exports.collectionsApi.useLazyFetchSubCollectionsQuery, exports.useFetchCollectionEntitiesQuery = exports.collectionsApi.useFetchCollectionEntitiesQuery, exports.useLazyFetchCollectionEntitiesQuery = exports.collectionsApi.useLazyFetchCollectionEntitiesQuery, exports.useCreateCollectionMutation = exports.collectionsApi.useCreateCollectionMutation, exports.useUpdateCollectionMutation = exports.collectionsApi.useUpdateCollectionMutation, exports.useDeleteCollectionMutation = exports.collectionsApi.useDeleteCollectionMutation, exports.useAddToCollectionMutation = exports.collectionsApi.useAddToCollectionMutation, exports.useRemoveFromCollectionMutation = exports.collectionsApi.useRemoveFromCollectionMutation;
// Export for manual cache management
_a = exports.collectionsApi.endpoints, exports.fetchRootCollection = _a.fetchRootCollection, exports.fetchSubCollections = _a.fetchSubCollections, exports.fetchCollectionEntities = _a.fetchCollectionEntities, exports.createCollection = _a.createCollection, exports.updateCollection = _a.updateCollection, exports.deleteCollection = _a.deleteCollection, exports.addToCollection = _a.addToCollection, exports.removeFromCollection = _a.removeFromCollection;
//# sourceMappingURL=collectionsApi.js.map