1+ import { useStorage } from '@ea11y-apps/global/hooks' ;
2+ import { mixpanelEvents , mixpanelService } from '@ea11y-apps/global/services' ;
13import { useState , createContext , useContext } from '@wordpress/element' ;
4+ import { escapeHTML } from '@wordpress/escape-html' ;
5+ import { __ } from '@wordpress/i18n' ;
6+ import APIReview from '../api' ;
27
38/**
49 * Context Component.
@@ -13,14 +18,99 @@ const SettingsProvider = ({ children }) => {
1318 const [ rating , setRating ] = useState ( 0 ) ;
1419 const [ feedback , setFeedback ] = useState ( '' ) ;
1520 const [ currentPage , setCurrentPage ] = useState ( 'ratings' ) ;
16- const [ nextButtonDisabled , setNextButtonDisabled ] = useState ( true ) ;
1721 const [ isOpened , setIsOpened ] = useState ( true ) ;
22+ const { save, get } = useStorage ( ) ;
1823
1924 // Notification
2025 const [ showNotification , setShowNotification ] = useState ( false ) ;
2126 const [ notificationMessage , setNotificationMessage ] = useState ( '' ) ;
2227 const [ notificationType , setNotificationType ] = useState ( '' ) ;
2328
29+ const errorNotification = ( message ) => {
30+ setNotificationMessage ( message ) ;
31+ setNotificationType ( 'error' ) ;
32+ setShowNotification ( true ) ;
33+ } ;
34+
35+ const successNotification = ( message ) => {
36+ setNotificationMessage ( message ) ;
37+ setNotificationType ( 'success' ) ;
38+ setShowNotification ( true ) ;
39+ } ;
40+
41+ /**
42+ * Close the popover.
43+ * @param {Object } event
44+ * @param {string } reason
45+ */
46+ const handleClose = ( event , reason ) => {
47+ if ( 'backdropClick' !== reason ) {
48+ setIsOpened ( false ) ;
49+ }
50+
51+ mixpanelService . sendEvent ( mixpanelEvents . review . dismissClicked ) ;
52+ } ;
53+
54+ const handleSubmit = async (
55+ close ,
56+ avoidClosing = false ,
57+ submittedRating = null ,
58+ ) => {
59+ const ratingToSubmit = submittedRating !== null ? submittedRating : rating ;
60+ try {
61+ const response = await APIReview . sendFeedback ( {
62+ rating : ratingToSubmit ,
63+ feedback,
64+ } ) . then ( async ( res ) => {
65+ await save ( {
66+ ea11y_review_data : {
67+ ...get . data . ea11y_review_data ,
68+ rating : parseInt ( ratingToSubmit ) ,
69+ feedback : escapeHTML ( feedback ) ,
70+ submitted : true ,
71+ } ,
72+ } ) ;
73+
74+ return res ;
75+ } ) ;
76+
77+ if ( ratingToSubmit && ! feedback ) {
78+ mixpanelService . sendEvent ( mixpanelEvents . review . starSelected , {
79+ rating : parseInt ( ratingToSubmit ) ,
80+ } ) ;
81+ }
82+
83+ if ( feedback ) {
84+ mixpanelService . sendEvent ( mixpanelEvents . review . feedbackSubmitted , {
85+ feedback_text : escapeHTML ( feedback ) ,
86+ rating : parseInt ( ratingToSubmit ) ,
87+ } ) ;
88+ }
89+
90+ if ( ! response ?. success && parseInt ( rating ) < 4 ) {
91+ /**
92+ * Show success message if the feedback was already submitted.
93+ */
94+ await successNotification (
95+ __ ( 'Feedback already submitted' , 'pojo-accessibility' ) ,
96+ ) ;
97+ } else if ( response ?. success && parseInt ( rating ) < 4 ) {
98+ await successNotification (
99+ __ ( 'Thank you for your feedback!' , 'pojo-accessibility' ) ,
100+ ) ;
101+ }
102+
103+ if ( ! avoidClosing ) {
104+ await close ( ) ;
105+ }
106+
107+ return true ;
108+ } catch ( e ) {
109+ errorNotification ( __ ( 'Failed to submit!' , 'pojo-accessibility' ) ) ;
110+ return false ;
111+ }
112+ } ;
113+
24114 return (
25115 < SettingsContext . Provider
26116 value = { {
@@ -30,8 +120,6 @@ const SettingsProvider = ({ children }) => {
30120 setFeedback,
31121 currentPage,
32122 setCurrentPage,
33- nextButtonDisabled,
34- setNextButtonDisabled,
35123 showNotification,
36124 setShowNotification,
37125 notificationMessage,
@@ -40,6 +128,10 @@ const SettingsProvider = ({ children }) => {
40128 setNotificationType,
41129 isOpened,
42130 setIsOpened,
131+ handleClose,
132+ handleSubmit,
133+ errorNotification,
134+ successNotification,
43135 } }
44136 >
45137 { children }
0 commit comments