Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions App.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { BrowserRouter } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { NavigationMenu } from "@shopify/app-bridge-react";
import { NavMenu } from "@shopify/app-bridge-react";
import Routes from "./Routes";

import {
AppBridgeProvider,
QueryProvider,
PolarisProvider,
} from "./components";
import { QueryProvider, PolarisProvider } from "./components";

export default function App() {
// Any .tsx or .jsx files in /pages will become a route
Expand All @@ -18,19 +14,13 @@ export default function App() {
return (
<PolarisProvider>
<BrowserRouter>
<AppBridgeProvider>
<QueryProvider>
<NavigationMenu
navigationLinks={[
{
label: t("NavigationMenu.pageName"),
destination: "/pagename",
},
]}
/>
<Routes pages={pages} />
</QueryProvider>
</AppBridgeProvider>
<QueryProvider>
<NavMenu>
<a href="/" rel="home" />
<a href="/pagename">{t("NavigationMenu.pageName")}</a>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add home page if the app root is not / <a href="/app" rel="home">Home</a>

</NavMenu>
<Routes pages={pages} />
</QueryProvider>
</BrowserRouter>
</PolarisProvider>
);
Expand Down
94 changes: 44 additions & 50 deletions components/ProductsCard.jsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,73 @@
import { useState } from "react";
import { Card, TextContainer, Text } from "@shopify/polaris";
import { Toast } from "@shopify/app-bridge-react";
import { useAppBridge } from "@shopify/app-bridge-react";
import { useTranslation } from "react-i18next";
import { useAppQuery, useAuthenticatedFetch } from "../hooks";
import { useQuery } from "react-query";

export function ProductsCard() {
const emptyToastProps = { content: null };
const [isLoading, setIsLoading] = useState(true);
const [toastProps, setToastProps] = useState(emptyToastProps);
const fetch = useAuthenticatedFetch();
const shopify = useAppBridge();
const { t } = useTranslation();
const [isPopulating, setIsPopulating] = useState(false);
const productsCount = 5;

const {
data,
refetch: refetchProductCount,
isLoading: isLoadingCount,
isRefetching: isRefetchingCount,
} = useAppQuery({
url: "/api/products/count",
reactQueryOptions: {
onSuccess: () => {
setIsLoading(false);
},
} = useQuery({
queryKey: ["productCount"],
queryFn: async () => {
const response = await fetch("/api/products/count");
return await response.json();
},
refetchOnWindowFocus: false,
});

const toastMarkup = toastProps.content && !isRefetchingCount && (
<Toast {...toastProps} onDismiss={() => setToastProps(emptyToastProps)} />
);
const setPopulating = (flag) => {
shopify.loading(flag);
setIsPopulating(flag);
};

const handlePopulate = async () => {
setIsLoading(true);
const response = await fetch("/api/products", {method: "POST"});
setPopulating(true);
const response = await fetch("/api/products", { method: "POST" });

if (response.ok) {
await refetchProductCount();
setToastProps({
content: t("ProductsCard.productsCreatedToast", {
count: productsCount,
}),
});

shopify.toast.show(
t("ProductsCard.productsCreatedToast", { count: productsCount })
);
} else {
setIsLoading(false);
setToastProps({
content: t("ProductsCard.errorCreatingProductsToast"),
error: true,
shopify.toast.show(t("ProductsCard.errorCreatingProductsToast"), {
isError: true,
});
}

setPopulating(false);
};

return (
<>
{toastMarkup}
<Card
title={t("ProductsCard.title")}
sectioned
primaryFooterAction={{
content: t("ProductsCard.populateProductsButton", {
count: productsCount,
}),
onAction: handlePopulate,
loading: isLoading,
}}
>
<TextContainer spacing="loose">
<p>{t("ProductsCard.description")}</p>
<Text as="h4" variant="headingMd">
{t("ProductsCard.totalProductsHeading")}
<Text variant="bodyMd" as="p" fontWeight="semibold">
{isLoadingCount ? "-" : data.count}
</Text>
<Card
title={t("ProductsCard.title")}
sectioned
primaryFooterAction={{
content: t("ProductsCard.populateProductsButton", {
count: productsCount,
}),
onAction: handlePopulate,
loading: isPopulating,
}}
>
<TextContainer spacing="loose">
<p>{t("ProductsCard.description")}</p>
<Text as="h4" variant="headingMd">
{t("ProductsCard.totalProductsHeading")}
<Text variant="bodyMd" as="p" fontWeight="semibold">
{isLoadingCount ? "-" : data?.count}
</Text>
</TextContainer>
</Card>
</>
</Text>
</TextContainer>
</Card>
);
}
92 changes: 0 additions & 92 deletions components/providers/AppBridgeProvider.jsx

This file was deleted.

6 changes: 1 addition & 5 deletions components/providers/PolarisProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { useCallback } from "react";
import { AppProvider } from "@shopify/polaris";
import { useNavigate } from "@shopify/app-bridge-react";
import "@shopify/polaris/build/esm/styles.css";
import { getPolarisTranslations } from "../../utils/i18nUtils";

function AppBridgeLink({ url, children, external, ...rest }) {
const navigate = useNavigate();
const handleClick = useCallback(() => {
navigate(url);
}, [url]);
const handleClick = useCallback(() => window.open(url), [url]);
Copy link
Member

@henrytao-me henrytao-me May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering we are fixing the template in this PR, can we wait til the back stack fix is out. I would like to bring shopify.navigation.onNavigate to the template to fix back stack as a green path. https://github.com/Shopify/app-bridge-next/pull/345/files#diff-930d0931e69b8c66af5536e0b6fa87053b0e051c7ab29c6cee9abe1f37fb5aa8R25

=> The back stack PRs should be merged this week.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can wait. Will we need to change anything here though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we do. There are two options. I think option 1 is the best for most cases.

  • Option 1: still call navigate(url); like before but need to call extra method like so
  const handleClick = useCallback(async () => {
    await shopify.saveBar.leaveConfirmation();
    navigate(url);
  }, [url]);
  • Option 2: subscribe to shopify.navigation.onNavigate like so
shopify.navigate.onNavigate(() => {
    const {url, intercept} = event;
    // call intercept if the url is a SPA
    intercept({
      async handler() {
        navigate(url);
      },
    });
});
...
const handleClick = useCallback(() => window.open(url), [url]);


const IS_EXTERNAL_LINK_REGEX = /^(?:[a-z][a-z\d+.-]*:|\/\/)/;

Expand Down
1 change: 0 additions & 1 deletion components/providers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { AppBridgeProvider } from "./AppBridgeProvider";
export { QueryProvider } from "./QueryProvider";
export { PolarisProvider } from "./PolarisProvider";
2 changes: 0 additions & 2 deletions hooks/index.js

This file was deleted.

30 changes: 0 additions & 30 deletions hooks/useAppQuery.js

This file was deleted.

42 changes: 0 additions & 42 deletions hooks/useAuthenticatedFetch.js

This file was deleted.

3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
document.getElementsByTagName('head')[0].append(script);
}
</script>

<meta name="shopify-api-key" content="%VITE_SHOPIFY_API_KEY%" />
<script src="https://cdn.shopify.com/shopifycloud/app-bridge.js"></script>
</head>
<body>
<div id="app"><!--index.jsx injects App.jsx here--></div>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@formatjs/intl-localematcher": "^0.4.0",
"@formatjs/intl-pluralrules": "^5.2.4",
"@shopify/app-bridge": "^3.7.7",
"@shopify/app-bridge-react": "^3.7.7",
"@shopify/app-bridge-react": "^4.1.3",
"@shopify/i18next-shopify": "^0.2.9",
"@shopify/polaris": "^10.49.1",
"@vitejs/plugin-react": "4.2.1",
Expand Down
Loading