Skip to content

Commit

Permalink
Adding upcoming events into email directly
Browse files Browse the repository at this point in the history
  • Loading branch information
gregv committed Dec 22, 2023
1 parent a53bb60 commit 51c01d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 19 additions & 1 deletion app/routes/admin+/_email+/CustomEmail.server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Container, Html, Link, Tailwind, Text } from '@react-email/components'
import tailwindConfig from '../../../../tailwind.config.ts'
import { siteBaseUrl, siteName } from '~/data.ts'

export function CustomEmail({ message }: { message: string }) {
export function CustomEmail({ upcomingEvents, message }: { upcomingEvents: any[], message: string }) {
return (
<Tailwind config={tailwindConfig}>
<Html lang="en" dir="ltr">
Expand All @@ -17,6 +17,24 @@ export function CustomEmail({ message }: { message: string }) {
<Link href={siteBaseUrl + "/calendar"} style={{ color: '#007BFF', textDecoration: 'none' }}>
View upcoming events
</Link>
{
// List out upcoming events with event.title
// Render event.start and event.end with a date format
upcomingEvents.map(event => {
return (
<div key={event.title}>
<Text>
🐴 {event.title} {event.start.toLocaleString()} - {event.end.toLocaleTimeString()}
</Text>
</div>
)
}
)




}
</p>
</Container>
</Html>
Expand Down
12 changes: 11 additions & 1 deletion app/routes/admin+/_email+/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export async function action({ request, params }: DataFunctionArgs) {
]
const selectedRoles = roles.filter(role => submission.payload[role] === 'on')
const recipients = await getRecipientsFromRoles(selectedRoles)
const upcomingEvents = await getUpcomingEvents(5);

if (recipients.length === 0) {
return json(
Expand All @@ -82,7 +83,7 @@ export async function action({ request, params }: DataFunctionArgs) {
sendEmail({
to: recipient,
subject: submission.payload.subject,
react: <CustomEmail message={submission.payload.message} />,
react: <CustomEmail upcomingEvents={upcomingEvents} message={submission.payload.message} />,
}).then(result => {
if (result.status === 'error') {
console.error(
Expand Down Expand Up @@ -241,6 +242,15 @@ export default function Email() {
)
}

async function getUpcomingEvents(limit: number) {
const events = await prisma.event.findMany({
where: { start: { gt: new Date() } },
take: limit,
})
return events
}


async function getRecipientsFromRoles(roles: string[]) {
const recipients = new Set<string>()
if (roles.includes('allVolunteers')) {
Expand Down

0 comments on commit 51c01d9

Please sign in to comment.