You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractSharedVideoDialog.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // @flow
  2. import { Component } from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { getYoutubeId } from '../functions';
  5. /**
  6. * The type of the React {@code Component} props of
  7. * {@link AbstractSharedVideoDialog}.
  8. */
  9. export type Props = {
  10. /**
  11. * Invoked to update the shared video link.
  12. */
  13. dispatch: Dispatch<any>,
  14. /**
  15. * Function to be invoked after typing a valid video.
  16. */
  17. onPostSubmit: Function,
  18. /**
  19. * Invoked to obtain translated strings.
  20. */
  21. t: Function
  22. };
  23. /**
  24. * Implements an abstract class for {@code SharedVideoDialog}.
  25. */
  26. export default class AbstractSharedVideoDialog<S: *> extends Component < Props, S > {
  27. /**
  28. * Instantiates a new component.
  29. *
  30. * @inheritdoc
  31. */
  32. constructor(props: Props) {
  33. super(props);
  34. this._onSetVideoLink = this._onSetVideoLink.bind(this);
  35. }
  36. _onSetVideoLink: string => boolean;
  37. /**
  38. * Validates the entered video link by extracting the id and dispatches it.
  39. *
  40. * It returns a boolean to comply the Dialog behaviour:
  41. * {@code true} - the dialog should be closed.
  42. * {@code false} - the dialog should be left open.
  43. *
  44. * @param {string} link - The entered video link.
  45. * @returns {boolean}
  46. */
  47. _onSetVideoLink(link: string) {
  48. if (!link) {
  49. return false;
  50. }
  51. const trimmedLink = link.trim();
  52. if (!trimmedLink) {
  53. return false;
  54. }
  55. const { onPostSubmit } = this.props;
  56. const youtubeId = getYoutubeId(trimmedLink);
  57. if (youtubeId) {
  58. onPostSubmit(youtubeId);
  59. return true;
  60. }
  61. // Check if the URL is valid, native may crash otherwise.
  62. try {
  63. // eslint-disable-next-line no-new
  64. new URL(trimmedLink);
  65. } catch (_) {
  66. return false;
  67. }
  68. onPostSubmit(trimmedLink);
  69. return true;
  70. }
  71. }