File size: 2,452 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
import PropTypes from 'prop-types';
import { useState } from 'react';
import { useNotificationDevicesQuery } from 'calypso/data/notification-devices/use-notification-devices-query';
import Labels from './labels';
import Stream from './stream';
import StreamSelector from './stream-selector';

/**
 * Module variables
 */
const streams = {
	TIMELINE: 'timeline',
	EMAIL: 'email',
	DEVICES: 'devices',
};

function NotificationSettingsForm( { blogId, settingKeys, settings, onToggle } ) {
	const [ selectedStream, setSelectedStream ] = useState( streams.TIMELINE );
	const { data: devices = [] } = useNotificationDevicesQuery();

	const getSelectedStreamSettings = () => {
		if ( isNaN( selectedStream ) ) {
			return settings[ selectedStream ];
		}

		return settings.devices?.find( ( { device_id } ) => device_id === parseInt( selectedStream ) );
	};

	const selectedStreamSettings = getSelectedStreamSettings();

	return (
		<div className="notification-settings-form">
			<StreamSelector
				selectedStream={ selectedStream }
				onChange={ setSelectedStream }
				settings={ selectedStreamSettings }
			/>
			<div className="notification-settings-form__streams">
				<Labels settingKeys={ settingKeys } />
				<Stream
					key={ streams.TIMELINE }
					blogId={ blogId }
					stream={ streams.TIMELINE }
					settingKeys={ settingKeys }
					settings={ settings[ streams.TIMELINE ] }
					onToggle={ onToggle }
				/>
				<Stream
					key={ streams.EMAIL }
					blogId={ blogId }
					stream={ streams.EMAIL }
					settingKeys={ settingKeys }
					settings={ settings[ streams.EMAIL ] }
					onToggle={ onToggle }
				/>
				{ devices && devices.length > 0 && (
					<Stream
						key={ streams.DEVICES }
						blogId={ blogId }
						devices={ devices }
						settingKeys={ settingKeys }
						settings={ settings[ streams.DEVICES ] }
						onToggle={ onToggle }
					/>
				) }
				<Stream
					key="selected-stream"
					className="selected-stream"
					blogId={ blogId }
					stream={ selectedStream }
					settingKeys={ settingKeys }
					settings={ selectedStreamSettings }
					onToggle={ onToggle }
				/>
			</div>
		</div>
	);
}

NotificationSettingsForm.propTypes = {
	blogId: PropTypes.oneOfType( [ PropTypes.string, PropTypes.number ] ).isRequired,
	settingKeys: PropTypes.arrayOf( PropTypes.string ).isRequired,
	settings: PropTypes.object.isRequired,
	onToggle: PropTypes.func.isRequired,
};

export default NotificationSettingsForm;