Skip to content

Commit 886a551

Browse files
committed
updates
1 parent fab16b2 commit 886a551

File tree

3 files changed

+90
-96
lines changed

3 files changed

+90
-96
lines changed

components/snipcart/actions/create-discount/create-discount.mjs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ export default {
1414
"name",
1515
],
1616
},
17-
maxNumberOfUsages: {
18-
propDefinition: [
19-
app,
20-
"maxNumberOfUsages",
21-
],
22-
},
2317
trigger: {
2418
propDefinition: [
2519
app,
@@ -32,15 +26,15 @@ export default {
3226
app,
3327
"code",
3428
],
35-
disabled: true,
29+
optional: true,
3630
hidden: true,
3731
},
3832
totalToReach: {
3933
propDefinition: [
4034
app,
4135
"totalToReach",
4236
],
43-
disabled: true,
37+
optional: true,
4438
hidden: true,
4539
},
4640
type: {
@@ -55,37 +49,42 @@ export default {
5549
app,
5650
"amount",
5751
],
58-
disabled: true,
52+
optional: true,
5953
hidden: true,
6054
},
6155
rate: {
6256
propDefinition: [
6357
app,
6458
"rate",
6559
],
66-
disabled: true,
60+
optional: true,
6761
hidden: true,
6862
},
63+
maxNumberOfUsages: {
64+
propDefinition: [
65+
app,
66+
"maxNumberOfUsages",
67+
],
68+
},
6969
},
70-
async additionalProps(existingProps) {
71-
const props = {};
72-
if (this.trigger === "Code") {
73-
existingProps.code.hidden = false;
74-
existingProps.code.disabled: false,
75-
}
76-
if (this.trigger === "Total") {
77-
existingProps.totalToReach.hidden = false;
78-
existingProps.totalToReach.disabled = false;
79-
}
80-
if (this.type === "FixedAmount") {
81-
existingProps.amount.hidden = false;
82-
existingProps.amount.disabled = false;
83-
}
84-
if (this.type === "Rate") {
85-
existingProps.rate.hidden = false;
86-
existingProps.rate.disabled = false;
87-
}
88-
return props;
70+
async additionalProps(props) {
71+
const triggerIsCode = this.trigger === "Code";
72+
const triggerIsTotal = this.trigger === "Total";
73+
74+
props.code.hidden = !triggerIsCode;
75+
props.code.optional = !triggerIsCode;
76+
props.totalToReach.hidden = !triggerIsTotal;
77+
props.totalToReach.optional = !triggerIsTotal;
78+
79+
const typeIsFixedAmount = this.type === "FixedAmount";
80+
const typeIsRate = this.type === "Rate";
81+
82+
props.amount.hidden = !typeIsFixedAmount;
83+
props.amount.optional = !typeIsFixedAmount;
84+
props.rate.hidden = !typeIsRate;
85+
props.rate.optional = !typeIsRate;
86+
87+
return {};
8988
},
9089
async run({ $ }) {
9190
const response = await this.app.createDiscount({
@@ -99,7 +98,6 @@ export default {
9998
type: this.type,
10099
amount: this.amount,
101100
rate: this.rate,
102-
discount: this.discount,
103101
},
104102
});
105103

components/snipcart/actions/update-discount/update-discount.mjs

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,106 +19,99 @@ export default {
1919
app,
2020
"name",
2121
],
22-
},
23-
maxNumberOfUsages: {
24-
propDefinition: [
25-
app,
26-
"maxNumberOfUsages",
27-
],
22+
optional: true,
2823
},
2924
trigger: {
3025
propDefinition: [
3126
app,
3227
"trigger",
3328
],
29+
optional: true,
3430
reloadProps: true,
3531
},
3632
code: {
3733
propDefinition: [
3834
app,
3935
"code",
4036
],
41-
disabled: true,
37+
optional: true,
4238
hidden: true,
4339
},
4440
totalToReach: {
4541
propDefinition: [
4642
app,
4743
"totalToReach",
4844
],
49-
disabled: true,
45+
optional: true,
5046
hidden: true,
5147
},
5248
type: {
5349
propDefinition: [
5450
app,
5551
"type",
5652
],
53+
optional: true,
5754
reloadProps: true,
5855
},
5956
amount: {
6057
propDefinition: [
6158
app,
6259
"amount",
6360
],
64-
disabled: true,
61+
optional: true,
6562
hidden: true,
6663
},
6764
rate: {
6865
propDefinition: [
6966
app,
7067
"rate",
7168
],
72-
disabled: true,
69+
optional: true,
7370
hidden: true,
7471
},
72+
maxNumberOfUsages: {
73+
propDefinition: [
74+
app,
75+
"maxNumberOfUsages",
76+
],
77+
},
7578
},
76-
async additionalProps() {
77-
const props = {};
78-
if (this.trigger === "Code") {
79-
props.code = {
80-
type: "string",
81-
label: "Code",
82-
description: "Code for the discount",
83-
};
84-
}
85-
if (this.trigger === "Total") {
86-
props.totalToReach = {
87-
type: "string",
88-
label: "Total to Reach",
89-
description: "Minimum amount required to activate the discount",
90-
};
91-
}
92-
if (this.type === "FixedAmount") {
93-
props.amount = {
94-
type: "string",
95-
label: "Amount",
96-
description: "Discount amount. Required when discount type is `FixedAmount`",
97-
};
98-
}
99-
if (this.type === "Rate") {
100-
props.rate = {
101-
type: "string",
102-
label: "Rate",
103-
description: "Discount percentage, i.e.: `10`. Required when discount type is `Rate`",
104-
};
105-
}
106-
return props;
79+
async additionalProps(props) {
80+
const triggerIsCode = this.trigger === "Code";
81+
const triggerIsTotal = this.trigger === "Total";
82+
83+
props.code.hidden = !triggerIsCode;
84+
props.code.optional = !triggerIsCode;
85+
props.totalToReach.hidden = !triggerIsTotal;
86+
props.totalToReach.optional = !triggerIsTotal;
87+
88+
const typeIsFixedAmount = this.type === "FixedAmount";
89+
const typeIsRate = this.type === "Rate";
90+
91+
props.amount.hidden = !typeIsFixedAmount;
92+
props.amount.optional = !typeIsFixedAmount;
93+
props.rate.hidden = !typeIsRate;
94+
props.rate.optional = !typeIsRate;
95+
96+
return {};
10797
},
10898
async run({ $ }) {
99+
const discount = await this.app.getDiscount({
100+
$,
101+
id: this.discountId,
102+
});
109103
const response = await this.app.updateDiscount({
110104
$,
111105
id: this.discountId,
112106
data: {
113-
name: this.name,
114-
maxNumberOfUsages: this.maxNumberOfUsages,
115-
trigger: this.trigger,
116-
code: this.code,
117-
totalToReach: this.totalToReach,
118-
type: this.type,
119-
amount: this.amount,
120-
rate: this.rate,
121-
discount: this.discount,
107+
name: this.name || discount.name,
108+
maxNumberOfUsages: this.maxNumberOfUsages || discount.maxNumberOfUsages,
109+
trigger: this.trigger || discount.trigger,
110+
code: this.code || discount.code,
111+
totalToReach: this.totalToReach || discount.totalToReach,
112+
type: this.type || discount.type,
113+
amount: this.amount || discount.amount,
114+
rate: this.rate || discount.rate,
122115
},
123116
});
124117

components/snipcart/snipcart.app.mjs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ export default {
2626
description: "Name of the discount",
2727
},
2828
maxNumberOfUsages: {
29-
type: "string",
29+
type: "integer",
3030
label: "Max Number of Usages",
31-
description: "Maximum number of times the discount can be used",
31+
description: "The maximum number of usage for the discount. If not specified, customers will be able to use this discount indefinitely.",
32+
optional: true,
3233
},
3334
trigger: {
3435
type: "string",
3536
label: "Trigger",
36-
description: "Trigger condition for the discount",
37+
description: "Condition that will trigger the discount",
3738
options: constants.TRIGGER_OPTIONS,
3839
},
3940
code: {
@@ -49,7 +50,7 @@ export default {
4950
type: {
5051
type: "string",
5152
label: "Type",
52-
description: "The type of action that the discount will apply",
53+
description: "The type of action that the discount will apply to",
5354
options: constants.TYPE_OPTIONS,
5455
},
5556
amount: {
@@ -67,29 +68,37 @@ export default {
6768
_baseUrl() {
6869
return "https://app.snipcart.com/api";
6970
},
70-
async _makeRequest(opts = {}) {
71+
_makeRequest(opts = {}) {
7172
const {
7273
$ = this,
7374
path,
7475
...otherOpts
7576
} = opts;
7677
return axios($, {
7778
...otherOpts,
78-
url: this._baseUrl() + path,
79+
url: `${this._baseUrl()}${path}`,
7980
auth: {
8081
username: `${this.$auth.api_key}`,
8182
password: "",
8283
},
8384
});
8485
},
85-
async createDiscount(args = {}) {
86+
getDiscount({
87+
id, ...args
88+
}) {
89+
return this._makeRequest({
90+
path: `/discounts/${id}`,
91+
...args,
92+
});
93+
},
94+
createDiscount(args = {}) {
8695
return this._makeRequest({
8796
method: "post",
8897
path: "/discounts",
8998
...args,
9099
});
91100
},
92-
async updateDiscount({
101+
updateDiscount({
93102
id, ...args
94103
}) {
95104
return this._makeRequest({
@@ -98,7 +107,7 @@ export default {
98107
...args,
99108
});
100109
},
101-
async deleteDiscount({
110+
deleteDiscount({
102111
id, ...args
103112
}) {
104113
return this._makeRequest({
@@ -107,17 +116,11 @@ export default {
107116
...args,
108117
});
109118
},
110-
async listDiscounts(args = {}) {
119+
listDiscounts(args = {}) {
111120
return this._makeRequest({
112121
path: "/discounts",
113122
...args,
114123
});
115124
},
116-
async listProducts(args = {}) {
117-
return this._makeRequest({
118-
path: "/products",
119-
...args,
120-
});
121-
},
122125
},
123126
};

0 commit comments

Comments
 (0)