import { h, Component } from 'preact'; import PropTypes from 'prop-types'; class ChannelDetails extends Component { static propTypes = { channel: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types }; constructor(props) { super(props); this.state = { searchedUsers: [], // invitations: [], hasLeftChannel: false, }; const algoliaId = document.querySelector("meta[name='algolia-public-id']") .content; const algoliaKey = document.querySelector("meta[name='algolia-public-key']") .content; const env = document.querySelector("meta[name='environment']").content; const client = algoliasearch(algoliaId, algoliaKey); // eslint-disable-line no-undef this.index = client.initIndex(`searchables_${env}`); } triggerUserSearch = e => { const component = this; const query = e.target.value; const filters = { hitsPerPage: 20, attributesToRetrieve: ['id', 'title', 'path', 'user'], attributesToHighlight: [], filters: 'class_name:User', }; if (query.length > 0) { this.index.search(query, filters).then(content => { component.setState({ searchedUsers: content.hits }); }); } 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 (const key of keys) { 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 => (
)); let subHeader = ''; if (users.length === 25) { subHeader =