Skip to content

Commit 20d4cad

Browse files
authored
date input pre-fills with next billing date & cannot press go if the input has value (#24)
1 parent 748708a commit 20d4cad

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

src/form-sections/date.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ type InvalidDateErrorCode =
1919
| 'date_too_early'
2020
| 'second_donation_this_month'
2121
| 'date_out_of_range'
22+
| 'same_next_billing_date'
2223
| '';
2324

2425
enum InvalidDateErrorMessages {
2526
invalid_date = 'Please enter a valid date format (YYYY-MM-DD)',
2627
date_too_early = 'Date must be at least tomorrow.',
2728
second_donation_this_month = 'The date you selected will result in an additional donation for this month.',
2829
date_out_of_range = 'New donation date must be within the next 12 months.',
30+
same_next_billing_date = '',
2931
}
3032
@customElement('ia-mgc-edit-date')
3133
export class MGCEditPlanDate extends LitElement {
@@ -141,6 +143,13 @@ export class MGCEditPlanDate extends LitElement {
141143
this.newDate = undefined;
142144
}
143145

146+
formatDateToYYYYMMDD(date: Date): string {
147+
const year = date.getFullYear();
148+
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
149+
const dateNum = String(date.getDate()).padStart(2, '0');
150+
return `${year}-${month}-${dateNum}`;
151+
}
152+
144153
async clearStatusMessaging() {
145154
this.errorMessage = '';
146155
this.warningMessage = '';
@@ -180,6 +189,18 @@ export class MGCEditPlanDate extends LitElement {
180189
const chosenDate = new Date(validDate);
181190
chosenDate.setHours(0, 0, 0, 0);
182191

192+
// check input value against next billing date
193+
const isNextBillingDate = this.plan?.nextBillingDate
194+
? this.formatDateToYYYYMMDD(new Date(this.plan.nextBillingDate)) ===
195+
this.formatDateToYYYYMMDD(chosenDate)
196+
: false;
197+
if (isNextBillingDate) {
198+
return {
199+
valid: false,
200+
errorCode: 'same_next_billing_date',
201+
};
202+
}
203+
183204
if (chosenDate < today) {
184205
return {
185206
valid: false,
@@ -246,6 +267,10 @@ export class MGCEditPlanDate extends LitElement {
246267
}
247268

248269
get editDateForm(): TemplateResult {
270+
const nextBillingDateYYYYMMDD = this.plan?.nextBillingDate
271+
? this.formatDateToYYYYMMDD(new Date(this.plan.nextBillingDate))
272+
: '';
273+
const inputValueToUse = this.dateInput?.value ?? nextBillingDateYYYYMMDD;
249274
return html`
250275
<section>
251276
<form id="edit-date">
@@ -264,7 +289,7 @@ export class MGCEditPlanDate extends LitElement {
264289
name="edit-date"
265290
min=${this.minDate}
266291
max=${this.maxDate}
267-
value=""
292+
.value=${inputValueToUse}
268293
@focus=${() => this.clearStatusMessaging()}
269294
@change=${async () => {
270295
this.clearStatusMessaging();

src/models/plan.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ export class MonthlyPlan {
6565
this.plan.amount = newAmount;
6666
}
6767

68-
setNextBillingDate(newDate: string): void {
69-
this.payment.nextBillingDate.oldDate = this.payment.nextBillingDate.date;
70-
this.payment.nextBillingDate.date = newDate;
68+
get nextBillingDate(): string {
69+
// iso08601 date string
70+
return this.payment.nextBillingDate.date;
7171
}
7272

73-
get startDate(): string {
74-
const date = new Date(this.plan.start_date);
75-
return date.toLocaleDateString();
73+
setNextBillingDate(newDate: string) {
74+
// iso08601 date string
75+
this.payment.nextBillingDate.oldDate = this.payment.nextBillingDate.date;
76+
this.payment.nextBillingDate.date = newDate;
7677
}
7778

7879
get nextBillingDateLocale(): string {

test/form-sections/date.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ const plan = new MonthlyPlan({
6666
},
6767
});
6868

69+
const nextBillingDateYYYYMMDD = `${nextPaymentFirstOfMonth.getFullYear()}-${String(
70+
nextPaymentFirstOfMonth.getMonth() + 1
71+
).padStart(2, '0')}-${String(nextPaymentFirstOfMonth.getDate()).padStart(
72+
2,
73+
'0'
74+
)}`;
75+
6976
describe('<ia-mgc-edit-date>', () => {
7077
it('input validates before allowing form submission', async () => {
7178
const el = await fixture<MGCEditPlanDate>(
@@ -130,10 +137,11 @@ describe('<ia-mgc-edit-date>', () => {
130137
'0'
131138
)}`
132139
);
140+
expect(dateInput!.value).to.equal(nextBillingDateYYYYMMDD);
133141

134142
// checks if input value is invalid
135143
// date before today
136-
dateInput!.setAttribute('value', '2008-11-11');
144+
dateInput!.value = '2008-11-11';
137145
dateInput!.dispatchEvent(new Event('change'));
138146
await el.updateComplete;
139147

@@ -144,7 +152,7 @@ describe('<ia-mgc-edit-date>', () => {
144152
const twoYearsFromNowStr = `${twoYearsFromNow.getFullYear()}-${String(
145153
twoYearsFromNow.getMonth() + 1
146154
).padStart(2, '0')}-${String(twoYearsFromNow.getDate()).padStart(2, '0')}`;
147-
dateInput!.setAttribute('value', twoYearsFromNowStr);
155+
dateInput!.value = twoYearsFromNowStr;
148156
dateInput!.dispatchEvent(new Event('change'));
149157
await el.updateComplete;
150158

@@ -162,7 +170,7 @@ describe('<ia-mgc-edit-date>', () => {
162170
const lastDayOfCurrentMonthStr = `${lastDayOfCurrentMonth.getFullYear()}-${String(
163171
lastDayOfCurrentMonth.getMonth() + 1
164172
).padStart(2, '0')}-${lastDayOfCurrentMonth.getDate()}`;
165-
dateInput!.setAttribute('value', lastDayOfCurrentMonthStr);
173+
dateInput!.value = lastDayOfCurrentMonthStr;
166174
dateInput!.dispatchEvent(new Event('change'));
167175
await el.updateComplete;
168176

@@ -187,7 +195,7 @@ describe('<ia-mgc-edit-date>', () => {
187195
const nextMonthStr = `${nextMonth.getFullYear()}-${String(
188196
nextMonth.getMonth() + 1
189197
).padStart(2, '0')}-${String(nextMonth.getDate()).padStart(2, '0')}`;
190-
dateInput!.setAttribute('value', nextMonthStr);
198+
dateInput!.value = nextMonthStr;
191199
dateInput!.dispatchEvent(new Event('change'));
192200

193201
await el.updateComplete;

0 commit comments

Comments
 (0)