Skip to content

Commit

Permalink
doMarkReadMessage prop to Channel component
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Jul 31, 2020
1 parent 185575b commit 49a058b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/components/Channel/Channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ Channel.propTypes = {
* @param {Object} message
*/
doSendMessageRequest: PropTypes.func,
/**
* Override mark channel read request (Advanced usage only)
*
* @param {Channel} channel object
* */
doMarkReadRequest: PropTypes.func,
/** Override update(edit) message request (Advanced usage only)
*
* @param {String} channelId full channel ID in format of `type:id`
Expand All @@ -121,6 +127,7 @@ const ChannelInner = ({
LoadingErrorIndicator = LoadingErrorIndicatorComponent,
Attachment = AttachmentComponent,
Message = MessageSimpleComponent,
doMarkReadRequest,
...props
// eslint-disable-next-line sonarjs/cognitive-complexity
}) => {
Expand Down Expand Up @@ -148,13 +155,15 @@ const ChannelInner = ({
return;
}
lastRead.current = new Date();

logChatPromiseExecution(channel.markRead(), 'mark read');

if (doMarkReadRequest) {
doMarkReadRequest(channel);
} else {
logChatPromiseExecution(channel.markRead(), 'mark read');
}
if (originalTitle.current) {
document.title = originalTitle.current;
}
}, [channel]);
}, [channel, doMarkReadRequest]);

// eslint-disable-next-line react-hooks/exhaustive-deps
const markReadThrottled = useCallback(
Expand Down
10 changes: 10 additions & 0 deletions src/components/Channel/__tests__/Channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ describe('Channel', () => {

await waitFor(() => expect(markReadSpy).toHaveBeenCalledWith());
});
it('should use the doMarkReadRequest prop to mark channel as read, if that is defined', async () => {
jest.spyOn(channel, 'countUnread').mockImplementationOnce(() => 1);
const doMarkReadRequest = jest.fn();

renderComponent({
doMarkReadRequest,
});

await waitFor(() => expect(doMarkReadRequest).toHaveBeenCalledTimes(1));
});

// eslint-disable-next-line sonarjs/cognitive-complexity
describe('Children that consume ChannelContext', () => {
Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ export interface ChannelProps {
channelId: string,
message: Client.Message,
): Promise<Client.MessageResponse> | void;
doMarkReadRequest?(
channel: Client.Channel,
): Promise<Client.MessageResponse> | void;
/** Override update(edit) message request (Advanced usage only) */
doUpdateMessageRequest?(
channelId: string,
Expand Down

0 comments on commit 49a058b

Please sign in to comment.