File size: 4,497 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
	__experimentalText as Text,
	CheckboxControl,
	Button,
	Card,
	CardHeader,
	CardBody,
	CardFooter,
} from '@wordpress/components';
import { arrowLeft } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { useCallback, useEffect, useState } from 'react';
import { useScheduledUpdatesNotificationSettingsMutation } from 'calypso/data/plugins/use-scheduled-updates-notification-settings-mutation';
import { useScheduledUpdatesNotificationSettingsQuery } from 'calypso/data/plugins/use-scheduled-updates-notification-settings-query';
import { useDispatch, useSelector } from 'calypso/state';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';
import { fetchSettings } from 'calypso/state/notification-settings/actions';
import { getNotificationSettings } from 'calypso/state/notification-settings/selectors';
import { useSiteSlug } from './hooks/use-site-slug';

import './notification-settings.scss';

type Props = {
	onNavBack?: () => void;
};

export const NotificationSettings = ( { onNavBack }: Props ) => {
	const siteSlug = useSiteSlug();
	const translate = useTranslate();
	const dispatch = useDispatch();

	const { data: settings, isFetched } = useScheduledUpdatesNotificationSettingsQuery( siteSlug );
	const [ formValues, setFormValues ] = useState( {
		success: false,
		failure: false,
	} );
	useEffect( () => {
		dispatch( fetchSettings() );
	}, [ dispatch ] );

	const hasGlobalNotificationsDisabled = useSelector( ( state ) => {
		const settings = getNotificationSettings( state, 'wpcom' );
		return settings && ! settings.scheduled_updates;
	} );

	const {
		updateNotificationSettings,
		isPending: isSaving,
		isSuccess,
		isError,
	} = useScheduledUpdatesNotificationSettingsMutation( siteSlug );

	useEffect( () => {
		if ( isFetched && settings ) {
			setFormValues( {
				success: settings.success,
				failure: settings.failure,
			} );
		}
	}, [ isFetched, settings ] );

	useEffect( () => {
		if ( isSuccess ) {
			onNavBack?.();
			dispatch( successNotice( translate( 'Your notification settings have been saved.' ) ) );
		} else if ( isError ) {
			dispatch(
				errorNotice( translate( 'Failed to save notification settings. Please try again.' ) )
			);
		}
	}, [ isSuccess, isError, onNavBack ] );

	const handleCheckboxChange = ( field: keyof typeof formValues ) => ( checked: boolean ) => {
		setFormValues( ( prevValues ) => ( {
			...prevValues,
			[ field ]: checked,
		} ) );
	};

	const onSave = useCallback( () => {
		updateNotificationSettings( formValues );
	}, [ formValues, updateNotificationSettings ] );

	return (
		<Card className="plugins-update-manager">
			<CardHeader size="extraSmall">
				<div className="ch-placeholder">
					{ onNavBack && (
						<Button icon={ arrowLeft } onClick={ onNavBack }>
							{ translate( 'Back' ) }
						</Button>
					) }
				</div>
				<Text>{ translate( 'Notification Settings' ) }</Text>
				<div className="ch-placeholder"></div>
			</CardHeader>
			<CardBody className="notification-settings-form">
				<form>
					<label>{ translate( 'Email me' ) }</label>
					<Text className="info-msg">
						{ translate(
							'Receive email notifications to stay informed about the performance of the plugin updates.'
						) }
					</Text>

					<div className="form-field">
						<CheckboxControl
							label={ translate( 'On successful updates' ) }
							checked={ formValues.success }
							onChange={ handleCheckboxChange( 'success' ) }
							disabled={ ! isFetched || hasGlobalNotificationsDisabled }
						/>
					</div>
					<div className="form-field">
						<CheckboxControl
							label={ translate( 'On failed updates' ) }
							checked={ formValues.failure }
							onChange={ handleCheckboxChange( 'failure' ) }
							disabled={ ! isFetched || hasGlobalNotificationsDisabled }
						/>
					</div>

					{ hasGlobalNotificationsDisabled && (
						<Text className="info-msg">
							{ translate(
								"You've opted out of WordPress.com's scheduled update notifications. Head to {{notificationSettingsLink}}Notification Settings{{/notificationSettingsLink}} to re-enable them.",
								{
									components: {
										notificationSettingsLink: <a href="/me/notifications/updates" />,
									},
								}
							) }
						</Text>
					) }
				</form>
			</CardBody>
			<CardFooter>
				<Button variant="primary" disabled={ isSaving } onClick={ onSave }>
					{ translate( 'Save' ) }
				</Button>
			</CardFooter>
		</Card>
	);
};