@replyke/core
Version:
Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.
120 lines • 6.68 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.countUnreadNotifications = exports.markAllNotificationsAsRead = exports.markNotificationAsRead = exports.fetchAppNotifications = exports.useLazyCountUnreadNotificationsQuery = exports.useCountUnreadNotificationsQuery = exports.useMarkAllNotificationsAsReadMutation = exports.useMarkNotificationAsReadMutation = exports.useLazyFetchAppNotificationsQuery = exports.useFetchAppNotificationsQuery = exports.appNotificationsApi = void 0;
const baseApi_1 = require("./baseApi");
// Extended API with app notifications endpoints
exports.appNotificationsApi = baseApi_1.baseApi.injectEndpoints({
endpoints: (builder) => ({
// Fetch paginated app notifications
fetchAppNotifications: builder.query({
query: ({ projectId, page, limit }) => ({
url: `/${projectId}/app-notifications`,
method: "GET",
params: {
page,
limit,
},
}),
providesTags: (result, error, { projectId }) => [
{ type: "AppNotification", id: `${projectId}-LIST` },
...(result?.data?.map(({ id }) => ({
type: "AppNotification",
id,
})) ?? []),
],
}),
// Mark a notification as read
markNotificationAsRead: builder.mutation({
query: ({ projectId, notificationId }) => ({
url: `/${projectId}/app-notifications/${notificationId}/mark-as-read`,
method: "PATCH",
body: {},
responseHandler: async (response) => {
// Handle text responses (like "OK" from res.sendStatus(200))
const contentType = response.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
return response.json();
}
// For text responses, just return void since we don't need the content
return response.text().then(() => undefined);
},
}),
// Optimistically update the cache
async onQueryStarted({ projectId, notificationId }, { dispatch, queryFulfilled }) {
// Update all relevant queries in cache
const patches = [];
dispatch(exports.appNotificationsApi.util.updateQueryData("fetchAppNotifications",
// We need to find all queries for this projectId - this is a simplified approach
{ projectId, page: 1, limit: 10 }, // This should be more dynamic in practice
(draft) => {
const notification = draft.data.find((n) => n.id === notificationId);
if (notification) {
notification.isRead = true;
}
}));
try {
await queryFulfilled;
}
catch {
// Revert optimistic update on failure
patches.forEach((patch) => patch.undo());
}
},
invalidatesTags: (result, error, { projectId, notificationId }) => [
{ type: "AppNotification", id: notificationId },
{ type: "AppNotification", id: `${projectId}-LIST` },
],
}),
// Count unread notifications
countUnreadNotifications: builder.query({
query: ({ projectId }) => ({
url: `/${projectId}/app-notifications/count`,
method: "GET",
}),
providesTags: (result, error, { projectId }) => [
{ type: "AppNotification", id: `${projectId}-COUNT` },
],
}),
// Mark all notifications as read
markAllNotificationsAsRead: builder.mutation({
query: ({ projectId }) => ({
url: `/${projectId}/app-notifications/mark-all-as-read`,
method: "PATCH",
body: {},
}),
// Optimistically update the cache
async onQueryStarted({ projectId }, { dispatch, queryFulfilled }) {
// Update all relevant queries in cache
const patches = [];
// Update all notifications to be marked as read
dispatch(exports.appNotificationsApi.util.updateQueryData("fetchAppNotifications",
// We need to find all queries for this projectId - this is a simplified approach
{ projectId, page: 1, limit: 10 }, // This should be more dynamic in practice
(draft) => {
draft.data.forEach((notification) => {
notification.isRead = true;
});
}));
// Update unread count to 0
dispatch(exports.appNotificationsApi.util.updateQueryData("countUnreadNotifications", { projectId }, () => 0));
try {
await queryFulfilled;
}
catch {
// Revert optimistic update on failure
patches.forEach((patch) => patch.undo());
}
},
invalidatesTags: (result, error, { projectId }) => [
{ type: "AppNotification", id: `${projectId}-LIST` },
{ type: "AppNotification", id: `${projectId}-COUNT` },
],
}),
}),
});
// Export hooks for use in components
exports.useFetchAppNotificationsQuery = exports.appNotificationsApi.useFetchAppNotificationsQuery, exports.useLazyFetchAppNotificationsQuery = exports.appNotificationsApi.useLazyFetchAppNotificationsQuery, exports.useMarkNotificationAsReadMutation = exports.appNotificationsApi.useMarkNotificationAsReadMutation, exports.useMarkAllNotificationsAsReadMutation = exports.appNotificationsApi.useMarkAllNotificationsAsReadMutation, exports.useCountUnreadNotificationsQuery = exports.appNotificationsApi.useCountUnreadNotificationsQuery, exports.useLazyCountUnreadNotificationsQuery = exports.appNotificationsApi.useLazyCountUnreadNotificationsQuery;
// Export for manual cache management
_a = exports.appNotificationsApi.endpoints, exports.fetchAppNotifications = _a.fetchAppNotifications, exports.markNotificationAsRead = _a.markNotificationAsRead, exports.markAllNotificationsAsRead = _a.markAllNotificationsAsRead, exports.countUnreadNotifications = _a.countUnreadNotifications;
//# sourceMappingURL=appNotificationsApi.js.map