Skip to content

Commit ac10a7f

Browse files
committed
Add min price validation
1 parent de07b52 commit ac10a7f

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/lib/components/inventory/list_item_modal.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export class ListItemModal extends FloatElement {
5858

5959
private readonly MAX_PRICE_CENTS = 100000 * 100; // $100,000
6060

61+
private readonly MIN_PRICE_CENTS = 3; // $0.03
62+
6163
private readonly SALES_FEE_PERCENTAGE = 0.02;
6264

6365
private readonly DURATION_OPTIONS = [
@@ -150,8 +152,11 @@ export class ListItemModal extends FloatElement {
150152
}
151153

152154
private validatePrice(price: number | undefined): {isValid: boolean; error?: {message: string}} {
153-
if (!price || isNaN(price) || price <= 0) {
154-
return {isValid: false, error: {message: 'Please enter a valid price greater than $0.00'}};
155+
if (!price || isNaN(price) || price < this.MIN_PRICE_CENTS) {
156+
return {
157+
isValid: false,
158+
error: {message: `Please enter a valid price of at least $${(this.MIN_PRICE_CENTS / 100).toFixed(2)}`},
159+
};
155160
}
156161

157162
if (price > this.MAX_PRICE_CENTS) {
@@ -209,12 +214,23 @@ export class ListItemModal extends FloatElement {
209214

210215
// Convert to cents for storage
211216
const dollars = parseFloat(value || '0');
212-
if (dollars * 100 > this.MAX_PRICE_CENTS) {
217+
const cents = Math.ceil(dollars * 100);
218+
219+
// Validate the price
220+
if (cents < this.MIN_PRICE_CENTS) {
221+
this.error = {
222+
message: `Please enter a valid price of at least $${(this.MIN_PRICE_CENTS / 100).toFixed(2)}`,
223+
};
224+
this.customPrice = undefined;
225+
} else if (cents > this.MAX_PRICE_CENTS) {
226+
this.error = {
227+
message: 'Price cannot exceed $100,000 USD',
228+
};
213229
input.value = (this.MAX_PRICE_CENTS / 100).toString();
214-
this.updatePrice(this.MAX_PRICE_CENTS);
230+
this.customPrice = this.MAX_PRICE_CENTS;
215231
} else {
216-
const cents = Math.ceil(dollars * 100);
217-
this.updatePrice(Math.max(1, cents));
232+
this.error = undefined;
233+
this.customPrice = cents;
218234
}
219235
}
220236

@@ -232,7 +248,22 @@ export class ListItemModal extends FloatElement {
232248
if (this.recommendedPrice) {
233249
const exactPrice = (value / 100) * this.recommendedPrice;
234250
const newPrice = Math.ceil(exactPrice);
235-
this.updatePrice(newPrice);
251+
252+
// Validate the price
253+
if (newPrice < this.MIN_PRICE_CENTS) {
254+
this.error = {
255+
message: `Please enter a valid price of at least $${(this.MIN_PRICE_CENTS / 100).toFixed(2)}`,
256+
};
257+
this.customPrice = undefined;
258+
} else if (newPrice > this.MAX_PRICE_CENTS) {
259+
this.error = {
260+
message: 'Price cannot exceed $100,000 USD',
261+
};
262+
this.customPrice = this.MAX_PRICE_CENTS;
263+
} else {
264+
this.error = undefined;
265+
this.customPrice = newPrice;
266+
}
236267
}
237268
}
238269

0 commit comments

Comments
 (0)