-
Notifications
You must be signed in to change notification settings - Fork 0
Emails: Add button to send emails synchronously. #88
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
Draft
isabellalam12
wants to merge
1
commit into
08-24-emails-remove-defaults
Choose a base branch
from
08-24-emails-add-sync-button
base: 08-24-emails-remove-defaults
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import type { | ||
| SendAsyncRequest, | ||
| SendRequest, | ||
| EmailTemplate, | ||
| } from "@/features/emails/dto/emailDto"; | ||
|
|
||
| import { listTemplates, sendToEmailApi } from "@/features/emails/api/emailAPI"; | ||
| import { | ||
| showEmailError, | ||
| showEmailPending, | ||
| showEmailSuccess, | ||
| } from "@/features/emails/api/emailError"; | ||
| import { Button, Flex, Text } from "@mantine/core"; | ||
| import { modals } from "@mantine/modals"; | ||
| import { useMutation } from "@tanstack/react-query"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| export function SyncEmailSender({ | ||
| request, | ||
| }: { | ||
| request: SendAsyncRequest | null; | ||
| }) { | ||
| const [template, setTemplate] = useState<EmailTemplate | null>(null); | ||
|
|
||
| const mutation = useMutation({ | ||
| mutationFn: async (req: SendRequest) => sendToEmailApi(req), | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| // Find template subject and body from request.templateId | ||
| if (!request?.templateId) { | ||
| setTemplate(null); | ||
| return; | ||
| } | ||
|
|
||
| const loadTemplate = async () => { | ||
| try { | ||
| const templates = await listTemplates(); | ||
| const found = templates.find((t) => t.id === request.templateId); | ||
| setTemplate(found ?? null); | ||
| } catch { | ||
| setTemplate(null); | ||
| } | ||
| }; | ||
|
|
||
| void loadTemplate(); | ||
| }, [request?.templateId]); | ||
|
|
||
| useEffect(() => { | ||
| if (mutation.status === "pending") { | ||
| showEmailPending("pending", "Email sending in progress..."); | ||
| } | ||
| }, [mutation.status]); | ||
|
|
||
| const openModal = () => | ||
| modals.openConfirmModal({ | ||
| title: "Email Send Confirmation", | ||
| children: ( | ||
| <Text size="sm"> | ||
| Please confirm that you want to send {request?.messages.length} email | ||
| {request?.messages.length === 1 ? "" : "s"}. | ||
| </Text> | ||
| ), | ||
| labels: { confirm: "Confirm", cancel: "Cancel" }, | ||
| onCancel: () => | ||
| showEmailPending( | ||
| "Cancel", | ||
| `${request?.messages.length} Emails cancelled.`, | ||
| ), | ||
| onConfirm: () => void handleSend(), | ||
| }); | ||
|
|
||
| const handleSend = async () => { | ||
| if (!request) { | ||
| showEmailError("Preview first", "Preview before sending."); | ||
| return; | ||
| } | ||
|
|
||
| if (!template) { | ||
| showEmailError( | ||
| "Template missing", | ||
| "Could not load the template subject/body.", | ||
| ); | ||
| return; | ||
| } | ||
| // Convert SendAsyncRequest to SendRequest | ||
| const syncRequest: SendRequest = { | ||
| templateId: request.templateId, | ||
| subject: template.subject, | ||
| body: template.body, | ||
| replyTo: request.replyTo ?? null, | ||
| messages: request.messages, | ||
| }; | ||
|
|
||
| try { | ||
| await mutation.mutateAsync(syncRequest); | ||
| showEmailSuccess("Success", "Emails sent."); | ||
| } catch { | ||
| showEmailError("Error", "Unable to send emails."); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Flex> | ||
| <Button | ||
| fullWidth | ||
| loading={mutation.isPending} | ||
| onClick={openModal} | ||
| disabled={!request || !template} | ||
| > | ||
| Send Synchronous Emails | ||
| </Button> | ||
| </Flex> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical Race Condition: If the user rapidly changes templates, multiple
loadTemplate()calls will be in flight simultaneously and may resolve out of order, causing the wrong template to be set.Scenario:
Fix: Add cleanup to ignore stale results:
Spotted by Graphite

Is this helpful? React 👍 or 👎 to let us know.