File size: 4,706 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
146
147
148
149
150
151
152
153
154
155
156
157
158
import { Button, FormLabel } from '@automattic/components';
import { Modal } from '@wordpress/components';
import { Icon, close } from '@wordpress/icons';
import { useTranslate } from 'i18n-calypso';
import { ChangeEvent, useCallback, useEffect, useState } from 'react';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormTextarea from 'calypso/components/forms/form-textarea';
import ReviewsRatingsStars from 'calypso/components/reviews-rating-stars/reviews-ratings-stars';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { successNotice } from 'calypso/state/notices/actions';
import useSubmitProductFeedback from './use-submit-product-feedback';

import './style.scss';

type Props = {
	show: boolean;
	onClose?: () => void;
};

const DEFAULT_FEEDBACK_VALUE = '';
const DEFAULT_RATING_VALUE = 0;

export default function UserFeedbackModalForm( { show, onClose }: Props ) {
	const translate = useTranslate();
	const dispatch = useDispatch();

	const [ feedback, setFeedback ] = useState( DEFAULT_FEEDBACK_VALUE );
	const [ rating, setRating ] = useState( DEFAULT_RATING_VALUE );

	const { isSubmittingFeedback, submitFeedback, isSubmissionSuccessful } =
		useSubmitProductFeedback();

	const onModalClose = useCallback( () => {
		setFeedback( DEFAULT_FEEDBACK_VALUE );
		setRating( DEFAULT_RATING_VALUE );
		onClose?.();

		dispatch( recordTracksEvent( 'calypso_jetpack_user_feedback_form_close' ) );
	}, [ dispatch, onClose ] );

	useEffect( () => {
		if ( isSubmissionSuccessful ) {
			dispatch(
				successNotice( translate( 'Thank you for your feedback!' ), {
					id: 'submit-product-feedback-success',
					duration: 5000,
				} )
			);
			onModalClose();
		}
	}, [ dispatch, isSubmissionSuccessful, onModalClose, translate ] );

	const onFeedbackChange = useCallback( ( event: ChangeEvent< HTMLInputElement > ) => {
		setFeedback( event.currentTarget.value );
	}, [] );

	const onRatingChange = useCallback(
		( rating: number ) => {
			dispatch( recordTracksEvent( 'calypso_jetpack_user_feedback_form_rating_click' ) );
			setRating( rating );
		},
		[ dispatch ]
	);

	const hasCompletedForm = !! feedback && !! rating;

	const onSubmit = useCallback( () => {
		if ( ! hasCompletedForm ) {
			return;
		}

		dispatch(
			recordTracksEvent( 'calypso_jetpack_user_feedback_form_submit', {
				rating,
				feedback,
			} )
		);

		const sourceUrl = `${ window.location.origin }${ window.location.pathname }`;
		submitFeedback( { feedback, rating, source_url: sourceUrl } );
	}, [ dispatch, feedback, hasCompletedForm, rating, submitFeedback ] );

	useEffect( () => {
		if ( show ) {
			dispatch( recordTracksEvent( 'calypso_jetpack_user_feedback_form_open' ) );
		}
	}, [ dispatch, show ] );

	if ( ! show ) {
		return null;
	}

	return (
		<Modal
			className="user-feedback-modal-form"
			onRequestClose={ onModalClose }
			__experimentalHideHeader
		>
			<div className="user-feedback-modal-form__main">
				<Button
					className="user-feedback-modal-form__close-button"
					plain
					onClick={ onModalClose }
					aria-label={ translate( 'Close' ) }
				>
					<Icon size={ 24 } icon={ close } />
				</Button>

				<h1 className="user-feedback-modal-form__title">
					{ translate( 'Help us make Jetpack Manage even better' ) }
				</h1>

				<p className="user-feedback-modal-form__instruction">
					{ translate(
						'Your product feedback is extremely valuable to us. Our goal is to help you do your work better and more efficiently - all feedback is sent to our product team and helps inform our development roadmap.'
					) }
				</p>

				<FormFieldset>
					<FormLabel htmlFor="textarea">
						{ translate( 'What can we do to make Jetpack Manage better for you?' ) }
					</FormLabel>
					<FormTextarea
						name="textarea"
						id="textarea"
						placeholder="Add your feedback here"
						value={ feedback }
						onChange={ onFeedbackChange }
						onClick={ () =>
							dispatch( recordTracksEvent( 'calypso_jetpack_user_feedback_form_textarea_click' ) )
						}
					/>
				</FormFieldset>

				<FormFieldset>
					<FormLabel htmlFor="textarea">
						{ translate( 'How satisfied are you with Jetpack Manage?' ) }
					</FormLabel>
					<ReviewsRatingsStars rating={ rating } onSelectRating={ onRatingChange } />
				</FormFieldset>
			</div>

			<div className="user-feedback-modal-form__footer">
				<Button
					busy={ isSubmittingFeedback }
					className="user-feedback-modal-form__footer-submit"
					primary
					disabled={ ! hasCompletedForm }
					onClick={ onSubmit }
				>
					{ translate( 'Submit your feedback' ) }
				</Button>
			</div>
		</Modal>
	);
}