Skip to content

Commit cb72cdd

Browse files
committed
feat: Add possibility to deselect active channel #248
1 parent 8ebc418 commit cb72cdd

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

projects/stream-chat-angular/src/lib/channel.service.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ describe('ChannelService', () => {
182182
expect(latestMessagesSpy).toHaveBeenCalledWith({});
183183
});
184184

185+
it('should deselect active channel', async () => {
186+
await init();
187+
const messagesSpy = jasmine.createSpy();
188+
service.activeChannelMessages$.subscribe(messagesSpy);
189+
const activeChannelSpy = jasmine.createSpy();
190+
service.activeChannel$.subscribe(activeChannelSpy);
191+
const messageToQuoteSpy = jasmine.createSpy();
192+
service.messageToQuote$.subscribe(messageToQuoteSpy);
193+
const latestMessagesSpy = jasmine.createSpy();
194+
service.latestMessageDateByUserByChannels$.subscribe(latestMessagesSpy);
195+
messagesSpy.calls.reset();
196+
activeChannelSpy.calls.reset();
197+
messageToQuoteSpy.calls.reset();
198+
latestMessagesSpy.calls.reset();
199+
service.deselectActiveChannel();
200+
201+
expect(messagesSpy).toHaveBeenCalledWith([]);
202+
expect(activeChannelSpy).toHaveBeenCalledWith(undefined);
203+
expect(messageToQuoteSpy).toHaveBeenCalledWith(undefined);
204+
expect(latestMessagesSpy).toHaveBeenCalledWith({});
205+
});
206+
185207
it('should tell if user #hasMoreChannels$', async () => {
186208
await init();
187209
const spy = jasmine.createSpy();

projects/stream-chat-angular/src/lib/channel.service.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,22 @@ export class ChannelService {
325325
this.messageToQuoteSubject.next(undefined);
326326
}
327327

328+
/**
329+
* Deselects the currently active (if any) channel
330+
*/
331+
deselectActiveChannel() {
332+
const activeChannel = this.activeChannelSubject.getValue();
333+
if (!activeChannel) {
334+
return;
335+
}
336+
this.activeChannelMessagesSubject.next([]);
337+
this.activeChannelSubject.next(undefined);
338+
this.activeParentMessageIdSubject.next(undefined);
339+
this.activeThreadMessagesSubject.next([]);
340+
this.latestMessageDateByUserByChannelsSubject.next({});
341+
this.selectMessageToQuote(undefined);
342+
}
343+
328344
/**
329345
* Sets the given `message` as an active parent message. If `undefined` is provided, it will deleselect the current parent message.
330346
* @param message
@@ -392,7 +408,7 @@ export class ChannelService {
392408
* @param filters
393409
* @param sort
394410
* @param options
395-
* @param shouldSetActiveChannel Decides if the firs channel in the result should be made as active channel, or no channel should be marked as active
411+
* @param shouldSetActiveChannel Decides if the first channel in the result should be made as an active channel, or no channel should be marked as active
396412
* @returns the list of channels found by the query
397413
*/
398414
async init(
@@ -422,13 +438,8 @@ export class ChannelService {
422438
* Resets the `activeChannel$`, `channels$` and `activeChannelMessages$` Observables. Useful when disconnecting a chat user, use in combination with [`disconnectUser`](./ChatClientService.mdx/#disconnectuser).
423439
*/
424440
reset() {
425-
this.activeChannelMessagesSubject.next([]);
426-
this.activeChannelSubject.next(undefined);
427-
this.activeParentMessageIdSubject.next(undefined);
428-
this.activeThreadMessagesSubject.next([]);
441+
this.deselectActiveChannel();
429442
this.channelsSubject.next(undefined);
430-
this.latestMessageDateByUserByChannelsSubject.next({});
431-
this.selectMessageToQuote(undefined);
432443
}
433444

434445
/**

0 commit comments

Comments
 (0)