2018-05-12 14:30:06 +01:00
|
|
|
import React, { PureComponent, Fragment } from 'react';
|
2018-03-24 11:52:26 +00:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { IntlProvider, addLocaleData } from 'react-intl';
|
|
|
|
import { getLocale } from '../locales';
|
|
|
|
import MediaGallery from '../components/media_gallery';
|
2018-05-12 14:30:06 +01:00
|
|
|
import Video from '../features/video';
|
|
|
|
import Card from '../features/status/components/card';
|
2018-03-24 11:52:26 +00:00
|
|
|
import ModalRoot from '../components/modal_root';
|
|
|
|
import MediaModal from '../features/ui/components/media_modal';
|
|
|
|
import { fromJS } from 'immutable';
|
|
|
|
|
|
|
|
const { localeData, messages } = getLocale();
|
|
|
|
addLocaleData(localeData);
|
|
|
|
|
2018-05-12 14:30:06 +01:00
|
|
|
const MEDIA_COMPONENTS = { MediaGallery, Video, Card };
|
|
|
|
|
|
|
|
export default class MediaContainer extends PureComponent {
|
2018-03-24 11:52:26 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
locale: PropTypes.string.isRequired,
|
2018-05-12 14:30:06 +01:00
|
|
|
components: PropTypes.object.isRequired,
|
2018-03-24 11:52:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
media: null,
|
|
|
|
index: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
handleOpenMedia = (media, index) => {
|
2018-05-12 14:30:06 +01:00
|
|
|
document.body.classList.add('media-standalone__body');
|
2018-03-24 11:52:26 +00:00
|
|
|
this.setState({ media, index });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCloseMedia = () => {
|
2018-05-12 14:30:06 +01:00
|
|
|
document.body.classList.remove('media-standalone__body');
|
2018-03-24 11:52:26 +00:00
|
|
|
this.setState({ media: null, index: null });
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-05-12 14:30:06 +01:00
|
|
|
const { locale, components } = this.props;
|
2018-03-24 11:52:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<IntlProvider locale={locale} messages={messages}>
|
2018-05-12 14:30:06 +01:00
|
|
|
<Fragment>
|
|
|
|
{[].map.call(components, (component, i) => {
|
|
|
|
const componentName = component.getAttribute('data-component');
|
|
|
|
const Component = MEDIA_COMPONENTS[componentName];
|
|
|
|
const { media, card, ...props } = JSON.parse(component.getAttribute('data-props'));
|
|
|
|
|
|
|
|
Object.assign(props, {
|
|
|
|
...(media ? { media: fromJS(media) } : {}),
|
|
|
|
...(card ? { card: fromJS(card) } : {}),
|
|
|
|
});
|
2018-03-24 11:52:26 +00:00
|
|
|
|
|
|
|
return ReactDOM.createPortal(
|
2018-05-12 14:30:06 +01:00
|
|
|
<Component onOpenMedia={this.handleOpenMedia} {...props} key={`media-${i}`} />,
|
|
|
|
component,
|
2018-03-24 11:52:26 +00:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
<ModalRoot onClose={this.handleCloseMedia}>
|
|
|
|
{this.state.media === null || this.state.index === null ? null : (
|
|
|
|
<MediaModal
|
|
|
|
media={this.state.media}
|
|
|
|
index={this.state.index}
|
|
|
|
onClose={this.handleCloseMedia}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</ModalRoot>
|
2018-05-12 14:30:06 +01:00
|
|
|
</Fragment>
|
2018-03-24 11:52:26 +00:00
|
|
|
</IntlProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|