import { Box, Button, Input, Text } from "@saleor/macaw-ui/next"; import { NextPage } from "next"; import { SectionWithDescription } from "../../../components/section-with-description"; import { BoxWithBorder } from "../../../components/box-with-border"; import { defaultPadding } from "../../../components/ui-defaults"; import { BoxFooter } from "../../../components/box-footer"; import { trpcClient } from "../../../modules/trpc/trpc-client"; import { useDashboardNotification } from "@saleor/apps-shared/src/use-dashboard-notification"; import { Controller, useForm } from "react-hook-form"; import { SendgridCreateConfigurationInput } from "../../../modules/sendgrid/configuration/sendgrid-config-input-schema"; import { BasicLayout } from "../../../components/basic-layout"; import { useRouter } from "next/router"; const NewSendgridConfigurationPage: NextPage = () => { const router = useRouter(); const { notifySuccess, notifyError } = useDashboardNotification(); const { handleSubmit, control, setError } = useForm(); const { mutate: createConfiguration } = trpcClient.sendgridConfiguration.createConfiguration.useMutation({ onSuccess: async (data, variables) => { notifySuccess("Configuration saved"); router.push(`/configuration/sendgrid/edit/${data.id}`); }, onError(error) { let isFieldErrorSet = false; const fieldErrors = error.data?.zodError?.fieldErrors || {}; for (const fieldName in fieldErrors) { for (const message of fieldErrors[fieldName] || []) { isFieldErrorSet = true; setError(fieldName as keyof SendgridCreateConfigurationInput, { type: "manual", message, }); } } const formErrors = error.data?.zodError?.formErrors || []; const formErrorMessage = formErrors.length ? formErrors.join("\n") : undefined; notifyError( "Could not save the configuration", isFieldErrorSet ? "Submitted form contain errors" : "Error saving configuration", formErrorMessage ); }, }); return ( Connect Sendgrid with Saleor. Provide unique name for your configuration - you can create more than one. For example - production and development. Then, pass your API Key. Obtain it here. } >
{ createConfiguration({ ...data, }); })} > ( )} /> ( // TODO: add validation )} />
); }; export default NewSendgridConfigurationPage;