session-desktop/ts/components/conversation/InviteFriendsDialog.tsx

160 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-11-22 06:16:43 +01:00
import React from 'react';
2020-01-20 05:40:24 +01:00
import { SessionModal } from '../session/SessionModal';
import { SessionButton } from '../session/SessionButton';
import {
ContactType,
SessionMemberListItem,
} from '../session/SessionMemberListItem';
2020-01-20 05:40:24 +01:00
2019-11-22 06:16:43 +01:00
interface Props {
friendList: Array<any>;
chatName: string;
onSubmit: any;
onClose: any;
}
interface State {
friendList: Array<ContactType>;
2019-11-22 06:16:43 +01:00
}
export class InviteFriendsDialog extends React.Component<Props, State> {
constructor(props: any) {
super(props);
this.onMemberClicked = this.onMemberClicked.bind(this);
this.closeDialog = this.closeDialog.bind(this);
this.onClickOK = this.onClickOK.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
let friends = this.props.friendList;
2020-02-04 08:07:31 +01:00
2019-11-22 06:16:43 +01:00
friends = friends.map(d => {
const lokiProfile = d.getLokiProfile();
const name = lokiProfile ? lokiProfile.displayName : 'Anonymous';
// TODO: should take existing members into account
const existingMember = false;
return {
id: d.id,
authorPhoneNumber: d.id,
authorProfileName: name,
selected: false,
authorName: name,
authorColor: d.getColor(),
checkmarked: false,
existingMember,
};
});
this.state = {
friendList: friends,
};
window.addEventListener('keyup', this.onKeyUp);
}
public render() {
2019-11-25 02:02:03 +01:00
const titleText = `${window.i18n('addingFriends')} ${this.props.chatName}`;
2019-11-22 06:16:43 +01:00
const cancelText = window.i18n('cancel');
const okText = window.i18n('ok');
const hasFriends = this.state.friendList.length !== 0;
2019-11-22 06:16:43 +01:00
return (
2020-01-20 05:40:24 +01:00
<SessionModal
title={titleText}
onOk={() => null}
onClose={this.closeDialog}
>
<div className="spacer-lg" />
<div className="friend-selection-list">{this.renderMemberList()}</div>
{hasFriends ? null : (
2020-01-20 05:40:24 +01:00
<>
<div className="spacer-lg" />
<p className="no-friends">{window.i18n('noFriendsToAdd')}</p>
<div className="spacer-lg" />
</>
)}
2020-01-20 05:40:24 +01:00
<div className="spacer-lg" />
2020-02-04 08:07:31 +01:00
2020-01-20 05:40:24 +01:00
<div className="session-modal__button-group">
<SessionButton text={cancelText} onClick={this.closeDialog} />
<SessionButton
text={okText}
disabled={!hasFriends}
onClick={this.onClickOK}
2020-01-20 05:40:24 +01:00
/>
2019-11-22 06:16:43 +01:00
</div>
2020-01-20 05:40:24 +01:00
</SessionModal>
2019-11-22 06:16:43 +01:00
);
}
private onClickOK() {
const selectedFriends = this.state.friendList
.filter(d => d.checkmarked)
.map(d => d.id);
if (selectedFriends.length > 0) {
this.props.onSubmit(selectedFriends);
}
this.closeDialog();
}
private renderMemberList() {
const members = this.state.friendList;
return members.map((member: ContactType) => (
<SessionMemberListItem
member={member}
isSelected={false}
onSelect={(selectedMember: ContactType) => {
this.onMemberClicked(selectedMember);
}}
onUnselect={(selectedMember: ContactType) => {
this.onMemberClicked(selectedMember);
}}
/>
));
}
2019-11-22 06:16:43 +01:00
private onKeyUp(event: any) {
switch (event.key) {
case 'Enter':
this.onClickOK();
break;
case 'Esc':
case 'Escape':
this.closeDialog();
break;
default:
}
}
private onMemberClicked(clickedMember: ContactType) {
2019-11-22 06:16:43 +01:00
const updatedFriends = this.state.friendList.map(member => {
if (member.id === clickedMember.id) {
2019-11-22 06:16:43 +01:00
return { ...member, checkmarked: !member.checkmarked };
} else {
return member;
}
});
this.setState(state => {
return {
...state,
friendList: updatedFriends,
};
});
}
private closeDialog() {
window.removeEventListener('keyup', this.onKeyUp);
this.props.onClose();
}
2019-11-22 06:16:43 +01:00
}