mirror of
https://framagit.org/framasoft/framadate/funky-framadate-front.git
synced 2023-08-25 13:53:14 +02:00
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
|
import { Component, Input, OnInit } from '@angular/core';
|
||
|
import { environment } from '../../../../../../environments/environment';
|
||
|
import { DateUtilitiesService } from '../../../../../core/services/date.utilities.service';
|
||
|
import { FormArray, FormGroup } from '@angular/forms';
|
||
|
import { ToastService } from '../../../../../core/services/toast.service';
|
||
|
import { DateChoice } from '../../../../../../../mocks/old-stuff/config/defaultConfigs';
|
||
|
|
||
|
@Component({
|
||
|
selector: 'app-date-interval',
|
||
|
templateUrl: './interval.component.html',
|
||
|
styleUrls: ['./interval.component.scss'],
|
||
|
})
|
||
|
export class IntervalComponent implements OnInit {
|
||
|
@Input()
|
||
|
public form: FormGroup;
|
||
|
showDateInterval = true;
|
||
|
intervalDays: any;
|
||
|
intervalDaysDefault = environment.interval_days_default;
|
||
|
startDateInterval: Date;
|
||
|
startDateIntervalString: string;
|
||
|
endDateInterval: Date;
|
||
|
endDateIntervalString: string;
|
||
|
dateList: DateChoice[]; // sets of dateList as strings, config to set identical time for dateList in a special dateList poll
|
||
|
dateCalendarEnum: Date[] = [new Date('02/09/2021')];
|
||
|
|
||
|
constructor(public dateUtilities: DateUtilitiesService, private toastService: ToastService) {}
|
||
|
|
||
|
ngOnInit(): void {
|
||
|
this.setDefaultDatesForInterval();
|
||
|
}
|
||
|
|
||
|
countDays(): void {
|
||
|
this.intervalDays = this.dateUtilities.countDays(
|
||
|
this.dateUtilities.parseInputDateToDateObject(this.startDateInterval),
|
||
|
this.dateUtilities.parseInputDateToDateObject(this.endDateInterval)
|
||
|
);
|
||
|
// this.cd.detectChanges();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* add all the dates between the start and end dates in the interval section
|
||
|
*/
|
||
|
addIntervalOfDates(): void {
|
||
|
const newIntervalArray = this.dateUtilities.getDatesInRange(
|
||
|
this.dateUtilities.parseInputDateToDateObject(this.startDateInterval),
|
||
|
this.dateUtilities.parseInputDateToDateObject(this.endDateInterval),
|
||
|
1
|
||
|
);
|
||
|
|
||
|
const converted = [];
|
||
|
newIntervalArray.forEach((element) => {
|
||
|
converted.push({
|
||
|
literal: element.literal,
|
||
|
date_object: element.date_object,
|
||
|
timeList: [],
|
||
|
});
|
||
|
});
|
||
|
this.dateList = [...new Set(converted)];
|
||
|
// add only dates that are not already present with a Set of unique items
|
||
|
console.log('this.dateList', this.dateList);
|
||
|
this.showDateInterval = false;
|
||
|
|
||
|
this.form.patchValue({ choices: this.dateList });
|
||
|
|
||
|
this.toastService.display(`les dates ont été ajoutées aux réponses possibles.`);
|
||
|
}
|
||
|
|
||
|
get dateChoices() {
|
||
|
return this.form.get('dateChoices') as FormArray;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* default interval of dates proposed is from today to 7 dateList more
|
||
|
*/
|
||
|
setDefaultDatesForInterval(): void {
|
||
|
if (this.form) {
|
||
|
const dateCurrent = new Date();
|
||
|
// const dateJson = dateCurrent.toISOString();
|
||
|
this.startDateInterval = dateCurrent;
|
||
|
this.endDateInterval = this.dateUtilities.addDaysToDate(this.intervalDaysDefault, dateCurrent);
|
||
|
|
||
|
this.form.patchValue({
|
||
|
startDateInterval: this.startDateInterval,
|
||
|
endDateInterval: this.endDateInterval,
|
||
|
});
|
||
|
this.dateCalendarEnum = [
|
||
|
dateCurrent,
|
||
|
this.dateUtilities.addDaysToDate(this.intervalDaysDefault, dateCurrent),
|
||
|
];
|
||
|
this.countDays();
|
||
|
}
|
||
|
}
|
||
|
}
|