@@ -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
2425enum 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' )
3133export 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 <for m 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 ( ) ;
0 commit comments