You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			162 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			162 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			TypeScript
		
	
| 
											6 years ago
										 | import React from 'react'; | ||
|  | 
 | ||
| 
											6 years ago
										 | import { SessionModal } from '../session/SessionModal'; | ||
|  | import { SessionButton } from '../session/SessionButton'; | ||
| 
											6 years ago
										 | import { | ||
|  |   ContactType, | ||
|  |   SessionMemberListItem, | ||
|  | } from '../session/SessionMemberListItem'; | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 | interface Props { | ||
| 
											5 years ago
										 |   contactList: Array<any>; | ||
| 
											6 years ago
										 |   chatName: string; | ||
|  |   onSubmit: any; | ||
|  |   onClose: any; | ||
|  | } | ||
|  | 
 | ||
|  | interface State { | ||
| 
											5 years ago
										 |   contactList: Array<ContactType>; | ||
| 
											6 years ago
										 | } | ||
|  | 
 | ||
| 
											5 years ago
										 | export class InviteContactsDialog extends React.Component<Props, State> { | ||
| 
											6 years ago
										 |   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); | ||
|  | 
 | ||
| 
											5 years ago
										 |     let contacts = this.props.contactList; | ||
| 
											6 years ago
										 | 
 | ||
| 
											5 years ago
										 |     contacts = contacts.map(d => { | ||
| 
											6 years ago
										 |       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 = { | ||
| 
											5 years ago
										 |       contactList: contacts, | ||
| 
											6 years ago
										 |     }; | ||
|  | 
 | ||
|  |     window.addEventListener('keyup', this.onKeyUp); | ||
|  |   } | ||
|  | 
 | ||
|  |   public render() { | ||
| 
											6 years ago
										 |     const titleText = `${window.i18n('addingFriends')} ${this.props.chatName}`; | ||
| 
											6 years ago
										 |     const cancelText = window.i18n('cancel'); | ||
|  |     const okText = window.i18n('ok'); | ||
|  | 
 | ||
| 
											5 years ago
										 |     const hasContacts = this.state.contactList.length !== 0; | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 |     return ( | ||
| 
											6 years ago
										 |       <SessionModal | ||
|  |         title={titleText} | ||
|  |         onOk={() => null} | ||
|  |         onClose={this.closeDialog} | ||
|  |       > | ||
| 
											6 years ago
										 |         <div className="spacer-lg" /> | ||
|  | 
 | ||
| 
											6 years ago
										 |         <div className="friend-selection-list">{this.renderMemberList()}</div> | ||
| 
											5 years ago
										 |         {hasContacts ? null : ( | ||
| 
											6 years ago
										 |           <> | ||
|  |             <div className="spacer-lg" /> | ||
|  |             <p className="no-friends">{window.i18n('noFriendsToAdd')}</p> | ||
|  |             <div className="spacer-lg" /> | ||
|  |           </> | ||
| 
											6 years ago
										 |         )} | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 |         <div className="spacer-lg" /> | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 |         <div className="session-modal__button-group"> | ||
|  |           <SessionButton text={cancelText} onClick={this.closeDialog} /> | ||
|  |           <SessionButton | ||
|  |             text={okText} | ||
| 
											5 years ago
										 |             disabled={!hasContacts} | ||
| 
											6 years ago
										 |             onClick={this.onClickOK} | ||
| 
											6 years ago
										 |           /> | ||
| 
											6 years ago
										 |         </div> | ||
| 
											6 years ago
										 |       </SessionModal> | ||
| 
											6 years ago
										 |     ); | ||
|  |   } | ||
|  | 
 | ||
|  |   private onClickOK() { | ||
| 
											5 years ago
										 |     const selectedContacts = this.state.contactList | ||
| 
											6 years ago
										 |       .filter(d => d.checkmarked) | ||
|  |       .map(d => d.id); | ||
|  | 
 | ||
| 
											5 years ago
										 |     if (selectedContacts.length > 0) { | ||
|  |       this.props.onSubmit(selectedContacts); | ||
| 
											6 years ago
										 |     } | ||
|  | 
 | ||
|  |     this.closeDialog(); | ||
|  |   } | ||
|  | 
 | ||
| 
											6 years ago
										 |   private renderMemberList() { | ||
| 
											5 years ago
										 |     const members = this.state.contactList; | ||
| 
											6 years ago
										 | 
 | ||
| 
											6 years ago
										 |     return members.map((member: ContactType, index: number) => ( | ||
| 
											6 years ago
										 |       <SessionMemberListItem | ||
|  |         member={member} | ||
| 
											6 years ago
										 |         key={index} | ||
| 
											6 years ago
										 |         index={index} | ||
| 
											6 years ago
										 |         isSelected={false} | ||
|  |         onSelect={(selectedMember: ContactType) => { | ||
|  |           this.onMemberClicked(selectedMember); | ||
|  |         }} | ||
|  |         onUnselect={(selectedMember: ContactType) => { | ||
|  |           this.onMemberClicked(selectedMember); | ||
|  |         }} | ||
|  |       /> | ||
|  |     )); | ||
|  |   } | ||
|  | 
 | ||
| 
											6 years ago
										 |   private onKeyUp(event: any) { | ||
|  |     switch (event.key) { | ||
|  |       case 'Enter': | ||
|  |         this.onClickOK(); | ||
|  |         break; | ||
|  |       case 'Esc': | ||
|  |       case 'Escape': | ||
|  |         this.closeDialog(); | ||
|  |         break; | ||
|  |       default: | ||
|  |     } | ||
|  |   } | ||
|  | 
 | ||
| 
											6 years ago
										 |   private onMemberClicked(clickedMember: ContactType) { | ||
| 
											5 years ago
										 |     const updatedContacts = this.state.contactList.map(member => { | ||
| 
											6 years ago
										 |       if (member.id === clickedMember.id) { | ||
| 
											6 years ago
										 |         return { ...member, checkmarked: !member.checkmarked }; | ||
|  |       } else { | ||
|  |         return member; | ||
|  |       } | ||
|  |     }); | ||
|  | 
 | ||
|  |     this.setState(state => { | ||
|  |       return { | ||
|  |         ...state, | ||
| 
											5 years ago
										 |         contactList: updatedContacts, | ||
| 
											6 years ago
										 |       }; | ||
|  |     }); | ||
|  |   } | ||
| 
											6 years ago
										 | 
 | ||
|  |   private closeDialog() { | ||
|  |     window.removeEventListener('keyup', this.onKeyUp); | ||
|  |     this.props.onClose(); | ||
|  |   } | ||
| 
											6 years ago
										 | } |