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.
		
		
		
		
		
			
		
			
				
	
	
		
			83 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			TypeScript
		
	
| import React, { useState } from 'react';
 | |
| import { getConversationController } from '../../session/conversations';
 | |
| 
 | |
| import _ from 'lodash';
 | |
| import { SpacerLG } from '../basic/Text';
 | |
| import { useDispatch } from 'react-redux';
 | |
| import { changeNickNameModal } from '../../state/ducks/modalDialog';
 | |
| import { SessionButton, SessionButtonColor } from '../basic/SessionButton';
 | |
| import { SessionWrapperModal } from '../SessionWrapperModal';
 | |
| 
 | |
| type Props = {
 | |
|   conversationId: string;
 | |
| };
 | |
| 
 | |
| export const SessionNicknameDialog = (props: Props) => {
 | |
|   const { conversationId } = props;
 | |
|   const [nickname, setNickname] = useState('');
 | |
| 
 | |
|   const dispatch = useDispatch();
 | |
| 
 | |
|   /**
 | |
|    * Changes the state of nickname variable. If enter is pressed, saves the current
 | |
|    * entered nickname value as the nickname.
 | |
|    */
 | |
|   const onNicknameInput = async (event: any) => {
 | |
|     if (event.key === 'Enter') {
 | |
|       await saveNickname();
 | |
|     } else {
 | |
|       const currentNicknameEntered = event.target.value;
 | |
|       setNickname(currentNicknameEntered);
 | |
|     }
 | |
|   };
 | |
| 
 | |
|   const onClickClose = () => {
 | |
|     dispatch(changeNickNameModal(null));
 | |
|   };
 | |
| 
 | |
|   /**
 | |
|    * Saves the currently entered nickname.
 | |
|    */
 | |
|   const saveNickname = async () => {
 | |
|     if (!conversationId) {
 | |
|       throw new Error('Cant save without conversation id');
 | |
|     }
 | |
|     const conversation = getConversationController().get(conversationId);
 | |
|     await conversation.setNickname(nickname);
 | |
|     onClickClose();
 | |
|   };
 | |
| 
 | |
|   return (
 | |
|     <SessionWrapperModal
 | |
|       title={window.i18n('changeNickname')}
 | |
|       onClose={onClickClose}
 | |
|       showExitIcon={false}
 | |
|       showHeader={true}
 | |
|     >
 | |
|       <div className="session-modal__centered">
 | |
|         <span className="subtle">{window.i18n('changeNicknameMessage')}</span>
 | |
|         <SpacerLG />
 | |
|       </div>
 | |
| 
 | |
|       <input
 | |
|         autoFocus={true}
 | |
|         type="nickname"
 | |
|         id="nickname-modal-input"
 | |
|         placeholder={window.i18n('nicknamePlaceholder')}
 | |
|         onKeyUp={e => {
 | |
|           void onNicknameInput(_.cloneDeep(e));
 | |
|         }}
 | |
|       />
 | |
| 
 | |
|       <div className="session-modal__button-group">
 | |
|         <SessionButton text={window.i18n('cancel')} onClick={onClickClose} />
 | |
|         <SessionButton
 | |
|           text={window.i18n('ok')}
 | |
|           onClick={saveNickname}
 | |
|           buttonColor={SessionButtonColor.Green}
 | |
|         />
 | |
|       </div>
 | |
|     </SessionWrapperModal>
 | |
|   );
 | |
| };
 |