File size: 2,723 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { SegmentedControl } from '@automattic/components';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import Notice from 'calypso/components/notice';
import EditorMediaModalGalleryEdit from './edit';
import EditorMediaModalGalleryPreviewIndividual from './preview-individual';
import EditorMediaModalGalleryPreviewShortcode from './preview-shortcode';

const noop = () => {};

/* eslint-disable wpcalypso/jsx-classname-namespace */

class EditorMediaModalGalleryPreview extends Component {
	static propTypes = {
		site: PropTypes.object,
		settings: PropTypes.object,
		onUpdateSetting: PropTypes.func,
		invalidItemDropped: PropTypes.bool,
		onDismissInvalidItemDropped: PropTypes.func,
	};

	static defaultProps = {
		settings: Object.freeze( {} ),
		onUpdateSetting: noop,
		invalidItemDropped: false,
		onDismissInvalidItemDropped: noop,
	};

	state = {
		isEditing: false,
	};

	renderPreviewModeToggle() {
		const { translate } = this.props;

		return (
			<SegmentedControl className="editor-media-modal-gallery__preview-toggle" compact>
				<SegmentedControl.Item
					selected={ ! this.state.isEditing }
					onClick={ () => this.setState( { isEditing: false } ) }
				>
					{ translate( 'Preview' ) }
				</SegmentedControl.Item>
				<SegmentedControl.Item
					selected={ this.state.isEditing }
					onClick={ () => this.setState( { isEditing: true } ) }
				>
					{ translate( 'Edit' ) }
				</SegmentedControl.Item>
			</SegmentedControl>
		);
	}

	renderPreview() {
		const { site, settings, onUpdateSetting } = this.props;

		if ( ! site || ! settings.items ) {
			return;
		}

		if ( this.state.isEditing ) {
			return (
				<EditorMediaModalGalleryEdit
					site={ site }
					settings={ settings }
					onUpdateSetting={ onUpdateSetting }
				/>
			);
		}

		if ( 'individual' === settings.type ) {
			return <EditorMediaModalGalleryPreviewIndividual items={ settings.items } />;
		}

		return <EditorMediaModalGalleryPreviewShortcode siteId={ site.ID } settings={ settings } />;
	}

	render() {
		const { translate } = this.props;

		return (
			<div className="editor-media-modal-gallery__preview">
				{ this.props.invalidItemDropped && (
					<Notice
						status="is-warning"
						onDismissClick={ this.props.onDismissInvalidItemDropped }
						isCompact
					>
						{ translate(
							'Galleries can only include images. All other uploads will be added to your media library.'
						) }
					</Notice>
				) }
				<div className="editor-media-modal-gallery__preview-wrapper">{ this.renderPreview() }</div>
				{ this.renderPreviewModeToggle() }
			</div>
		);
	}
}

export default localize( EditorMediaModalGalleryPreview );