about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/containers/media_galleries_container.js
blob: a694578821ee64c6b1766e98109c546423a9b42d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { IntlProvider, addLocaleData } from 'react-intl';
import { getLocale } from 'mastodon/locales';
import MediaGallery from 'flavours/glitch/components/media_gallery';
import ModalRoot from 'flavours/glitch/components/modal_root';
import MediaModal from 'flavours/glitch/features/ui/components/media_modal';
import { fromJS } from 'immutable';

const { localeData, messages } = getLocale();
addLocaleData(localeData);

export default class MediaGalleriesContainer extends React.PureComponent {

  static propTypes = {
    locale: PropTypes.string.isRequired,
    galleries: PropTypes.object.isRequired,
  };

  state = {
    media: null,
    index: null,
  };

  handleOpenMedia = (media, index) => {
    document.body.classList.add('media-gallery-standalone__body');
    this.setState({ media, index });
  }

  handleCloseMedia = () => {
    document.body.classList.remove('media-gallery-standalone__body');
    this.setState({ media: null, index: null });
  }

  render () {
    const { locale, galleries } = this.props;

    return (
      <IntlProvider locale={locale} messages={messages}>
        <React.Fragment>
          {[].map.call(galleries, gallery => {
            const { media, ...props } = JSON.parse(gallery.getAttribute('data-props'));

            return ReactDOM.createPortal(
              <MediaGallery
                {...props}
                media={fromJS(media)}
                onOpenMedia={this.handleOpenMedia}
              />,
              gallery
            );
          })}
          <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>
        </React.Fragment>
      </IntlProvider>
    );
  }

}