import { useAppBridge, withAuthorization } 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 { useAppApi } from "../hooks/useAppApi"; import { AppColumnsLayout } from "../lib/ui/app-columns-layout"; import { useDashboardNotification } from "@saleor/apps-shared"; import { Box, BoxProps, Text, Input, Button } from "@saleor/macaw-ui/next"; interface ConfigurationField { key: string; value: string; } function Section(props: BoxProps) { return ; } function Instructions() { const { appBridge } = useAppBridge(); const openExternalUrl = (url: string) => { // eslint-disable-next-line appBridge?.dispatch({ type: "redirect", payload: { newContext: true, actionId: "redirect_from_klaviyo_app", to: url, }, }); }; 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 { 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(); fetch("/api/configuration", { method: "POST", headers: [ ["content-type", "application/json"], [SALEOR_API_URL_HEADER, appBridgeState?.saleorApiUrl!], [SALEOR_AUTHORIZATION_BEARER_HEADER, appBridgeState?.token!], ], 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:
  • Internet connection has been lost
  • Application installation process is still in progress. If you use Vercel, you may need to wait for redeployment of the app - try again in a minute.
  • Application is misconfigured. If you would like to know more how auth configuration is kept,{" "} go to APL documentation .
); } if (configuration === undefined) { return

Loading...

; } return (
Klaviyo configuration {configuration!.map(({ key, value }) => (
))}
); } export default Configuration;