import { useAppBridge, useAuthenticatedFetch } from "@saleor/app-sdk/app-bridge"; import { SALEOR_API_URL_HEADER, SALEOR_AUTHORIZATION_BEARER_HEADER } from "@saleor/app-sdk/const"; import { ChangeEvent, SyntheticEvent, useEffect, useState } from "react"; import { useDashboardNotification } from "@saleor/apps-shared"; import { Breadcrumbs, ButtonsBox, Layout, SkeletonLayout, TextLink } from "@saleor/apps-ui"; import { Box, Button, Input, Text } from "@saleor/macaw-ui/next"; import { useAppApi } from "../hooks/useAppApi"; interface ConfigurationField { key: string; value: string; } function Instructions() { return ( How to set up App will send events as Klaviyo metrics each time Saleor Event occurs. When first metric is sent, it should be available in Klaviyo to build on top of. Metric name can be customized, PUBLIC_TOKEN must be provided to enable the app. Useful links How to configure ); } function Configuration() { const { appBridgeState } = useAppBridge(); const { notifySuccess, notifyError } = useDashboardNotification(); const [configuration, setConfiguration] = useState(); const authenticatedFetch = useAuthenticatedFetch() as typeof window.fetch; const { data: configurationData, error } = useAppApi({ url: "/api/configuration", }); useEffect(() => { if (configurationData && !configuration) { setConfiguration(configurationData.data); } }, [configurationData, configuration]); /** * TODO Rewrite to tRPC */ const handleSubmit = (event: SyntheticEvent) => { event.preventDefault(); authenticatedFetch("/api/configuration", { method: "POST", body: JSON.stringify({ data: configuration }), }) .then(async (response) => { if (response.status !== 200) { throw new Error("Error saving configuration data"); } notifySuccess("Success", "Configuration updated successfully"); }) .catch(async () => { await notifyError( "Configuration update failed. Ensure fields are filled correctly and you have MANAGE_APPS permission", ); }); }; const onChange = (event: ChangeEvent) => { const { name, value } = event.target as HTMLInputElement; setConfiguration((prev) => prev!.map((prevField) => (prevField.key === name ? { ...prevField, value } : prevField)), ); }; if (error) { console.error("Can't establish connection with the App API: ", error); return (

⚠️ Can't connect with the App API

You may see this error because:
); } if (configuration === undefined) { return ; } return ( Configuration }> } > {configuration!.map(({ key, value }) => (
))}
); } export default Configuration;