Skip to content

Commit d0b4f75

Browse files
authored
Merge pull request #31 from internetarchive/webdev-7672-mgc-amount-form-fix
Edit amount form should submit with ENTER button as well
2 parents aa4bd17 + 3ddb625 commit d0b4f75

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

src/form-sections/amount.ts

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,50 @@ export class MGCEditPlanAmount extends LitElement {
187187
this.donationPaymentInfo = undefined;
188188
}
189189

190+
handleSubmit(e: Event, iaButton?: MGCButton) {
191+
e.preventDefault();
192+
this.clearStatusMessaging();
193+
194+
const button =
195+
iaButton ?? (this.form.querySelector('#update-amount') as MGCButton);
196+
if (button) this.updateButtonState(button);
197+
198+
if (!this.newAmount) {
199+
this.errorMessage = 'Please enter a new amount';
200+
if (button) button.isDisabled = false;
201+
return;
202+
}
203+
204+
const input = this.form.querySelector(
205+
'input[name="amount"]'
206+
) as HTMLInputElement;
207+
const requestedAmount = Number(input.value) ?? 0;
208+
const amountTooLow = requestedAmount < 1;
209+
const amountTooHigh = requestedAmount >= 9999;
210+
211+
if (amountTooLow) {
212+
this.errorMessage = 'Please enter a valid amount';
213+
}
214+
215+
if (amountTooHigh) {
216+
this.errorMessage =
217+
'Amount must be less than $9,999. Would you like to donate more? Please contact us at [email protected]';
218+
}
219+
220+
if (amountTooHigh || amountTooLow) {
221+
if (button) this.updateButtonState(button);
222+
return;
223+
}
224+
225+
this.requestAmountUpdate(e);
226+
}
227+
228+
private async updateButtonState(iaButton: MGCButton) {
229+
// eslint-disable-next-line no-param-reassign
230+
(iaButton as MGCButton).isDisabled = true;
231+
await iaButton?.updateComplete;
232+
}
233+
190234
async clearStatusMessaging() {
191235
this.errorMessage = '';
192236
this.updateMessage = '';
@@ -210,7 +254,10 @@ export class MGCEditPlanAmount extends LitElement {
210254
get editAmountForm(): TemplateResult {
211255
return html`
212256
<section>
213-
<form id="edit-plan-amount">
257+
<form
258+
id="edit-plan-amount"
259+
@submit=${(e: Event) => this.handleSubmit(e)}
260+
>
214261
<p>Current donation amount: $${this.plan?.amount}</p>
215262
<div>
216263
$
@@ -220,7 +267,7 @@ export class MGCEditPlanAmount extends LitElement {
220267
type="number"
221268
id="amount"
222269
name="amount"
223-
required="true"
270+
?required=${true}
224271
@focus=${() => this.clearStatusMessaging()}
225272
@input=${(e: Event) => {
226273
const newAmount = Number((e.target as HTMLInputElement).value);
@@ -260,41 +307,8 @@ export class MGCEditPlanAmount extends LitElement {
260307
id="update-amount"
261308
class="ia-button primary"
262309
type="submit"
263-
.clickHandler=${async (e: Event, iaButton: MGCButton) => {
264-
this.clearStatusMessaging();
265-
266-
if (!this.newAmount) {
267-
this.errorMessage = 'Please enter a new amount';
268-
return;
269-
}
270-
271-
// eslint-disable-next-line no-param-reassign
272-
(iaButton as MGCButton).isDisabled = true;
273-
await iaButton.updateComplete;
274-
275-
// check if request is in range
276-
const input = this.form.querySelector(
277-
'input[name="amount"]'
278-
) as HTMLInputElement;
279-
const requestedAmount = Number(input.value) ?? 0;
280-
const amountTooLow = requestedAmount < 1;
281-
const amountTooHigh = requestedAmount >= 9999;
282-
if (amountTooLow) {
283-
this.errorMessage = 'Please enter a valid amount';
284-
}
285-
if (amountTooHigh) {
286-
this.errorMessage =
287-
'Amount must be less than $9,999. Would you like to donate more? Please contact us at [email protected]';
288-
}
289-
if (amountTooHigh || amountTooLow) {
290-
// eslint-disable-next-line no-param-reassign
291-
(iaButton as MGCButton).isDisabled = false;
292-
await iaButton.updateComplete;
293-
return;
294-
}
295-
296-
this.requestAmountUpdate(e);
297-
}}
310+
.clickHandler=${(e: Event, iaButton: MGCButton) =>
311+
this.handleSubmit(e, iaButton)}
298312
>
299313
Update
300314
</ia-mgc-button>

0 commit comments

Comments
 (0)