Skip to content

Commit

Permalink
fix: ensure number of days is always calculated properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ublefo committed May 2, 2024
1 parent 79a6137 commit c3cb961
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/app/common/modals/extension-modal/extension-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {TaskComment, TaskCommentService, Task} from 'src/app/api/models/doubtfir
import {AppInjector} from 'src/app/app-injector';
import {FormControl, Validators, FormGroup, FormGroupDirective, NgForm} from '@angular/forms';
import {MatDatepickerInputEvent} from '@angular/material/datepicker';
import {differenceInWeeks, differenceInDays, isAfter, addDays} from 'date-fns';
import {differenceInDays, isPast, addDays, startOfDay} from 'date-fns';
import {ErrorStateMatcher} from '@angular/material/core';

/** Error when invalid control is dirty, touched, or submitted. */
Expand Down Expand Up @@ -41,20 +41,19 @@ export class ExtensionModalComponent {

get extensionDuration(): number {
// calculating the number of days between now and the requested date
return differenceInDays(this.extensionDate, this.data.task.localDueDate()) + 1;
// use start of day to get correct number of days
return differenceInDays(
startOfDay(this.extensionDate),
startOfDay(this.data.task.localDueDate()),
);
}

// minimum date is due date if before target date, current date if after target date
// minimum date is the next day of due date if before target date, next day of current date if after target date
minDate = new Date(
addDays(
isAfter(Date.now(), this.data.task.localDueDate())
? new Date()
: this.data.task.localDueDate(),
1,
),
addDays(isPast(this.data.task.localDueDate()) ? new Date() : this.data.task.localDueDate(), 1),
);
maxDate = this.data.task.localDeadlineDate(); // deadline, hard deadline
extensionDate = new Date(this.minDate);
maxDate = this.data.task.localDeadlineDate(); // due date, hard deadline
extensionDate = new Date(startOfDay(this.minDate));
addEvent(type: string, event: MatDatepickerInputEvent<Date>) {
this.extensionDate = new Date(event.value);
}
Expand Down

0 comments on commit c3cb961

Please sign in to comment.