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';
|
2020-07-08 07:46:37 +02:00
|
|
|
import { SessionButton, SessionButtonColor } from '../session/SessionButton';
|
2021-04-22 10:03:58 +02:00
|
|
|
import { ContactType, SessionMemberListItem } from '../session/SessionMemberListItem';
|
2020-12-03 02:09:39 +01:00
|
|
|
import { DefaultTheme, withTheme } from 'styled-components';
|
2020-01-20 05:40:24 +01:00
|
|
|
|
2019-11-22 06:16:43 +01:00
|
|
|
interface Props {
|
2020-06-16 02:00:37 +02:00
|
|
|
contactList: Array<any>;
|
2019-11-22 06:16:43 +01:00
|
|
|
chatName: string;
|
|
|
|
onSubmit: any;
|
|
|
|
onClose: any;
|
2020-12-03 02:09:39 +01:00
|
|
|
theme: DefaultTheme;
|
2019-11-22 06:16:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2020-06-16 02:00:37 +02:00
|
|
|
contactList: Array<ContactType>;
|
2019-11-22 06:16:43 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 02:09:39 +01:00
|
|
|
class InviteContactsDialogInner extends React.Component<Props, State> {
|
2019-11-22 06:16:43 +01:00
|
|
|
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);
|
|
|
|
|
2020-06-16 02:00:37 +02:00
|
|
|
let contacts = this.props.contactList;
|
2020-02-04 08:07:31 +01:00
|
|
|
|
2020-06-16 02:00:37 +02:00
|
|
|
contacts = contacts.map(d => {
|
2019-11-22 06:16:43 +01:00
|
|
|
const lokiProfile = d.getLokiProfile();
|
2021-04-22 10:03:58 +02:00
|
|
|
const name = lokiProfile ? lokiProfile.displayName : window.i18n('anonymous');
|
2019-11-22 06:16:43 +01:00
|
|
|
|
|
|
|
// TODO: should take existing members into account
|
|
|
|
const existingMember = false;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: d.id,
|
|
|
|
authorPhoneNumber: d.id,
|
|
|
|
authorProfileName: name,
|
2021-03-19 00:43:47 +01:00
|
|
|
authorAvatarPath: d?.getAvatarPath(),
|
2019-11-22 06:16:43 +01:00
|
|
|
selected: false,
|
|
|
|
authorName: name,
|
|
|
|
checkmarked: false,
|
|
|
|
existingMember,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
this.state = {
|
2020-06-16 02:00:37 +02:00
|
|
|
contactList: contacts,
|
2019-11-22 06:16:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('keyup', this.onKeyUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2020-07-29 07:33:02 +02:00
|
|
|
const titleText = `${window.i18n('addingContacts')} ${this.props.chatName}`;
|
2019-11-22 06:16:43 +01:00
|
|
|
const cancelText = window.i18n('cancel');
|
|
|
|
const okText = window.i18n('ok');
|
|
|
|
|
2020-06-16 02:00:37 +02:00
|
|
|
const hasContacts = this.state.contactList.length !== 0;
|
2019-11-25 07:29:07 +01:00
|
|
|
|
2019-11-22 06:16:43 +01:00
|
|
|
return (
|
2021-04-22 10:03:58 +02:00
|
|
|
<SessionModal title={titleText} onClose={this.closeDialog} theme={this.props.theme}>
|
2020-02-05 01:12:50 +01:00
|
|
|
<div className="spacer-lg" />
|
|
|
|
|
2020-08-31 06:52:26 +02:00
|
|
|
<div className="contact-selection-list">{this.renderMemberList()}</div>
|
2020-06-16 02:00:37 +02:00
|
|
|
{hasContacts ? null : (
|
2020-01-20 05:40:24 +01:00
|
|
|
<>
|
|
|
|
<div className="spacer-lg" />
|
2020-08-31 06:52:26 +02:00
|
|
|
<p className="no-contacts">{window.i18n('noContactsToAdd')}</p>
|
2020-01-20 05:40:24 +01:00
|
|
|
<div className="spacer-lg" />
|
|
|
|
</>
|
2019-11-25 07:29:07 +01:00
|
|
|
)}
|
2020-01-20 05:40:24 +01:00
|
|
|
|
2020-02-05 01:12:50 +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}
|
2020-06-16 02:00:37 +02:00
|
|
|
disabled={!hasContacts}
|
2019-11-25 07:29:07 +01:00
|
|
|
onClick={this.onClickOK}
|
2020-07-08 07:46:37 +02:00
|
|
|
buttonColor={SessionButtonColor.Green}
|
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() {
|
2021-04-22 10:03:58 +02:00
|
|
|
const selectedContacts = this.state.contactList.filter(d => d.checkmarked).map(d => d.id);
|
2019-11-22 06:16:43 +01:00
|
|
|
|
2020-06-16 02:00:37 +02:00
|
|
|
if (selectedContacts.length > 0) {
|
|
|
|
this.props.onSubmit(selectedContacts);
|
2019-11-22 06:16:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.closeDialog();
|
|
|
|
}
|
|
|
|
|
2020-02-12 03:53:18 +01:00
|
|
|
private renderMemberList() {
|
2020-06-16 02:00:37 +02:00
|
|
|
const members = this.state.contactList;
|
2021-04-22 10:03:58 +02:00
|
|
|
const selectedContacts = this.state.contactList.filter(d => d.checkmarked).map(d => d.id);
|
2020-02-12 03:53:18 +01:00
|
|
|
|
2020-05-19 04:01:42 +02:00
|
|
|
return members.map((member: ContactType, index: number) => (
|
2020-02-12 03:53:18 +01:00
|
|
|
<SessionMemberListItem
|
|
|
|
member={member}
|
2020-05-21 07:10:54 +02:00
|
|
|
key={index}
|
2020-05-19 04:01:42 +02:00
|
|
|
index={index}
|
2021-01-13 02:36:31 +01:00
|
|
|
isSelected={selectedContacts.some(m => m === member.id)}
|
2020-02-12 03:53:18 +01:00
|
|
|
onSelect={(selectedMember: ContactType) => {
|
|
|
|
this.onMemberClicked(selectedMember);
|
|
|
|
}}
|
|
|
|
onUnselect={(selectedMember: ContactType) => {
|
|
|
|
this.onMemberClicked(selectedMember);
|
|
|
|
}}
|
2020-12-03 03:47:32 +01:00
|
|
|
theme={this.props.theme}
|
2020-02-12 03:53:18 +01:00
|
|
|
/>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 03:53:18 +01:00
|
|
|
private onMemberClicked(clickedMember: ContactType) {
|
2020-06-16 02:00:37 +02:00
|
|
|
const updatedContacts = this.state.contactList.map(member => {
|
2020-02-12 03:53:18 +01:00
|
|
|
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,
|
2020-06-16 02:00:37 +02:00
|
|
|
contactList: updatedContacts,
|
2019-11-22 06:16:43 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2020-02-12 03:53:18 +01:00
|
|
|
|
|
|
|
private closeDialog() {
|
|
|
|
window.removeEventListener('keyup', this.onKeyUp);
|
|
|
|
this.props.onClose();
|
|
|
|
}
|
2019-11-22 06:16:43 +01:00
|
|
|
}
|
2020-12-03 02:09:39 +01:00
|
|
|
|
2020-12-03 03:47:32 +01:00
|
|
|
export const InviteContactsDialog = InviteContactsDialogInner;
|