Skip to content

Commit

Permalink
feat(headless): add remove notifications method
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge committed Jan 28, 2024
1 parent 0a63277 commit aa9f323
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/client/src/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export class ApiService {
return await this.httpClient.delete(`/widgets/messages/${messageId}`, {});
}

async removeMessages(messageId: string[]): Promise<any> {
return await this.httpClient.post(`/widgets/messages/bulk/delete`, {
messageId: messageId,
});
}

async removeAllMessages(feedId?: string): Promise<any> {
const url = feedId
? `/widgets/messages?feedId=${feedId}`
Expand Down
48 changes: 48 additions & 0 deletions packages/headless/src/lib/headless.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
IUpdateUserPreferencesVariables,
IUpdateUserGlobalPreferencesVariables,
UpdateResult,
EmptyObject,
} from './types';

export const NOTIFICATION_CENTER_TOKEN_KEY = 'nc_token';
Expand Down Expand Up @@ -806,6 +807,53 @@ export class HeadlessService {
});
}

public async removeNotifications({
messageIds,
listener,
onSuccess,
onError,
}: {
messageIds: string[];
listener: (
result: UpdateResult<EmptyObject, unknown, { messageIds: string[] }>
) => void;
onSuccess?: (obj: EmptyObject) => void;
onError?: (error: unknown) => void;
}) {
this.assertSessionInitialized();

const { result, unsubscribe } = this.queryService.subscribeMutation<
EmptyObject,
unknown,
{ messageIds: string[] }
>({
options: {
mutationFn: (variables) =>
this.api.removeMessages(variables.messageIds),
onSuccess: (data) => {
this.queryClient.refetchQueries(NOTIFICATIONS_QUERY_KEY, {
exact: false,
});
},
},
listener: (res) => this.callUpdateListener(res, listener),
});

result
.mutate({ messageIds })
.then((data) => {
onSuccess?.(data);

return data;
})
.catch((error) => {
onError?.(error);
})
.finally(() => {
unsubscribe();
});
}

public async updateAction({
messageId,
actionButtonType,
Expand Down
1 change: 1 addition & 0 deletions packages/headless/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ export type UpdateResult<

export type IMessageId = string | string[];
export type IFeedId = string | string[];
export type EmptyObject = Record<string, never>;

0 comments on commit aa9f323

Please sign in to comment.