From 39362db69dc1625406bda3f2ac54d193e78ef886 Mon Sep 17 00:00:00 2001 From: Eric van der Vlist Date: Sun, 5 Mar 2023 17:25:37 +0100 Subject: [PATCH] Implementing a send invitation action --- src/components/user/User.tsx | 47 +++++++++++++++++++++++++++++++++++- src/routes/user/[id].tsx | 42 -------------------------------- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/src/components/user/User.tsx b/src/components/user/User.tsx index bb60e92..8375942 100644 --- a/src/components/user/User.tsx +++ b/src/components/user/User.tsx @@ -22,13 +22,58 @@ import { update } from '~/lib/update'; import { replicationDocument } from '~/lib/replication-document'; import { get } from '~/lib/get'; import { v4 as uuid } from 'uuid'; -import { sendInvitation } from '~/routes/user/[id]'; + +import { createServerAction$ } from 'solid-start/server'; +import { readConfig } from '~/server-only-lib/read-config'; +import { createTransport } from 'nodemailer'; +import { resolve } from 'path'; +import hbs from 'nodemailer-express-handlebars'; + interface Props { values?: () => any; } const User: Component = (props) => { + + const [sendingInvitation, sendInvitation] = createServerAction$( + async (args: { + id: string; + mail: string; + webInvitation: string; + appInvitation: string; + }) => { + const { id, mail, webInvitation, appInvitation } = args; + const { credentials, mailer } = readConfig(); + const transporter = createTransport(mailer); + + const handlebarOptions = { + viewEngine: { + partialsDir: resolve('src/mail-template/'), + defaultLayout: false, + }, + viewPath: resolve('src/mail-template/'), + }; + transporter.use('compile', hbs(handlebarOptions)); + + const code = [...Array(6)].map((_) => (Math.random() * 10) | 0).join(''); + + const mailOptions = { + from: '"Dyomedea app" ', // sender address + to: mail, // list of receivers + subject: "Invitation à configurer l'application Dyomedea.", + template: 'invitation', // the name of the template file i.e email.handlebars + context: { + id, + webInvitation, + appInvitation, + }, + }; + + await transporter.sendMail(mailOptions); + } + ); + const navigate = useNavigate(); const [progress, setProgress] = createSignal(-1); diff --git a/src/routes/user/[id].tsx b/src/routes/user/[id].tsx index 2c397af..4839e4f 100644 --- a/src/routes/user/[id].tsx +++ b/src/routes/user/[id].tsx @@ -3,49 +3,7 @@ import User from '~/components/user'; import { createEffect, createSignal, Show } from 'solid-js'; import { adminCredentials, CheckCredentials } from '~/components/credentials'; import { get } from '~/lib/get'; -import { createServerAction$ } from 'solid-start/server'; -import { readConfig } from '~/server-only-lib/read-config'; -import { createTransport } from 'nodemailer'; -import { resolve } from 'path'; -import hbs from 'nodemailer-express-handlebars'; -export const [sendingInvitation, sendInvitation] = createServerAction$( - async (args: { - id: string; - mail: string; - webInvitation: string; - appInvitation: string; - }) => { - const { id, mail, webInvitation, appInvitation } = args; - const { credentials, mailer } = readConfig(); - const transporter = createTransport(mailer); - - const handlebarOptions = { - viewEngine: { - partialsDir: resolve('src/mail-template/'), - defaultLayout: false, - }, - viewPath: resolve('src/mail-template/'), - }; - transporter.use('compile', hbs(handlebarOptions)); - - const code = [...Array(6)].map((_) => (Math.random() * 10) | 0).join(''); - - const mailOptions = { - from: '"Dyomedea app" ', // sender address - to: mail, // list of receivers - subject: "Invitation à configurer l'application Dyomedea.", - template: 'invitation', // the name of the template file i.e email.handlebars - context: { - id, - webInvitation, - appInvitation, - }, - }; - - await transporter.sendMail(mailOptions); - } -); export default () => { const { id } = useParams();