This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
vanilla.template.js
353 lines (295 loc) · 9.66 KB
/
vanilla.template.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import {
BlockStack,
Button,
Card,
Checkbox,
InlineStack,
Text,
TextBlock,
TextField,
extend,
} from '@shopify/admin-ui-extensions';
const translations = {
de: {
hello: 'Guten Tag',
},
en: {
hello: 'Hello',
},
fr: {
hello: 'Bonjour',
},
};
// 'Add' mode should allow a user to add the current product to an existing selling plan
// [Shopify admin renders this mode inside a modal container]
function Add(root, api) {
// Information about the product and/or plan your extension is editing.
// Your extension receives different data in each mode.
const data = api.data;
// Information about the merchant's selected language. Use this to support multiple languages.
const locale = api.locale.initialValue;
// Use locale to set translations with a fallback
const localizedStrings = translations[locale] || translations.en;
// Session token contains information about the current user. Use it to authenticate calls
// from your extension to your app server.
const sessionToken = api.sessionToken;
// The UI your extension renders inside
const {close, done, setPrimaryAction, setSecondaryAction} = api.container;
const mockPlans = [
{name: 'Subscription Plan A', id: 'a'},
{name: 'Subscription Plan B', id: 'b'},
{name: 'Subscription Plan C', id: 'c'},
];
// Configure the extension container UI
setPrimaryAction({
content: 'Add to plan',
onAction: async () => {
// Get a fresh session token before every call to your app server.
const token = await sessionToken.getSessionToken();
// Here, send the form data to your app server to add the product to an existing plan.
// Upon completion, call done() to trigger a reload of the resource page
// and terminate the extension.
done();
},
});
setSecondaryAction({
content: 'Cancel',
onAction: () => close(),
});
const localizedHelloText = root.createComponent(TextBlock, {
size: 'extraLarge',
});
localizedHelloText.appendChild(root.createText(`${localizedStrings.hello}!`));
root.appendChild(localizedHelloText);
const textElement = root.createComponent(Text);
textElement.appendChild(
root.createText(
`Add {Product id ${data.productId}} to an existing plan or existing plans`
)
);
root.appendChild(textElement);
const inlineStack = root.createComponent(InlineStack);
mockPlans.forEach((plan) => {
const checkbox = root.createComponent(Checkbox, {
label: plan.name,
checked: false,
onChange: (checked) => {
checkbox.updateProps({
checked,
});
},
});
inlineStack.appendChild(checkbox);
});
root.appendChild(inlineStack);
root.mount();
}
// 'Create' mode should create a new selling plan, and add the current product to it
// [Shopify admin renders this mode inside an app overlay container]
function Create(root, api) {
const data = api.data;
const locale = api.locale.initialValue;
const sessionToken = api.sessionToken;
const {close, done} = api.container;
const localizedStrings = translations[locale] || translations.en;
const primaryButton = root.createComponent(Button, {
title: 'Create plan',
kind: 'primary',
onPress: async () => {
const token = await sessionToken.getSessionToken();
// Here, send the form data to your app server to create the new plan.
done();
},
});
const secondaryButton = root.createComponent(Button, {
title: 'Cancel',
onPress: () => close(),
});
const textContainerStack = root.createComponent(BlockStack, {
spacing: 'none',
});
root.appendChild(textContainerStack);
const textElement = root.createComponent(TextBlock, {size: 'extraLarge'});
textElement.appendChild(
root.createText(`${localizedStrings.hello}! Create subscription plan`)
);
textContainerStack.appendChild(textElement);
const planTitleCard = root.createComponent(Card, {
sectioned: true,
title: `Create subscription plan for Product id ${data.productId}`,
});
root.appendChild(planTitleCard);
const planTitleField = root.createComponent(TextField, {
label: 'Plan title',
value: '',
onChange(value) {
planTitleField.updateProps({
value,
});
},
});
planTitleCard.appendChild(planTitleField);
const planDetailsCard = root.createComponent(Card, {
sectioned: true,
title: 'Delivery and discount',
});
root.appendChild(planDetailsCard);
const inlineStack = root.createComponent(InlineStack);
planDetailsCard.appendChild(inlineStack);
const deliveryFrequencyField = root.createComponent(TextField, {
type: 'number',
label: 'Delivery frequency (in weeks)',
value: undefined,
onChange(value) {
deliveryFrequencyField.updateProps({
value,
});
},
});
inlineStack.appendChild(deliveryFrequencyField);
const percentageOffField = root.createComponent(TextField, {
type: 'number',
label: 'Percentage off (%)',
value: undefined,
onChange(value) {
percentageOffField.updateProps({
value,
});
},
});
inlineStack.appendChild(percentageOffField);
const actionsElement = root.createComponent(InlineStack, {
inlineAlignment: 'trailing',
});
root.appendChild(actionsElement);
actionsElement.appendChild(secondaryButton);
const primaryButtonStack = root.createComponent(InlineStack, {
inlineAlignment: 'trailing',
});
actionsElement.appendChild(primaryButtonStack);
primaryButtonStack.appendChild(primaryButton);
root.mount();
}
// 'Remove' mode should remove the current product from a selling plan.
// This should not delete the selling plan.
// [Shopify admin renders this mode inside a modal container]
function Remove(root, api) {
const data = api.data;
const locale = api.locale.initialValue;
const sessionToken = api.sessionToken;
const {close, done, setPrimaryAction, setSecondaryAction} = api.container;
const localizedStrings = translations[locale] || translations.en;
setPrimaryAction({
content: 'Remove from plan',
onAction: async () => {
const token = await sessionToken.getSessionToken();
// Here, send the form data to your app server to remove the product from the plan.
done();
},
});
setSecondaryAction({
content: 'Cancel',
onAction: () => close(),
});
const localizedHelloText = root.createComponent(TextBlock, {
size: 'extraLarge',
});
localizedHelloText.appendChild(root.createText(`${localizedStrings.hello}!`));
root.appendChild(localizedHelloText);
const textElement = root.createComponent(Text);
textElement.appendChild(
root.createText(
`Remove {Product id ${data.productId}} from {Plan group id ${data.sellingPlanGroupId}}`
)
);
root.appendChild(textElement);
root.mount();
}
// 'Edit' mode should modify an existing selling plan.
// Changes should affect other products that have this plan applied.
// [Shopify admin renders this mode inside an app overlay container]
function Edit(root, api) {
const data = api.data;
const locale = api.locale.initialValue;
const sessionToken = api.sessionToken;
const {close, done} = api.container;
const localizedStrings = translations[locale] || translations.en;
const primaryButton = root.createComponent(Button, {
title: 'Edit plan',
kind: 'primary',
onPress: async () => {
const token = await sessionToken.getSessionToken();
// Here, send the form data to your app server to modify the selling plan.
done();
},
});
const secondaryButton = root.createComponent(Button, {
title: 'Cancel',
onPress: () => close(),
});
const textContainerStack = root.createComponent(BlockStack, {
spacing: 'none',
});
root.appendChild(textContainerStack);
const textElement = root.createComponent(TextBlock, {size: 'extraLarge'});
textElement.appendChild(
root.createText(`${localizedStrings.hello}! Edit subscription plan`)
);
textContainerStack.appendChild(textElement);
const planTitleCard = root.createComponent(Card, {
sectioned: true,
title: `Edit subscription plan for Product id ${data.productId}`,
});
root.appendChild(planTitleCard);
const planTitleField = root.createComponent(TextField, {
label: 'Plan title',
value: 'Current Plan',
onChange(value) {
planTitleField.updateProps({
value,
});
},
});
planTitleCard.appendChild(planTitleField);
const planDetailsCard = root.createComponent(Card, {
sectioned: true,
title: 'Delivery and discount',
});
root.appendChild(planDetailsCard);
const inlineStack = root.createComponent(InlineStack);
planDetailsCard.appendChild(inlineStack);
const deliveryFrequencyField = root.createComponent(TextField, {
type: 'number',
label: 'Delivery frequency (in weeks)',
value: '1',
onChange(value) {
deliveryFrequencyField.updateProps({
value,
});
},
});
inlineStack.appendChild(deliveryFrequencyField);
const percentageOffField = root.createComponent(TextField, {
type: 'number',
label: 'Percentage off (%)',
value: '10',
onChange(value) {
percentageOffField.updateProps({
value,
});
},
});
inlineStack.appendChild(percentageOffField);
const actionsElement = root.createComponent(InlineStack, {
inlineAlignment: 'trailing',
});
root.appendChild(actionsElement);
actionsElement.appendChild(secondaryButton);
actionsElement.appendChild(primaryButton);
root.mount();
}
// Your extension must render all four modes
extend('Admin::Product::SubscriptionPlan::Add', Add);
extend('Admin::Product::SubscriptionPlan::Create', Create);
extend('Admin::Product::SubscriptionPlan::Remove', Remove);
extend('Admin::Product::SubscriptionPlan::Edit', Edit);