1 import { Component } from 'react'; 2 import { WithTranslation } from 'react-i18next'; 3 4 import { IStore } from '../../app/types'; 5 import { extractYoutubeIdOrURL } from '../functions'; 6 7 /** 8 * The type of the React {@code Component} props of 9 * {@link AbstractSharedVideoDialog}. 10 */ 11 export interface IProps extends WithTranslation { 12 13 /** 14 * Invoked to update the shared video link. 15 */ 16 dispatch: IStore['dispatch']; 17 18 /** 19 * Function to be invoked after typing a valid video. 20 */ 21 onPostSubmit: Function; 22 } 23 24 /** 25 * Implements an abstract class for {@code SharedVideoDialog}. 26 */ 27 export default class AbstractSharedVideoDialog<S> extends Component < IProps, S > { 28 29 /** 30 * Instantiates a new component. 31 * 32 * @inheritdoc 33 */ 34 constructor(props: IProps) { 35 super(props); 36 37 this._onSetVideoLink = this._onSetVideoLink.bind(this); 38 } 39 40 /** 41 * Validates the entered video link by extracting the id and dispatches it. 42 * 43 * It returns a boolean to comply the Dialog behaviour: 44 * {@code true} - the dialog should be closed. 45 * {@code false} - the dialog should be left open. 46 * 47 * @param {string} link - The entered video link. 48 * @returns {boolean} 49 */ 50 _onSetVideoLink(link: string) { 51 const { onPostSubmit } = this.props; 52 53 const id = extractYoutubeIdOrURL(link); 54 55 if (!id) { 56 return false; 57 } 58 59 onPostSubmit(id); 60 61 return true; 62 } 63 }