/* eslint-env browser */ // Model import { ChatBoxModel } from './chat-box.model'; // Interfaces import { IChatProps, IChatState, IContact } from './types'; import './style.scss'; // Internal Components import { AppHeaderComponent } from './functional-components/app-header.component'; import { ChatHeaderComponent } from './functional-components/chat-header.component'; import { ChatSideBarComponent } from './functional-components/chat-sidebar.component'; import { ChatMainComponent } from './functional-components/chat-main.component'; // Internals const { withWidth, isWidthUp, withStyles, Grid, Card, Typography, List, ListItem, ListItemText, Avatar, TextField, Button, SendIcon, AccountCircleIcon, GroupWorkIcon } = ChatBoxModel.uiFrameworkComponents; const { React, Component, i18next, SplitPane, withTranslation, find } = ChatBoxModel.libraries; const { Wrapper, Tabs } = ChatBoxModel.components; const { ChatStyles } = ChatBoxModel.styles; type iModalForm = 'createGroupForm' | 'settingsForm'; class Chat extends Component { constructor(props) { super(props); this.state = { opened: false, currentChat: '', createGroupForm: false, settingsForm: false }; } componentDidMount() { const { readUsersAndChat } = this.props; i18next.addResourceBundle( 'en-US', 'translation', ChatBoxModel.i18nKeys['en-US'] ); i18next.addResourceBundle( 'de-DE', 'translation', ChatBoxModel.i18nKeys['de-DE'] ); i18next.addResourceBundle( 'fr-FR', 'translation', ChatBoxModel.i18nKeys['fr-FR'] ); readUsersAndChat(); } handleDrawerToggle = () => { const { opened } = this.state; this.setState({ opened: !opened }); }; scrollToBottomChat = () => { if (typeof window !== 'undefined') { setTimeout(() => { const objDiv = typeof document !== 'undefined' && document.getElementById('chats'); if (objDiv && objDiv.scrollHeight) { objDiv.scrollTop = objDiv.scrollHeight; } }, 1); } }; modalOpen = (modalForm: iModalForm) => this.setState({ [modalForm]: true }); modalHandleClose = (modalForm: iModalForm) => this.setState({ [modalForm]: false }); sendChat = () => { const { currentChat } = this.state; const { submitChat, githubUserData, groupId } = this.props; return ( currentChat !== '' && submitChat({ variables: { ownerId: githubUserData.id, groupId, message: currentChat, date: new Date().toISOString() }, callBack: () => this.setState({ currentChat: '' }) }) ); } handleKeyDown = e => { if (e.key === 'Enter') { this.sendChat(); } }; renderSubmitChatBox = () => { const { classes, chatData: { groupMembers, member, groupType }, githubUserData } = this.props; const { currentChat } = this.state; let groupMemberData = false; if (groupType === 'group'){ groupMemberData = find(groupMembers, [ 'member.githubUid', githubUserData.id ]); } else { groupMemberData = member && member.id && true; } return groupMemberData ? (
this.setState({ currentChat: e.target.value })} onKeyDown={this.handleKeyDown} className={classes.input} />
) : (
You are not part of this group - Click here to Join
); }; renderTabs = () => { const { classes, userData, groupData, t, onSelectGroup, onSelectContact, githubUserData, submitCreateGroup } = this.props; return ( , tabName: t('chatbox-all-users'), tabContent: () => ( {userData.map((contact: IContact, index) => ( submitCreateGroup({ variables: { ownerId: githubUserData.id, memberId: contact.githubUid, groupName: `${githubUserData.name || githubUserData.login} (${ githubUserData.realId }) - ${contact.name || contact.githubId} (${ contact.id })`, groupDescription: 'private group', groupImage: 'none', groupType: 'personal', accessType: 'private', date: new Date() }, callBack: groupId => onSelectContact(groupId) })} > ))} ) }, { icon: , tabName: t('chatbox-all-groups'), tabContent: () => ( {groupData.map((groupVal: any, index) => ( onSelectGroup(groupVal.id)} > ))} ) } ]} /> ); } render() { const { classes, chatData: { chats, member, groupType, groupName, groupDescription, groupImage }, submitCreateGroup, userData, SharedComponent, width, t, githubUserData, title } = this.props; const { opened, createGroupForm, settingsForm } = this.state; const currentUsername = githubUserData.name && githubUserData.name.indexOf(' ') === -1 ? githubUserData.name : (githubUserData.name && githubUserData.name.split(' ')[0]) || githubUserData.login; const oppositeAvatarUrl = groupType === 'personal' ? member.avatarUrl : groupImage; const oppositeUserName = groupType === 'personal' ? member.name || member.githubId : groupName; const oppositeDescription = groupType === 'personal' ? 'Online' : groupDescription; const SideBarConent = this.renderTabs(); const createGroupMembers = userData.map(val => val.name ? { value: `${val.name} (${val.githubId})`, key: val.id } : { value: val.githubId, key: val.id } ); this.scrollToBottomChat(); return (
); } } const TypedChatStyles: any = ChatStyles; const TypeChatBox: any = Chat; export default withWidth()(withStyles(TypedChatStyles)(withTranslation()(TypeChatBox)));