Skip to content

Commit bddffd6

Browse files
committed
Upgrade dependencies and updates for App Bridge React V4
1 parent ed94770 commit bddffd6

File tree

3 files changed

+42
-57
lines changed

3 files changed

+42
-57
lines changed

sample-apps/discounts/app/routes/app.volume-discount.$functionId.$id.jsx

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { useEffect } from "react";
1+
import { returnToDiscounts } from "~/utils/navigation";
22
import { json } from "@remix-run/node";
33
import { useForm, useField } from "@shopify/react-form";
4-
import { useAppBridge } from "@shopify/app-bridge-react";
5-
import { Redirect } from "@shopify/app-bridge/actions";
64
import { CurrencyCode } from "@shopify/react-i18n";
75
import {
86
Form,
@@ -21,7 +19,6 @@ import {
2119
RequirementType,
2220
SummaryCard,
2321
UsageLimitsCard,
24-
onBreadcrumbAction,
2522
} from "@shopify/discount-app-components";
2623
import {
2724
Banner,
@@ -100,7 +97,7 @@ export const action = async ({ params, request }) => {
10097
],
10198
},
10299
},
103-
}
100+
},
104101
);
105102

106103
const responseJson = await response.json();
@@ -134,7 +131,7 @@ export const action = async ({ params, request }) => {
134131
],
135132
},
136133
},
137-
}
134+
},
138135
);
139136

140137
const responseJson = await response.json();
@@ -199,7 +196,7 @@ export const loader = async ({ params, request }) => {
199196
variables: {
200197
id: `gid://shopify/DiscountNode/${id}`,
201198
},
202-
}
199+
},
203200
);
204201

205202
const responseJson = await response.json();
@@ -225,7 +222,7 @@ export const loader = async ({ params, request }) => {
225222
endsAt,
226223
} = responseJson.data.discountNode.discount;
227224
const configuration = JSON.parse(
228-
responseJson.data.discountNode.configurationField.value
225+
responseJson.data.discountNode.configurationField.value,
229226
);
230227

