Skip to content

Commit

Permalink
Merge pull request #2483 from zetkin/feature/issue-2441-send-preview-…
Browse files Browse the repository at this point in the history
…email-to-custom-address

Implemented input of preview email address
  • Loading branch information
richardolsson authored Jan 19, 2025
2 parents f937e29 + 00e61a8 commit fb9f3dc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { FC } from 'react';
import { FC, useEffect, useState } from 'react';
import {
Alert,
Box,
Button,
CircularProgress,
TextField,
Typography,
} from '@mui/material';
import z from 'zod';
import { ErrorOutlined } from '@mui/icons-material';

import messageIds from 'features/emails/l10n/messageIds';
import { Msg } from 'core/i18n';
import useCurrentUser from 'features/user/hooks/useCurrentUser';
import useSendTestEmail from 'features/emails/hooks/useSendTestEmail';

const emailAddressIsValid = (emailAddress: string): boolean =>
z.string().email().safeParse(emailAddress).success;

const PreviewTab: FC = () => {
const user = useCurrentUser();
const { emailWasSent, isLoading, reset, sendTestEmail } = useSendTestEmail();
const [emailError, setEmailError] = useState(false);
const [destinationEmailAddress, setDestinationEmailAddress] = useState('');
useEffect(() => {
if (user && destinationEmailAddress == '') {
setDestinationEmailAddress(user.email);
}
}, [user?.email]);

if (!user) {
return null;
Expand All @@ -28,7 +41,21 @@ const PreviewTab: FC = () => {
<Typography>
<Msg id={messageIds.editor.settings.tabs.preview.sendTo} />
</Typography>
<Typography>{user.email}</Typography>
<TextField
error={emailError}
onChange={(ev) => setDestinationEmailAddress(ev.target.value)}
type="email"
value={destinationEmailAddress}
/>
{emailError && (
<Alert color="error" icon={<ErrorOutlined />}>
<Typography>
<Msg
id={messageIds.editor.settings.tabs.preview.invalidEmailAddress}
/>
</Typography>
</Alert>
)}
{emailWasSent && (
<Alert color="success">
<Typography mb={2}>
Expand All @@ -47,7 +74,12 @@ const PreviewTab: FC = () => {
{!emailWasSent && (
<Button
onClick={() => {
sendTestEmail();
const emailValid = emailAddressIsValid(destinationEmailAddress);
setEmailError(!emailValid);
if (!emailValid) {
return;
}
sendTestEmail({ ...user, email: destinationEmailAddress });
}}
variant="contained"
>
Expand Down
17 changes: 5 additions & 12 deletions src/features/emails/hooks/useSendTestEmail.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { useState } from 'react';

import useUser from 'core/hooks/useUser';
import { useApiClient, useNumericRouteParams } from 'core/hooks';
import { ZetkinUser } from '../../../utils/types/zetkin';

export default function useSendTestEmail() {
const apiClient = useApiClient();
const [isLoading, setIsLoading] = useState(false);
const [emailWasSent, setEmailWasSent] = useState(false);

// TODO: Get these as props instead
const user = useUser();
if (!user) {
// This should never happen on pages where this hook is used
throw new Error('Sending test email only works for signed in users');
}

const { emailId, orgId } = useNumericRouteParams();

return {
Expand All @@ -23,14 +16,14 @@ export default function useSendTestEmail() {
reset: () => {
setEmailWasSent(false);
},
sendTestEmail: async () => {
sendTestEmail: async (recipient: ZetkinUser) => {
setIsLoading(true);
await apiClient.post(`/api/orgs/${orgId}/emails/${emailId}/preview`, {
recipients: [
{
email: user.email,
first_name: user.first_name,
last_name: user.last_name,
email: recipient.email,
first_name: recipient.first_name,
last_name: recipient.last_name,
},
],
});
Expand Down
1 change: 1 addition & 0 deletions src/features/emails/l10n/messageIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default makeMessages('feat.emails', {
instructions: m(
'Here you can send this email to yourself to preview what it will look like for the recipients. '
),
invalidEmailAddress: m('This is not a valid email address'),
okButton: m('OK!'),
sendButton: m('Send'),
sendTo: m('The email will be sent to this address:'),
Expand Down

0 comments on commit fb9f3dc

Please sign in to comment.