Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add evaluation of email in Send Feedback #2110

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

export const BasicModal = ({
Expand All @@ -16,9 +17,13 @@ export const BasicModal = ({
withCloseButton = true,
contentMaxWidth = 420,
children,
closeCallback,
...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
Loading