231228
const discount = {
@@ -252,20 +249,9 @@ export default function VolumeEdit() {
252249
const actionData = useActionData();
253250
const { discount } = useLoaderData();
254251
const navigation = useNavigation();
255-
const app = useAppBridge();
256-
257252
const isLoading = navigation.state === "submitting";
258253
const currencyCode = CurrencyCode.Cad;
259254
const submitErrors = actionData?.errors || [];
260-
const redirect = Redirect.create(app);
261-
262-
useEffect(() => {
263-
if (actionData?.errors.length === 0) {
264-
redirect.dispatch(Redirect.Action.ADMIN_SECTION, {
265-
name: Redirect.ResourceType.Discount,
266-
});
267-
}
268-
}, [actionData]);
269255

270256
if (!discount) {
271257
return <NotFoundPage />;
@@ -349,18 +335,15 @@ export default function VolumeEdit() {
349335

350336
return (
351337
// Render a discount form using Polaris components and the discount app components
352-
<Page
353-
title="Create volume discount"
354-
backAction={{
355-
content: "Discounts",
356-
onAction: () => onBreadcrumbAction(redirect, true),
357-
}}
358-
primaryAction={{
359-
content: "Save",
360-
onAction: submit,
361-
loading: isLoading,
362-
}}
363-
>
338+
<Page>
339+
<ui-title-bar title="Create volume discount">
340+
<button variant="breadcrumb" onClick={returnToDiscounts}>
341+
Discounts
342+
</button>
343+
<button variant="primary" onClick={submit}>
344+
Save discount
345+
</button>
346+
</ui-title-bar>
364347
<Layout>
365348
{errorBanner}
366349
<Layout.Section>
@@ -454,7 +437,7 @@ export default function VolumeEdit() {
454437
secondaryActions={[
455438
{
456439
content: "Discard",
457-
onAction: () => onBreadcrumbAction(redirect, true),
440+
onAction: returnToDiscounts,
458441
},
459442
]}
460443
/>

sample-apps/discounts/app/routes/app.volume-discount.$functionId.new.jsx

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useEffect, useMemo } from "react";
22
import { json } from "@remix-run/node";
33
import { useForm, useField } from "@shopify/react-form";
4-
import { useAppBridge } from "@shopify/app-bridge-react";
5-
import { Redirect } from "@shopify/app-bridge/actions";
64
import { CurrencyCode } from "@shopify/react-i18n";
75
import {
86
Form,
@@ -20,7 +18,6 @@ import {
2018
RequirementType,
2119
SummaryCard,
2220
UsageLimitsCard,
23-
onBreadcrumbAction,
2421
} from "@shopify/discount-app-components";
2522
import {
2623
Banner,
@@ -35,6 +32,7 @@ import {
3532
} from "@shopify/polaris";
3633

3734
import shopify from "../shopify.server";
35+
import { returnToDiscounts } from "~/utils/navigation";
3836

3937
// This is a server-side action that is invoked when the form is submitted.
4038
// It makes an admin GraphQL request to create a discount.
@@ -75,6 +73,9 @@ export const action = async ({ params, request }) => {
7573
`#graphql
7674
mutation CreateCodeDiscount($discount: DiscountCodeAppInput!) {
7775
discountCreate: discountCodeAppCreate(codeAppDiscount: $discount) {
76+
codeAppDiscount{
77+
discountId
78+
}
7879
userErrors {
7980
code
8081
message
@@ -99,17 +100,22 @@ export const action = async ({ params, request }) => {
99100
],
100101
},
101102
},
102-
}
103+
},
103104
);
104105

105106
const responseJson = await response.json();
107+
106108
const errors = responseJson.data.discountCreate?.userErrors;
107-
return json({ errors });
109+
const discount = responseJson.data.discountCreate?.codeAppDiscount;
110+
return json({ errors, discount: { ...discount, functionId } });
108111
} else {
109112
const response = await admin.graphql(
110113
`#graphql
111114
mutation CreateAutomaticDiscount($discount: DiscountAutomaticAppInput!) {
112115
discountCreate: discountAutomaticAppCreate(automaticAppDiscount: $discount) {
116+
automaticAppDiscount {
117+
discountId
118+
}
113119
userErrors {
114120
code
115121
message
@@ -134,7 +140,7 @@ export const action = async ({ params, request }) => {
134140
],
135141
},
136142
},
137-
}
143+
},
138144
);
139145

140146
const responseJson = await response.json();
@@ -148,19 +154,15 @@ export default function VolumeNew() {
148154
const submitForm = useSubmit();
149155
const actionData = useActionData();
150156
const navigation = useNavigation();
151-
const app = useAppBridge();
152157
const todaysDate = useMemo(() => new Date(), []);
153158

154159
const isLoading = navigation.state === "submitting";
155160
const currencyCode = CurrencyCode.Cad;
156161
const submitErrors = actionData?.errors || [];
157-
const redirect = Redirect.create(app);
158162

159163
useEffect(() => {
160-
if (actionData?.errors.length === 0) {
161-
redirect.dispatch(Redirect.Action.ADMIN_SECTION, {
162-
name: Redirect.ResourceType.Discount,
163-
});
164+
if (actionData?.errors.length === 0 && actionData?.discount) {
165+
returnToDiscounts();
164166
}
165167
}, [actionData]);
166168

@@ -244,18 +246,15 @@ export default function VolumeNew() {
244246

245247
return (
246248
// Render a discount form using Polaris components and the discount app components
247-
<Page
248-
title="Create volume discount"
249-
backAction={{
250-
content: "Discounts",
251-
onAction: () => onBreadcrumbAction(redirect, true),
252-
}}
253-
primaryAction={{
254-
content: "Save",
255-
onAction: submit,
256-
loading: isLoading,
257-
}}
258-
>
249+
<Page>
250+
<ui-title-bar title="Create volume discount">
251+
<button variant="breadcrumb" onClick={returnToDiscounts}>
252+
Discounts
253+
</button>
254+
<button variant="primary" onClick={submit}>
255+
Save discount
256+
</button>
257+
</ui-title-bar>
259258
<Layout>
260259
{errorBanner}
261260
<Layout.Section>
@@ -349,7 +348,7 @@ export default function VolumeNew() {
349348
secondaryActions={[
350349
{
351350
content: "Discard",
352-
onAction: () => onBreadcrumbAction(redirect, true),
351+
onAction: returnToDiscounts,
353352
},
354353
]}
355354
/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function returnToDiscounts () {
2+
open("shopify://admin/discounts", "_top")
3+
}

0 commit comments

Comments
 (0)