Skip to content

Commit

Permalink
feat: validate dates
Browse files Browse the repository at this point in the history
  • Loading branch information
gion-andri committed Dec 5, 2023
1 parent ab7a48a commit ade83f3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/app/shared/utils/dates.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,22 @@ export class DatesUtil {
const djs = dayjs.unix(date);
return djs.format('DD-MM-YYYY H:mm:ss');
}

isValidEventDate(input: string): string | null {
const parts = input.split("-");
if (parts.length === 3 && (+parts[0] > 9999)) {
return 'too_far_future';
}

const dateObj = dayjs(input, "YYYY-MM-DD");
if (dateObj.isBefore(dayjs())) {
return 'past';
}

if (dateObj.isAfter(dayjs('2039-12-31'))) {
return 'too_far_future';
}

return null;
}
}
17 changes: 17 additions & 0 deletions src/app/shared/validators/date.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AbstractControl, FormControl, ValidationErrors, ValidatorFn } from "@angular/forms";
import { DatesUtil } from '../utils/dates.util';

export function dateValidator(datesUtil: DatesUtil): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const selectedDate = (control as FormControl).value

const val = datesUtil.isValidEventDate(selectedDate);
if (val === 'past') {
return {'isInPast': true}
} else if (val === 'too_far_future') {
return {'isInFuture': true}
}

return null;
}
}
10 changes: 9 additions & 1 deletion src/app/user-area/pages/new-event/new-event.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,15 @@ <h2>
<input type="date" class="form-control" id="date-{{i}}" formControlName="date"
[class.is-invalid]="isOccurrenceFieldInvalid(i, 'date')">
<div class="invalid-feedback" *ngIf="isOccurrenceFieldInvalid(i, 'date')">
Endatescha p.pl. ina data.
<ng-container *ngIf="isOccurrenceFieldError(i, 'date', 'required')">
Endatescha p.pl. ina data.
</ng-container>
<ng-container *ngIf="isOccurrenceFieldError(i, 'date', 'isInPast')">
Betg pussaivel da registrar ina data en il passà.
</ng-container>
<ng-container *ngIf="isOccurrenceFieldError(i, 'date', 'isInFuture')">
Betg pussaivel da registrar occurrenzas suenter ils 31-12-2039.
</ng-container>
</div>
</div>
<div class="col-md-2" [class.is-invalid]="isOccurrenceFieldInvalid(i, 'start')">
Expand Down
13 changes: 11 additions & 2 deletions src/app/user-area/pages/new-event/new-event.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { rmLocale } from '../../../shared/utils/day-js-locale';
import { minCheckboxValidator } from '../../../shared/validators/mincheckbox.validator';
import { debounceTime, fromEvent, take } from 'rxjs';
import { NetiquetteComponent } from '../../../shared/components/netiquette/netiquette.component';
import { dateValidator } from '../../../shared/validators/date.validator';
import { DatesUtil } from '../../../shared/utils/dates.util';

const regexUrl = '(https?://)?([\\da-z.-]+)\\.([a-z.]{2,6})[/\\w .-]*/?';

Expand Down Expand Up @@ -54,6 +56,7 @@ export class NewEventComponent implements OnInit {
private router: Router,
private notificationsService: NotificationsService,
private el: ElementRef,
private datesUtil: DatesUtil
) {
const navigation = this.router.getCurrentNavigation();
if (navigation && navigation.extras.state) {
Expand Down Expand Up @@ -170,6 +173,12 @@ export class NewEventComponent implements OnInit {
return field.hasError(errorName);
}

isOccurrenceFieldError(index: number, fieldName: string, errorName: string) {
const occurrence = this.eventOccurrencesFormArray.at(index);
const field = occurrence.get(fieldName)!;
return field.hasError(errorName);
}

get genresFormArray(): FormArray {
return this.f.get('genres') as FormArray;
}
Expand Down Expand Up @@ -278,7 +287,7 @@ export class NewEventComponent implements OnInit {
if (this.eventToChange) {
this.eventToChange.occurrences.forEach((occurrence, idx) => {
const o = this.fb.group({
date: [dayjs(occurrence.date, 'D-M-YYYY').format('YYYY-MM-DD'), Validators.required],
date: [dayjs(occurrence.date, 'D-M-YYYY').format('YYYY-MM-DD'), [Validators.required, dateValidator(this.datesUtil)]],
start: [occurrence.start, Validators.required],
end: [occurrence.end],
isAllDay: [occurrence.isAllDay],
Expand All @@ -294,7 +303,7 @@ export class NewEventComponent implements OnInit {

addNewOccurrence() {
const o = this.fb.group({
date: ['', Validators.required],
date: ['', [Validators.required, dateValidator(this.datesUtil)]],
start: ['', Validators.required],
end: [''],
isAllDay: [false],
Expand Down

0 comments on commit ade83f3

Please sign in to comment.