2023-09-06 12:22:54 +00:00
|
|
|
|
import { useAppBridge, useAuthenticatedFetch } from "@saleor/app-sdk/app-bridge";
|
2023-02-09 09:41:54 +00:00
|
|
|
|
import { SALEOR_API_URL_HEADER, SALEOR_AUTHORIZATION_BEARER_HEADER } from "@saleor/app-sdk/const";
|
2023-02-28 19:18:34 +00:00
|
|
|
|
|
2023-02-09 09:41:54 +00:00
|
|
|
|
import { ChangeEvent, SyntheticEvent, useEffect, useState } from "react";
|
|
|
|
|
|
2023-04-14 15:40:49 +00:00
|
|
|
|
import { useDashboardNotification } from "@saleor/apps-shared";
|
2023-09-06 12:22:54 +00:00
|
|
|
|
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";
|
2023-02-09 09:41:54 +00:00
|
|
|
|
|
|
|
|
|
interface ConfigurationField {
|
|
|
|
|
key: string;
|
|
|
|
|
value: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Instructions() {
|
|
|
|
|
return (
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<Box>
|
|
|
|
|
<Text as={"h3"} variant="heading" marginY={4}>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
How to set up
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</Text>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<Text as="p" marginBottom={2}>
|
|
|
|
|
App will send events as Klaviyo metrics each time Saleor Event occurs.
|
|
|
|
|
</Text>
|
|
|
|
|
<Text as="p" marginBottom={2}>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
When first metric is sent, it should be available in Klaviyo to build on top of.
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</Text>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<Text as="p" marginBottom={4}>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
Metric name can be customized, PUBLIC_TOKEN must be provided to enable the app.
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</Text>
|
|
|
|
|
<Text as={"h3"} variant="heading">
|
|
|
|
|
Useful links
|
|
|
|
|
</Text>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<TextLink href="https://github.com/saleor/saleor-app-klaviyo" newTab>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
Visit repository & readme
|
2023-09-06 12:22:54 +00:00
|
|
|
|
</TextLink>
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<Text as={"h3"} variant="heading">
|
|
|
|
|
How to configure
|
|
|
|
|
</Text>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<TextLink
|
2023-02-09 09:41:54 +00:00
|
|
|
|
href="https://help.klaviyo.com/hc/en-us/articles/115005062267-How-to-Manage-Your-Account-s-API-Keys"
|
2023-09-06 12:22:54 +00:00
|
|
|
|
newTab
|
2023-02-09 09:41:54 +00:00
|
|
|
|
>
|
|
|
|
|
Read about public tokens
|
2023-09-06 12:22:54 +00:00
|
|
|
|
</TextLink>
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</li>
|
|
|
|
|
<li>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<TextLink href="https://www.klaviyo.com/account#api-keys-tab" newTab>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
Get public token here
|
2023-09-06 12:22:54 +00:00
|
|
|
|
</TextLink>
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</li>
|
|
|
|
|
<li>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<TextLink
|
2023-02-09 09:41:54 +00:00
|
|
|
|
href="https://help.klaviyo.com/hc/en-us/articles/115005076787-Guide-to-Managing-Your-Metrics"
|
2023-09-06 12:22:54 +00:00
|
|
|
|
newTab
|
2023-02-09 09:41:54 +00:00
|
|
|
|
>
|
|
|
|
|
Read about metrics
|
2023-09-06 12:22:54 +00:00
|
|
|
|
</TextLink>
|
2023-06-28 07:45:35 +00:00
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
</Box>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Configuration() {
|
|
|
|
|
const { appBridgeState } = useAppBridge();
|
2023-04-14 15:40:49 +00:00
|
|
|
|
const { notifySuccess, notifyError } = useDashboardNotification();
|
2023-02-09 09:41:54 +00:00
|
|
|
|
const [configuration, setConfiguration] = useState<ConfigurationField[]>();
|
2023-09-06 12:22:54 +00:00
|
|
|
|
const authenticatedFetch = useAuthenticatedFetch() as typeof window.fetch;
|
2023-02-09 09:41:54 +00:00
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2023-09-06 12:22:54 +00:00
|
|
|
|
authenticatedFetch("/api/configuration", {
|
2023-02-09 09:41:54 +00:00
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify({ data: configuration }),
|
|
|
|
|
})
|
|
|
|
|
.then(async (response) => {
|
|
|
|
|
if (response.status !== 200) {
|
|
|
|
|
throw new Error("Error saving configuration data");
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 15:40:49 +00:00
|
|
|
|
notifySuccess("Success", "Configuration updated successfully");
|
2023-02-09 09:41:54 +00:00
|
|
|
|
})
|
|
|
|
|
.catch(async () => {
|
2023-04-14 15:40:49 +00:00
|
|
|
|
await notifyError(
|
2023-09-06 12:22:54 +00:00
|
|
|
|
"Configuration update failed. Ensure fields are filled correctly and you have MANAGE_APPS permission",
|
2023-04-14 15:40:49 +00:00
|
|
|
|
);
|
2023-02-09 09:41:54 +00:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onChange = (event: ChangeEvent) => {
|
|
|
|
|
const { name, value } = event.target as HTMLInputElement;
|
2023-04-18 13:10:00 +00:00
|
|
|
|
|
2023-02-09 09:41:54 +00:00
|
|
|
|
setConfiguration((prev) =>
|
2023-09-06 12:22:54 +00:00
|
|
|
|
prev!.map((prevField) => (prevField.key === name ? { ...prevField, value } : prevField)),
|
2023-02-09 09:41:54 +00:00
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error("Can't establish connection with the App API: ", error);
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<h1>⚠️ Can't connect with the App API</h1>
|
|
|
|
|
You may see this error because:
|
|
|
|
|
<ul>
|
|
|
|
|
<li>Internet connection has been lost</li>
|
|
|
|
|
<li>
|
|
|
|
|
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.
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
Application is misconfigured. If you would like to know more how auth configuration is
|
|
|
|
|
kept,{" "}
|
|
|
|
|
<a
|
|
|
|
|
href="https://github.com/saleor/saleor-app-sdk/blob/main/docs/apl.md"
|
|
|
|
|
target="_blank"
|
|
|
|
|
rel="noreferrer"
|
|
|
|
|
>
|
|
|
|
|
go to APL documentation
|
|
|
|
|
</a>
|
|
|
|
|
.
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (configuration === undefined) {
|
2023-09-06 12:22:54 +00:00
|
|
|
|
return <SkeletonLayout.Section />;
|
2023-02-09 09:41:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-09-06 12:22:54 +00:00
|
|
|
|
<Box>
|
|
|
|
|
<Breadcrumbs marginBottom={10}>
|
|
|
|
|
<Breadcrumbs.Item>Configuration</Breadcrumbs.Item>
|
|
|
|
|
</Breadcrumbs>
|
|
|
|
|
<Layout.AppSection heading="Set up integration" sideContent={<Instructions />}>
|
|
|
|
|
<Layout.AppSectionCard
|
|
|
|
|
as={"form"}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
footer={
|
|
|
|
|
<ButtonsBox>
|
|
|
|
|
<Button type="submit" variant="primary">
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</ButtonsBox>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<Box display={"grid"} gap={4} gridAutoFlow={"row"}>
|
|
|
|
|
{configuration!.map(({ key, value }) => (
|
|
|
|
|
<div key={key}>
|
|
|
|
|
<Input label={key} name={key} onChange={onChange} value={value} />
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</Box>
|
|
|
|
|
</Layout.AppSectionCard>
|
|
|
|
|
</Layout.AppSection>
|
|
|
|
|
</Box>
|
2023-02-09 09:41:54 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 07:45:35 +00:00
|
|
|
|
export default Configuration;
|