import { h, Component } from 'preact'; import PropTypes from 'prop-types'; import debounceAction from '../src/utils/debounceAction'; class ChannelDetails extends Component { static propTypes = { channel: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types }; constructor(props) { super(props); this.debouncedUserSearch = debounceAction( this.triggerUserSearch.bind(this), { config: { leading: true } }, ); this.state = { searchedUsers: [], hasLeftChannel: false, }; } triggerUserSearch = e => { const component = this; const query = e.target.value; if (query.length > 0) { const searchHash = { per_page: 20, search_fields: query }; const searchParams = new URLSearchParams(searchHash).toString(); fetch(`/search/users?${searchParams}`, { method: 'GET', headers: { Accept: 'application/json', 'X-CSRF-Token': window.csrfToken, 'Content-Type': 'application/json', }, credentials: 'same-origin', }) .then(response => response.json()) .then(response => { component.setState({ searchedUsers: response.result }); }); } else { component.setState({ searchedUsers: [] }); } }; triggerInvite = e => { const component = this; const id = e.target.dataset.content; e.target.style.display = 'none'; fetch(`/chat_channel_memberships`, { method: 'POST', headers: { Accept: 'application/json', 'X-CSRF-Token': window.csrfToken, 'Content-Type': 'application/json', }, body: JSON.stringify({ chat_channel_membership: { user_id: id, chat_channel_id: component.props.channel.id, }, }), credentials: 'same-origin', }) .then(response => response) .then(this.handleInvitationSuccess) .catch(null); }; triggerLeaveChannel = e => { e.preventDefault(); const id = e.target.dataset.content; fetch(`/chat_channel_memberships/${id}`, { method: 'DELETE', headers: { Accept: 'application/json', 'X-CSRF-Token': window.csrfToken, 'Content-Type': 'application/json', }, body: JSON.stringify({}), credentials: 'same-origin', }) .then(response => response) .then(this.handleLeaveChannelSuccess) .catch(null); }; handleLeaveChannelSuccess = () => { this.setState({ hasLeftChannel: true }); }; handleInvitationSuccess = response => { console.log(response); // eslint-disable-line no-console }; userInList = (list, user) => { const keys = Object.keys(list); for (let i = 0; i < keys.length; i += 1) { const key = keys[i]; if (user.id === list[key].id) { return true; } } return false; }; render() { const channel = this.props.channel; // eslint-disable-line const users = Object.values(channel.channel_users).map(user => (
{`${user.username} {user.name}
)); let subHeader = ''; if (users.length === 25) { subHeader =

Recently Active Members

; } let modSection = ''; let searchedUsers = []; let pendingInvites = []; if (channel.channel_mod_ids.includes(window.currentUser.id)) { // eslint-disable-next-line searchedUsers = this.state.searchedUsers.map(user => { if (!this.userInList(channel.pending_users_select_fields, user)) { let invite = ( ); if (this.userInList(channel.channel_users, user)) { invite = ( is already in {' '} {channel.channel_name} ); } return (
profile_image @ {user.user.username} {' '} - {/* prettier-ignore */} {' '} {user.title} {' '} {invite}
); } }); pendingInvites = channel.pending_users_select_fields.map(user => (
@ {user.username} {' '} - {' '} {user.name}
)); modSection = (

Invite Members

{searchedUsers}

Pending Invites:

{pendingInvites}
Contact yo@dev.to for assistance.
); // eslint-disable-next-line } else if (this.state.hasLeftChannel) { modSection = (

Danger Zone

You have left this channel {' '} 😢😢😢

This may take a few minutes to be reflected in the sidebar

); } else { modSection = (

Danger Zone

); } return (

{channel.channel_name}

{channel.description || ''}
{subHeader} {users} {modSection}
); } } export default ChannelDetails;