|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; |
| 4 | + |
| 5 | +export default function FeedbackWidget() { |
| 6 | + const { siteConfig } = useDocusaurusContext(); |
| 7 | + const GOOGLE_SCRIPT_URL = siteConfig.customFields?.googleScriptUrl as string; |
| 8 | + |
| 9 | + const [vote, setVote] = useState<null | 'yes' | 'no'>(null); |
| 10 | + const [comment, setComment] = useState(''); |
| 11 | + const [submitted, setSubmitted] = useState(false); |
| 12 | + const [submitting, setSubmitting] = useState(false); |
| 13 | + |
| 14 | + const handleVote = (value: 'yes' | 'no') => { |
| 15 | + setVote(value); |
| 16 | + if (value === 'yes') { |
| 17 | + submitFeedback(value, ''); |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + const submitFeedback = async (voteValue: string, commentValue: string) => { |
| 22 | + setSubmitting(true); |
| 23 | + try { |
| 24 | + const data = { |
| 25 | + url: window.location.href, |
| 26 | + vote: voteValue, |
| 27 | + comment: commentValue, |
| 28 | + userAgent: navigator.userAgent, |
| 29 | + }; |
| 30 | + |
| 31 | + await fetch(GOOGLE_SCRIPT_URL, { |
| 32 | + method: 'POST', |
| 33 | + mode: 'no-cors', |
| 34 | + headers: { |
| 35 | + 'Content-Type': 'text/plain;charset=utf-8', |
| 36 | + }, |
| 37 | + body: JSON.stringify(data), |
| 38 | + }); |
| 39 | + |
| 40 | + setSubmitted(true); |
| 41 | + } catch (error) { |
| 42 | + setSubmitted(true); |
| 43 | + } finally { |
| 44 | + setSubmitting(false); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const handleCommentSubmit = (e: React.FormEvent) => { |
| 49 | + e.preventDefault(); |
| 50 | + if (vote) { |
| 51 | + submitFeedback(vote, comment); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + if (submitted) { |
| 56 | + return ( |
| 57 | + <div className="feedback-container"> |
| 58 | + <div className="feedback-widget"> |
| 59 | + <p className="thank-you">Thanks for your feedback! 🙏🏽</p> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <div className="feedback-container"> |
| 67 | + <div className="feedback-widget"> |
| 68 | + {!vote ? ( |
| 69 | + <div className="initial-state"> |
| 70 | + <span className="question">Was this helpful?</span> |
| 71 | + <div className="buttons"> |
| 72 | + <button |
| 73 | + className={clsx("button", "thumbs-up")} |
| 74 | + onClick={() => handleVote('yes')} |
| 75 | + aria-label="Yes" |
| 76 | + > |
| 77 | + 👍 Yes |
| 78 | + </button> |
| 79 | + <button |
| 80 | + className={clsx("button", "thumbs-down")} |
| 81 | + onClick={() => setVote('no')} |
| 82 | + aria-label="No" |
| 83 | + > |
| 84 | + 👎 No |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + ) : ( |
| 89 | + <form className="comment-form" onSubmit={handleCommentSubmit}> |
| 90 | + <p className="follow-up"> |
| 91 | + {vote === 'yes' ? 'What was most helpful?' : 'How can we improve this page?'} |
| 92 | + </p> |
| 93 | + <textarea |
| 94 | + className="textarea" |
| 95 | + value={comment} |
| 96 | + onChange={(e) => setComment(e.target.value)} |
| 97 | + placeholder={vote === 'yes' ? "Optional comments..." : "Please tell us what's missing or unclear..."} |
| 98 | + rows={3} |
| 99 | + /> |
| 100 | + <div className="form-actions"> |
| 101 | + <button |
| 102 | + type="button" |
| 103 | + className="cancel-button" |
| 104 | + onClick={() => setVote(null)} |
| 105 | + > |
| 106 | + Cancel |
| 107 | + </button> |
| 108 | + <button |
| 109 | + type="submit" |
| 110 | + className="submit-button" |
| 111 | + disabled={submitting} |
| 112 | + > |
| 113 | + {submitting ? 'Sending...' : 'Send Feedback'} |
| 114 | + </button> |
| 115 | + </div> |
| 116 | + </form> |
| 117 | + )} |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments