Skip to content

Commit 9ecca0d

Browse files
CRNS-29: Adding support for channel.deleted event in ChannelList
1 parent 6e2972b commit 9ecca0d

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/components/ChannelList.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ const ChannelList = withChatContext(
7373
* Function that overrides default behaviour when channel gets updated
7474
*
7575
* @param {Component} thisArg Reference to ChannelList component
76-
* @param {Event} event [Event object](https://getstream.io/chat/docs/#event_object) corresponding to `notification.channel_updated` event
76+
* @param {Event} event [Event object](https://getstream.io/chat/docs/#event_object) corresponding to `channel.updated` event
7777
* */
7878
onChannelUpdated: PropTypes.func,
79+
/**
80+
* Function that overrides default behaviour when channel gets deleted. In absence of this prop, channel will be removed from the list.
81+
*
82+
* @param {Component} thisArg Reference to ChannelList component
83+
* @param {Event} event [Event object](https://getstream.io/chat/docs/#event_object) corresponding to `channel.deleted` event
84+
* */
85+
onChannelDeleted: PropTypes.func,
7986
/**
8087
* Object containing query filters
8188
* @see See [Channel query documentation](https://getstream.io/chat/docs/#query_channels) for a list of available fields for filter.
@@ -371,7 +378,7 @@ const ChannelList = withChatContext(
371378
}
372379
}
373380

374-
// // Channel data is updated
381+
// Channel data is updated
375382
if (e.type === 'channel.updated') {
376383
const channels = this.state.channels;
377384
const channelIndex = channels.findIndex(
@@ -388,6 +395,27 @@ const ChannelList = withChatContext(
388395
)
389396
this.props.onChannelUpdated(this, e);
390397
}
398+
399+
// Channel is deleted
400+
if (e.type === 'channel.deleted') {
401+
if (
402+
this.props.onChannelDeleted &&
403+
typeof this.props.onChannelDeleted === 'function'
404+
) {
405+
this.props.onChannelDeleted(this, e);
406+
} else {
407+
const channels = this.state.channels;
408+
const channelIndex = channels.findIndex(
409+
(channel) => channel.cid === e.channel.cid,
410+
);
411+
// Remove the deleted channel from the list.
412+
channels.splice(channelIndex, 1);
413+
this.setState({
414+
channels: [...channels],
415+
});
416+
}
417+
}
418+
391419
return null;
392420
};
393421

types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ interface ChannelListProps extends ChatContextValue {
229229
thisArg: React.Component<ChannelListProps>,
230230
e: Client.ChannelUpdatedEvent,
231231
): void;
232+
onChannelDeleted?(
233+
thisArg: React.Component<ChannelListProps>,
234+
e: Client.ChannelDeletedEvent,
235+
): void;
232236
// TODO: Create proper interface for followings in chat js client.
233237
/** Object containing query filters */
234238
filters: object;

0 commit comments

Comments
 (0)