/
install.tsx
171 lines (159 loc) · 4.94 KB
/
install.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React from "react";
import Head from "next/head";
import prismatic from "@prismatic-io/embedded";
import { useRouter } from "next/router";
import usePrismaticAuth from "@/usePrismaticAuth";
const steps = {
initializing: "Initializing",
queryForInstances: "Querying for instances",
creatingInstance: "Creating instance",
updatingInstance: "Updating instance config variables",
redirectingToShopify: "Redirecting to Shopify",
};
type Steps = keyof typeof steps;
function InstallShopify() {
const router = useRouter();
const { authenticated, token, userinfo } = usePrismaticAuth();
const [step, setStep] = React.useState<Steps>("initializing");
const { shop } = router.query;
React.useEffect(() => {
/**
* Create a new instance (if needed), configure the instance given the `shop`
* query parameter, and redirect the user to the Shopify OAuth page.
*/
const redirectToShopifyAuth = async () => {
setStep("queryForInstances");
const getIntegrationResult = await prismatic.graphqlRequest({
query: `query getShopifyInstances {
marketplaceIntegrations(
name: "Shopify"
) {
nodes {
id
instances {
nodes {
id
}
}
}
}
}`,
});
const { nodes: instances } =
getIntegrationResult.data.marketplaceIntegrations.nodes[0].instances;
let instanceId = "";
if (instances.length === 0) {
setStep("creatingInstance");
// We need to create a new Shopify instance
const createInstanceResult = await prismatic.graphqlRequest({
query: `mutation createShopifyInstance(
$integrationId: ID!
$customerId: ID!
$instanceName: String!
) {
createInstance(
input: {
integration: $integrationId
customer: $customerId
name: $instanceName
}
) {
instance {
id
}
}
}`,
variables: {
integrationId:
getIntegrationResult.data.marketplaceIntegrations.nodes[0].id,
customerId: userinfo.authenticatedUser.customer.id,
instanceName: "Shopify",
},
});
instanceId = createInstanceResult.data.createInstance.instance.id;
} else {
// We have an existing Shopify instance
instanceId = instances[0].id;
}
// Configure the instance
setStep("updatingInstance");
const updateInstanceResult = await prismatic.graphqlRequest({
query: `mutation ($instanceId: ID!, $configVariables: [InputInstanceConfigVariable]) {
updateInstanceConfigVariables(
input: { id: $instanceId, configVariables: $configVariables }
) {
instance {
id
configVariables {
nodes {
requiredConfigVariable {
key
}
authorizeUrl
}
}
}
errors {
field
messages
}
}
}`,
variables: {
instanceId,
configVariables: [
{
key: "Shopify Connection",
values: JSON.stringify([
{ name: "host", type: "value", value: shop },
{
name: "tokenUrl",
type: "value",
value: `https://${shop}/admin/oauth/access_token`,
},
{
name: "authorizeUrl",
type: "value",
value: `https://${shop}/admin/oauth/authorize`,
},
]),
},
],
},
});
const { errors, instance } =
updateInstanceResult.data.updateInstanceConfigVariables;
if (errors.length > 0) {
console.error(errors);
}
const configVariables = instance.configVariables.nodes;
setStep("redirectingToShopify");
// Redirect to the instance's Shopify Connection authorize URL
window.location = configVariables.find(
(configVariable) =>
configVariable.requiredConfigVariable.key === "Shopify Connection",
).authorizeUrl;
};
if (authenticated && token && userinfo && shop) {
redirectToShopifyAuth();
}
}, [authenticated, token, userinfo?.authenticatedUser.customer.id, shop]);
return (
<>
<Head>
<title>Shopify Redirect</title>
</Head>
<div
style={{
height: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<div style={{ fontSize: "5em" }}>{steps[step]}</div>
</div>
</>
);
}
export default InstallShopify;