Skip to content

Commit

Permalink
add evaluation of email in Send Feedback (#2110)
Browse files Browse the repository at this point in the history
Co-authored-by: Joaquin Battilana <[email protected]>
Co-authored-by: Mark Hinschberger <[email protected]>
  • Loading branch information
3 people authored Nov 11, 2024
1 parent 3a3d226 commit 6b5f861
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/components/primitives/BasicModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface BasicModalProps {
setOpen: (value: boolean) => void;
withCloseButton?: boolean;
contentMaxWidth?: number;
closeCallback?: () => void;
disableEnforceFocus?: boolean;
}

Expand All @@ -17,10 +18,14 @@ export const BasicModal = ({
withCloseButton = true,
contentMaxWidth = 420,
children,
closeCallback,
disableEnforceFocus,
...props
}: BasicModalProps) => {
const handleClose = () => setOpen(false);
const handleClose = () => {
if (closeCallback) closeCallback();
setOpen(false);
};

return (
<Modal
Expand Down
40 changes: 37 additions & 3 deletions src/layouts/FeedbackDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,41 @@ export const FeedbackModal = () => {
const [success, setSuccess] = useState(false);
const [error, setError] = useState(false);
const [email, setEmail] = useState('');
const [emailError, setEmailError] = useState('');
const [dirtyEmailField, setDirtyEmailField] = useState(false);

const onBlur = () => {
if (!dirtyEmailField) setDirtyEmailField(true);
};

useEffect(() => {
if (feedbackDialogOpen) {
setSuccess(false);
setError(false);
setEmailError('');
}
}, [feedbackDialogOpen]);

const handleEmailChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const emailValue = e.target.value;
setEmail(emailValue);

const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(emailValue)) {
setEmailError('Please enter a valid email address');
} else {
setEmailError('');
}
};

const handleFeedbackSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();

if (emailError || !email) {
setEmailError('Please enter a valid email address');
return;
}

setIsLoading(true);

const url = process.env.NEXT_PUBLIC_API_BASEURL + '/feedback';
Expand All @@ -53,11 +77,18 @@ export const FeedbackModal = () => {
} finally {
setIsLoading(false);
setValue('');
setEmail('');
}
};

const onClose = () => {
setEmailError('');
setValue('');
setDirtyEmailField(false);
};

return (
<BasicModal open={feedbackDialogOpen} setOpen={setFeedbackOpen}>
<BasicModal open={feedbackDialogOpen} setOpen={setFeedbackOpen} closeCallback={onClose}>
<Box
sx={{
display: 'flex',
Expand Down Expand Up @@ -147,9 +178,12 @@ export const FeedbackModal = () => {

<TextField
// label="Email"
onBlur={onBlur}
fullWidth
value={email}
onChange={(e) => setEmail(e.target.value)}
onChange={handleEmailChange}
error={dirtyEmailField && !!emailError}
helperText={dirtyEmailField ? emailError : undefined}
sx={{ mb: 2 }}
/>
<Box sx={{ display: 'flex', alignItems: 'center', mb: 1 }}>
Expand All @@ -175,7 +209,7 @@ export const FeedbackModal = () => {
}}
/>
<Box display="flex" flexDirection={'row-reverse'} mt={3}>
<Button disabled={!value} variant="contained" type="submit">
<Button disabled={!value || !!emailError} variant="contained" type="submit">
<Trans>Send Feedback</Trans>
</Button>
</Box>
Expand Down

1 comment on commit 6b5f861

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit was deployed on ipfs

Please sign in to comment.