File size: 1,971 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
import clsx from 'clsx';
import { find, size } from 'lodash';
import PropTypes from 'prop-types';
import { PureComponent } from 'react';
import { connect } from 'react-redux';
import DeviceSelector from './device-selector';
import StreamHeader from './stream-header';
import StreamOptions from './stream-options';

class NotificationSettingsFormStream extends PureComponent {
	static propTypes = {
		blogId: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ).isRequired,
		stream: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ),
		settingKeys: PropTypes.arrayOf( PropTypes.string ).isRequired,
		settings: PropTypes.oneOfType( [ PropTypes.array, PropTypes.object ] ).isRequired,
		onToggle: PropTypes.func.isRequired,
	};

	state = { selectedDeviceId: null };

	getStreamSettings = () => {
		let { stream, settings } = this.props;

		if ( this.props.devices && size( this.props.devices ) > 0 ) {
			stream = parseInt( this.state.selectedDeviceId || this.props.devices[ 0 ].id, 10 );
			settings = find( this.props.settings, { device_id: stream } );
		}

		return { stream, settings };
	};

	onChangeDevices = ( event ) =>
		this.setState( { selectedDeviceId: parseInt( event.target.value, 10 ) } );

	render() {
		const { stream, settings } = this.getStreamSettings();

		return (
			<div className={ clsx( 'notification-settings-form-stream', this.props.className ) }>
				{ ( () => {
					if ( this.props.devices ) {
						return (
							<DeviceSelector
								devices={ this.props.devices }
								selectedDeviceId={ stream }
								onChange={ this.onChangeDevices }
							/>
						);
					}

					return <StreamHeader stream={ this.props.stream } />;
				} )() }
				<StreamOptions
					blogId={ this.props.blogId }
					stream={ stream }
					settingKeys={ this.props.settingKeys }
					settings={ settings }
					onToggle={ this.props.onToggle }
				/>
			</div>
		);
	}
}

export default connect()( NotificationSettingsFormStream );