import { SmtpConfiguration } from "../configuration/smtp-config-schema"; import { BoxWithBorder } from "../../../components/box-with-border"; import { Box, Button, Text } from "@saleor/macaw-ui/next"; import { defaultPadding } from "../../../components/ui-defaults"; import { SectionWithDescription } from "../../../components/section-with-description"; import { useRouter } from "next/router"; import { smtpUrls } from "../urls"; import { TextLink } from "@saleor/apps-ui"; import React from "react"; import { messageEventTypesLabels } from "../../event-handlers/message-event-types"; import { BoxFooter } from "../../../components/box-footer"; import { Table } from "../../../components/table"; import { useDashboardNotification } from "@saleor/apps-shared"; import { SmtpUpdateEventArray, smtpUpdateEventArraySchema, } from "../configuration/smtp-config-input-schema"; import { zodResolver } from "@hookform/resolvers/zod"; import { trpcClient } from "../../trpc/trpc-client"; import { useForm } from "react-hook-form"; import { setBackendErrors } from "../../../lib/set-backend-errors"; interface SmtpEventsSectionProps { configuration: SmtpConfiguration; } export const SmtpEventsSection = ({ configuration }: SmtpEventsSectionProps) => { const { notifySuccess, notifyError } = useDashboardNotification(); const router = useRouter(); // Sort events by displayed label const eventsSorted = configuration.events.sort((a, b) => messageEventTypesLabels[a.eventType].localeCompare(messageEventTypesLabels[b.eventType]) ); const { register, handleSubmit, setError } = useForm({ defaultValues: { configurationId: configuration.id, events: eventsSorted, }, resolver: zodResolver(smtpUpdateEventArraySchema), }); const trpcContext = trpcClient.useContext(); const { mutate } = trpcClient.smtpConfiguration.updateEventArray.useMutation({ onSuccess: async () => { notifySuccess("Configuration saved"); trpcContext.smtpConfiguration.invalidate(); }, onError(error) { setBackendErrors({ error, setError, notifyError }); }, }); return ( Chose which Saleor events should send emails via SMTP. You can modify the email templates using{" "} MJML {" "} syntax. } >
{ mutate(data); })} > Active Event type {eventsSorted.map((event, index) => ( {messageEventTypesLabels[event.eventType]} ))}
); };