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.

84 lines
2.6 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Base from '../../../components/modal_root';
  4. import BundleContainer from '../containers/bundle_container';
  5. import BundleModalError from './bundle_modal_error';
  6. import ModalLoading from './modal_loading';
  7. import ActionsModal from './actions_modal';
  8. import MediaModal from './media_modal';
  9. import VideoModal from './video_modal';
  10. import BoostModal from './boost_modal';
  11. import FavouriteModal from './favourite_modal';
  12. import DoodleModal from './doodle_modal';
  13. import ConfirmationModal from './confirmation_modal';
  14. import {
  15. OnboardingModal,
  16. MuteModal,
  17. ReportModal,
  18. SettingsModal,
  19. EmbedModal,
  20. ListEditor,
  21. } from 'flavours/glitch/util/async-components';
  22. const MODAL_COMPONENTS = {
  23. 'MEDIA': () => Promise.resolve({ default: MediaModal }),
  24. 'ONBOARDING': OnboardingModal,
  25. 'VIDEO': () => Promise.resolve({ default: VideoModal }),
  26. 'BOOST': () => Promise.resolve({ default: BoostModal }),
  27. 'FAVOURITE': () => Promise.resolve({ default: FavouriteModal }),
  28. 'DOODLE': () => Promise.resolve({ default: DoodleModal }),
  29. 'CONFIRM': () => Promise.resolve({ default: ConfirmationModal }),
  30. 'MUTE': MuteModal,
  31. 'REPORT': ReportModal,
  32. 'SETTINGS': SettingsModal,
  33. 'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
  34. 'EMBED': EmbedModal,
  35. 'LIST_EDITOR': ListEditor,
  36. };
  37. export default class ModalRoot extends React.PureComponent {
  38. static propTypes = {
  39. type: PropTypes.string,
  40. props: PropTypes.object,
  41. onClose: PropTypes.func.isRequired,
  42. };
  43. getSnapshotBeforeUpdate () {
  44. return { visible: !!this.props.type };
  45. }
  46. componentDidUpdate (prevProps, prevState, { visible }) {
  47. if (visible) {
  48. document.body.classList.add('with-modals--active');
  49. } else {
  50. document.body.classList.remove('with-modals--active');
  51. }
  52. }
  53. renderLoading = modalId => () => {
  54. return ['MEDIA', 'VIDEO', 'BOOST', 'FAVOURITE', 'DOODLE', 'CONFIRM', 'ACTIONS'].indexOf(modalId) === -1 ? <ModalLoading /> : null;
  55. }
  56. renderError = (props) => {
  57. const { onClose } = this.props;
  58. return <BundleModalError {...props} onClose={onClose} />;
  59. }
  60. render () {
  61. const { type, props, onClose } = this.props;
  62. const visible = !!type;
  63. return (
  64. <Base onClose={onClose} noEsc={props ? props.noEsc : false}>
  65. {visible && (
  66. <BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading(type)} error={this.renderError} renderDelay={200}>
  67. {(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
  68. </BundleContainer>
  69. )}
  70. </Base>
  71. );
  72. }
  73. }