|
| 1 | +import { |
| 2 | + PopoverDomRef, |
| 3 | + Ui5CustomEvent, |
| 4 | + TextAreaDomRef, |
| 5 | + Button, |
| 6 | + ButtonDomRef, |
| 7 | + Popover, |
| 8 | + Form, |
| 9 | + FormGroup, |
| 10 | + FormItem, |
| 11 | + Label, |
| 12 | + Link, |
| 13 | + RatingIndicator, |
| 14 | + TextArea, |
| 15 | +} from '@ui5/webcomponents-react'; |
| 16 | +import { Dispatch, RefObject, SetStateAction, useRef, useState } from 'react'; |
| 17 | +import { useAuthOnboarding } from '../../spaces/onboarding/auth/AuthContextOnboarding'; |
| 18 | +import { useTranslation } from 'react-i18next'; |
| 19 | +import { ButtonClickEventDetail } from '@ui5/webcomponents/dist/Button.js'; |
| 20 | +import PopoverPlacement from '@ui5/webcomponents/dist/types/PopoverPlacement.js'; |
| 21 | +import ButtonDesign from '@ui5/webcomponents/dist/types/ButtonDesign.js'; |
| 22 | + |
| 23 | +type UI5RatingIndicatorElement = HTMLElement & { value: number }; |
| 24 | + |
| 25 | +export function FeedbackButton() { |
| 26 | + const feedbackPopoverRef = useRef<PopoverDomRef>(null); |
| 27 | + const [feedbackMessage, setFeedbackMessage] = useState(''); |
| 28 | + const [feedbackSent, setFeedbackSent] = useState(false); |
| 29 | + const [feedbackPopoverOpen, setFeedbackPopoverOpen] = useState(false); |
| 30 | + const [rating, setRating] = useState(0); |
| 31 | + const { user } = useAuthOnboarding(); |
| 32 | + |
| 33 | + const onFeedbackClick = (e: Ui5CustomEvent<ButtonDomRef, ButtonClickEventDetail>) => { |
| 34 | + feedbackPopoverRef.current!.opener = e.target; |
| 35 | + setFeedbackPopoverOpen(!feedbackPopoverOpen); |
| 36 | + }; |
| 37 | + |
| 38 | + const onFeedbackMessageChange = (event: Ui5CustomEvent<TextAreaDomRef, { value: string; previousValue: string }>) => { |
| 39 | + const newValue = event.target.value; |
| 40 | + setFeedbackMessage(newValue); |
| 41 | + }; |
| 42 | + |
| 43 | + async function onFeedbackSent() { |
| 44 | + const payload = { |
| 45 | + message: feedbackMessage, |
| 46 | + rating: rating.toString(), |
| 47 | + user: user?.email, |
| 48 | + environment: window.location.hostname, |
| 49 | + }; |
| 50 | + try { |
| 51 | + await fetch('/api/feedback', { |
| 52 | + method: 'POST', |
| 53 | + headers: { |
| 54 | + 'Content-Type': 'application/json', |
| 55 | + }, |
| 56 | + body: JSON.stringify(payload), |
| 57 | + }); |
| 58 | + } catch (err) { |
| 59 | + console.log(err); |
| 60 | + } finally { |
| 61 | + setFeedbackSent(true); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return ( |
| 66 | + <> |
| 67 | + <Button icon="feedback" tooltip="Feedback" design={ButtonDesign.Transparent} onClick={onFeedbackClick} /> |
| 68 | + <FeedbackPopover |
| 69 | + open={feedbackPopoverOpen} |
| 70 | + setOpen={setFeedbackPopoverOpen} |
| 71 | + popoverRef={feedbackPopoverRef} |
| 72 | + setRating={setRating} |
| 73 | + rating={rating} |
| 74 | + feedbackMessage={feedbackMessage} |
| 75 | + feedbackSent={feedbackSent} |
| 76 | + onFeedbackSent={onFeedbackSent} |
| 77 | + onFeedbackMessageChange={onFeedbackMessageChange} |
| 78 | + /> |
| 79 | + </> |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +const FeedbackPopover = ({ |
| 84 | + open, |
| 85 | + setOpen, |
| 86 | + popoverRef, |
| 87 | + setRating, |
| 88 | + rating, |
| 89 | + onFeedbackSent, |
| 90 | + feedbackMessage, |
| 91 | + onFeedbackMessageChange, |
| 92 | + feedbackSent, |
| 93 | +}: { |
| 94 | + open: boolean; |
| 95 | + setOpen: (arg0: boolean) => void; |
| 96 | + popoverRef: RefObject<PopoverDomRef | null>; |
| 97 | + setRating: Dispatch<SetStateAction<number>>; |
| 98 | + rating: number; |
| 99 | + onFeedbackSent: () => void; |
| 100 | + feedbackMessage: string; |
| 101 | + onFeedbackMessageChange: ( |
| 102 | + event: Ui5CustomEvent< |
| 103 | + TextAreaDomRef, |
| 104 | + { |
| 105 | + value: string; |
| 106 | + previousValue: string; |
| 107 | + } |
| 108 | + >, |
| 109 | + ) => void; |
| 110 | + feedbackSent: boolean; |
| 111 | +}) => { |
| 112 | + const { t } = useTranslation(); |
| 113 | + |
| 114 | + const onRatingChange = (event: Event & { target: UI5RatingIndicatorElement }) => { |
| 115 | + setRating(event.target.value); |
| 116 | + }; |
| 117 | + |
| 118 | + return ( |
| 119 | + <> |
| 120 | + <Popover ref={popoverRef} placement={PopoverPlacement.Bottom} open={open} onClose={() => setOpen(false)}> |
| 121 | + <div |
| 122 | + style={{ |
| 123 | + padding: '1rem', |
| 124 | + width: '250px', |
| 125 | + }} |
| 126 | + > |
| 127 | + {!feedbackSent ? ( |
| 128 | + <Form headerText={t('ShellBar.feedbackHeader')}> |
| 129 | + <FormGroup> |
| 130 | + <FormItem labelContent={<Label style={{ color: 'black' }}>{t('ShellBar.feedbackRatingLabel')}</Label>}> |
| 131 | + <RatingIndicator value={rating} max={5} onChange={onRatingChange} /> |
| 132 | + </FormItem> |
| 133 | + <FormItem |
| 134 | + className="formAlignLabelStart" |
| 135 | + labelContent={<Label style={{ color: 'black' }}>{t('ShellBar.feedbackMessageLabel')}</Label>} |
| 136 | + > |
| 137 | + <TextArea |
| 138 | + value={feedbackMessage} |
| 139 | + placeholder={t('ShellBar.feedbackPlaceholder')} |
| 140 | + rows={5} |
| 141 | + onInput={onFeedbackMessageChange} |
| 142 | + /> |
| 143 | + </FormItem> |
| 144 | + <FormItem> |
| 145 | + <Button design="Emphasized" onClick={() => onFeedbackSent()}> |
| 146 | + {t('ShellBar.feedbackButton')} |
| 147 | + </Button> |
| 148 | + </FormItem> |
| 149 | + <FormItem> |
| 150 | + <Label style={{ color: 'gray' }}> |
| 151 | + {t('ShellBar.feedbackNotificationText')} |
| 152 | + <Link |
| 153 | + href="https://github.com/openmcp-project/ui-frontend/issues/new/choose" |
| 154 | + target="_blank" |
| 155 | + rel="noreferrer" |
| 156 | + > |
| 157 | + {t('ShellBar.feedbackNotificationAction')} |
| 158 | + </Link> |
| 159 | + </Label> |
| 160 | + </FormItem> |
| 161 | + </FormGroup> |
| 162 | + </Form> |
| 163 | + ) : ( |
| 164 | + <Label>{t('ShellBar.feedbackThanks')}</Label> |
| 165 | + )} |
| 166 | + </div> |
| 167 | + </Popover> |
| 168 | + </> |
| 169 | + ); |
| 170 | +}; |
0 commit comments