import React, { useEffect } from 'react'; import { TextField } from '@availity/mui-textfield'; import { LoadingButton, Button, IconButton } from '@availity/mui-button'; import { ToggleButtonGroup, ToggleButton } from '@availity/mui-toggle-button'; import { Grid } from '@availity/mui-layout'; import { SvgIconProps } from '@mui/material/SvgIcon'; import { ToggleButtonProps } from '@mui/material/ToggleButton'; import { styled } from '@mui/material/styles'; import { FaceFrownIcon, FaceNeutralIcon, FaceSmileIcon } from '@availity/mui-icon'; import { FormLabel } from '@availity/mui-form-utils'; import { avRegionsApi } from '@availity/api-axios'; import { useForm, SubmitHandler, Controller } from 'react-hook-form'; interface Inputs { feedback: string; smileField: string; } interface SmileButtonProps extends ToggleButtonProps { disabled: boolean; Icon: (props: SvgIconProps) => React.JSX.Element; label: string; value: string; } interface FeedbackFormProps { // eslint-disable-next-line @typescript-eslint/no-explicit-any analytics: { info: (entries: Record) => any }; appName: string; handleClose: () => void; loading: boolean; sent: boolean; setLoading: React.Dispatch>; setSent: React.Dispatch>; } const SmileButtons = styled(ToggleButtonGroup, { name: 'AvFeedbackContainer', slot: 'SmileButtons' })({}); const FormActions = styled(Grid, { name: 'AvFeedbackContainer', slot: 'FormActions' })({}); const SmileButton = ({ disabled, Icon, label, value, ...props }: SmileButtonProps) => (
); export const FeedbackForm = ({ analytics, appName, handleClose, loading, sent, setLoading, setSent, }: FeedbackFormProps): React.JSX.Element | null => { const { control, formState: { errors }, handleSubmit, register, setValue, watch, } = useForm(); const onSubmit: SubmitHandler = async ({ smileField, ...values }) => { setLoading(true); try { const response = await avRegionsApi.getCurrentRegion(); await analytics.info({ surveyId: `${appName.replaceAll(/\s/g, '_')}_Smile_Survey`, smileLocation: `${appName}`, smile: `icon-${smileField}`, url: window.location.href, region: response.data.regions[0] && response.data.regions[0].id, userAgent: window.navigator.userAgent, submitTime: new Date(), ...values, // Spread the form values onto the logger }); setSent(true); setLoading(false); } catch { setSent(false); setLoading(false); } }; useEffect(() => { if (sent) { setTimeout(() => { handleClose(); }, 2000); } }, [sent]); const options = [ { Icon: FaceSmileIcon, label: 'What do you like?', value: 'smile', }, { Icon: FaceNeutralIcon, label: 'What would you improve?', value: 'meh', }, { Icon: FaceFrownIcon, label: "What don't you like?", value: 'frown' }, ]; const smileFieldValue = watch('smileField'); const getFeedbackLabel = () => { const option = options.find((option) => option.value === smileFieldValue); return option?.label || 'What would you improve?'; }; if (!sent) { return ( ( ( ))} {...field} aria-labelledby="feedback-form-header" onChange={(event: React.MouseEvent, value: string) => { setValue(field.name, value); }} size="medium" exclusive /> )} /> Send Feedback ); } return null; };