|
1 | 1 | import { useState } from "react"; |
2 | 2 | import { Card, TextContainer, Text } from "@shopify/polaris"; |
3 | | -import { Toast } from "@shopify/app-bridge-react"; |
| 3 | +import { useAppBridge } from "@shopify/app-bridge-react"; |
4 | 4 | import { useTranslation } from "react-i18next"; |
5 | | -import { useAppQuery, useAuthenticatedFetch } from "../hooks"; |
| 5 | +import { useQuery } from "react-query"; |
6 | 6 |
|
7 | 7 | export function ProductsCard() { |
8 | | - const emptyToastProps = { content: null }; |
9 | | - const [isLoading, setIsLoading] = useState(true); |
10 | | - const [toastProps, setToastProps] = useState(emptyToastProps); |
11 | | - const fetch = useAuthenticatedFetch(); |
| 8 | + const shopify = useAppBridge(); |
12 | 9 | const { t } = useTranslation(); |
| 10 | + const [isPopulating, setIsPopulating] = useState(false); |
13 | 11 | const productsCount = 5; |
14 | 12 |
|
15 | 13 | const { |
16 | 14 | data, |
17 | 15 | refetch: refetchProductCount, |
18 | 16 | isLoading: isLoadingCount, |
19 | | - isRefetching: isRefetchingCount, |
20 | | - } = useAppQuery({ |
21 | | - url: "/api/products/count", |
22 | | - reactQueryOptions: { |
23 | | - onSuccess: () => { |
24 | | - setIsLoading(false); |
25 | | - }, |
| 17 | + } = useQuery({ |
| 18 | + queryKey: ["productCount"], |
| 19 | + queryFn: async () => { |
| 20 | + const response = await fetch("/api/products/count"); |
| 21 | + return await response.json(); |
26 | 22 | }, |
| 23 | + refetchOnWindowFocus: false, |
27 | 24 | }); |
28 | 25 |
|
29 | | - const toastMarkup = toastProps.content && !isRefetchingCount && ( |
30 | | - <Toast {...toastProps} onDismiss={() => setToastProps(emptyToastProps)} /> |
31 | | - ); |
| 26 | + const setPopulating = (flag) => { |
| 27 | + shopify.loading(flag); |
| 28 | + setIsPopulating(flag); |
| 29 | + }; |
32 | 30 |
|
33 | 31 | const handlePopulate = async () => { |
34 | | - setIsLoading(true); |
35 | | - const response = await fetch("/api/products", {method: "POST"}); |
| 32 | + setPopulating(true); |
| 33 | + const response = await fetch("/api/products", { method: "POST" }); |
36 | 34 |
|
37 | 35 | if (response.ok) { |
38 | 36 | await refetchProductCount(); |
39 | | - setToastProps({ |
40 | | - content: t("ProductsCard.productsCreatedToast", { |
41 | | - count: productsCount, |
42 | | - }), |
43 | | - }); |
| 37 | + |
| 38 | + shopify.toast.show( |
| 39 | + t("ProductsCard.productsCreatedToast", { count: productsCount }) |
| 40 | + ); |
44 | 41 | } else { |
45 | | - setIsLoading(false); |
46 | | - setToastProps({ |
47 | | - content: t("ProductsCard.errorCreatingProductsToast"), |
48 | | - error: true, |
| 42 | + shopify.toast.show(t("ProductsCard.errorCreatingProductsToast"), { |
| 43 | + isError: true, |
49 | 44 | }); |
50 | 45 | } |
| 46 | + |
| 47 | + setPopulating(false); |
51 | 48 | }; |
52 | 49 |
|
53 | 50 | return ( |
54 | | - <> |
55 | | - {toastMarkup} |
56 | | - <Card |
57 | | - title={t("ProductsCard.title")} |
58 | | - sectioned |
59 | | - primaryFooterAction={{ |
60 | | - content: t("ProductsCard.populateProductsButton", { |
61 | | - count: productsCount, |
62 | | - }), |
63 | | - onAction: handlePopulate, |
64 | | - loading: isLoading, |
65 | | - }} |
66 | | - > |
67 | | - <TextContainer spacing="loose"> |
68 | | - <p>{t("ProductsCard.description")}</p> |
69 | | - <Text as="h4" variant="headingMd"> |
70 | | - {t("ProductsCard.totalProductsHeading")} |
71 | | - <Text variant="bodyMd" as="p" fontWeight="semibold"> |
72 | | - {isLoadingCount ? "-" : data.count} |
73 | | - </Text> |
| 51 | + <Card |
| 52 | + title={t("ProductsCard.title")} |
| 53 | + sectioned |
| 54 | + primaryFooterAction={{ |
| 55 | + content: t("ProductsCard.populateProductsButton", { |
| 56 | + count: productsCount, |
| 57 | + }), |
| 58 | + onAction: handlePopulate, |
| 59 | + loading: isPopulating, |
| 60 | + }} |
| 61 | + > |
| 62 | + <TextContainer spacing="loose"> |
| 63 | + <p>{t("ProductsCard.description")}</p> |
| 64 | + <Text as="h4" variant="headingMd"> |
| 65 | + {t("ProductsCard.totalProductsHeading")} |
| 66 | + <Text variant="bodyMd" as="p" fontWeight="semibold"> |
| 67 | + {isLoadingCount ? "-" : data?.count} |
74 | 68 | </Text> |
75 | | - </TextContainer> |
76 | | - </Card> |
77 | | - </> |
| 69 | + </Text> |
| 70 | + </TextContainer> |
| 71 | + </Card> |
78 | 72 | ); |
79 | 73 | } |
0 commit comments