convert date to datepicker calendar in step 3

This commit is contained in:
Tykayn 2021-11-17 16:25:24 +01:00 committed by tykayn
parent 5ca08d489f
commit 706fd28698
8 changed files with 93 additions and 61 deletions

View File

@ -1,5 +1,5 @@
export interface DateChoice { export interface DateChoice {
literal: string; literal: String;
timeSlices: TimeSlices[]; timeSlices: TimeSlices[];
date_object: Date; date_object: Date;
} }

View File

@ -2,6 +2,7 @@ export interface DateChoice {
literal: string; literal: string;
timeSlices: TimeSlices[]; timeSlices: TimeSlices[];
date_object: Date; date_object: Date;
date_input: String;
} }
export interface TimeSlices { export interface TimeSlices {

View File

@ -1,5 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { DateChoice, defaultTimeOfDay } from '../../../../mocks/old-stuff/config/defaultConfigs'; import { defaultTimeOfDay } from '../../../../mocks/old-stuff/config/defaultConfigs';
import { DateChoice } from '../models/dateChoice.model';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
@ -91,37 +92,39 @@ export class DateUtilitiesService {
return 0; return 0;
} }
makeDefaultCalendarDateChoices(): Date[] {
return [
this.addDaysToDate(1, new Date()),
this.addDaysToDate(2, new Date()),
this.addDaysToDate(3, new Date()),
];
}
/** /**
* fill default dates for today + the next 3 dateChoices * fill default dates for today + the next 3 dateChoices
*/ */
makeDefaultDateChoices(): DateChoice[] { makeDefaultDateChoices(): DateChoice[] {
const today = new Date(); const today = new Date();
const ladate = this.addDaysToDate(0, today);
const ladate2 = this.addDaysToDate(1, today); const ladate2 = this.addDaysToDate(1, today);
const ladate3 = this.addDaysToDate(2, today); const ladate3 = this.addDaysToDate(2, today);
const ladate4 = this.addDaysToDate(3, today); const ladate4 = this.addDaysToDate(3, today);
return [ return [
{ this.convertToDateChoiceObject(ladate2),
literal: this.formateDateToInputStringNg(ladate), this.convertToDateChoiceObject(ladate3),
timeSlices: Object.create(defaultTimeOfDay), this.convertToDateChoiceObject(ladate4),
date_object: ladate,
},
{
literal: this.formateDateToInputStringNg(ladate2),
timeSlices: Object.create(defaultTimeOfDay),
date_object: ladate2,
},
{
literal: this.formateDateToInputStringNg(ladate3),
timeSlices: Object.create(defaultTimeOfDay),
date_object: ladate3,
},
{
literal: this.formateDateToInputStringNg(ladate4),
timeSlices: Object.create(defaultTimeOfDay),
date_object: ladate4,
},
]; ];
} }
/**
* convert a date to a DateChoice
* @param date
*/
convertToDateChoiceObject(date: Date): DateChoice {
return {
literal: this.formateDateToInputStringNg(date),
timeSlices: Object.create(defaultTimeOfDay),
date_object: date,
date_input: `${date.getFullYear()}-${date.getMonth()}-${date.getDay()}`,
};
}
} }

View File

@ -33,7 +33,7 @@ export class PollService implements Resolve<Poll> {
public endDateInterval: string; public endDateInterval: string;
public intervalDays: number = 1; public intervalDays: number = 1;
public intervalDaysDefault = 7; public intervalDaysDefault = 7;
public dateList: DateChoice[] = []; // sets of days as strings, config to set identical time for days in a special days poll public dateChoiceList: DateChoice[] = []; // sets of days as strings, config to set identical time for days in a special days poll
public timeList: TimeSlices[] = []; // ranges of time expressed as strings public timeList: TimeSlices[] = []; // ranges of time expressed as strings
public previousRouteName: string = '/administration'; public previousRouteName: string = '/administration';
public nextRouteName: string = '/administration/step/2'; public nextRouteName: string = '/administration/step/2';
@ -67,11 +67,9 @@ export class PollService implements Resolve<Poll> {
this.createFormGroup(); this.createFormGroup();
// fill in the next 3 days of the calendar date picker // fill in the next 3 days of the calendar date picker
this.calendar = [ this.calendar = this.DateUtilitiesService.makeDefaultCalendarDateChoices();
this.DateUtilitiesService.addDaysToDate(1, new Date()), this.dateChoiceList = this.DateUtilitiesService.makeDefaultDateChoices();
this.DateUtilitiesService.addDaysToDate(2, new Date()),
this.DateUtilitiesService.addDaysToDate(3, new Date()),
];
// disable days before today // disable days before today
for (let i = 1; i < 31; i++) { for (let i = 1; i < 31; i++) {
this.disabled_dates.push(this.DateUtilitiesService.addDaysToDate(-i, new Date())); this.disabled_dates.push(this.DateUtilitiesService.addDaysToDate(-i, new Date()));
@ -142,6 +140,7 @@ export class PollService implements Resolve<Poll> {
hasMaxCountOfAnswers: [300, [Validators.required]], hasMaxCountOfAnswers: [300, [Validators.required]],
useVoterUniqueLink: [false, [Validators.required]], useVoterUniqueLink: [false, [Validators.required]],
voterEmailList: ['', []], voterEmailList: ['', []],
hasSeveralHours: [false, []],
allowNewDateTime: [true, [Validators.required]], allowNewDateTime: [true, [Validators.required]],
}); });
this.form = form; this.form = form;
@ -306,12 +305,12 @@ export class PollService implements Resolve<Poll> {
timeList: [], timeList: [],
}); });
}); });
this.dateList = [...new Set(converted)]; this.dateChoiceList = [...new Set(converted)];
// add only dates that are not already present with a Set of unique items // add only dates that are not already present with a Set of unique items
console.log('this.dateList', this.dateList); console.log('this.dateChoiceList', this.dateChoiceList);
this.showDateInterval = false; this.showDateInterval = false;
this.form.patchValue({ choices: this.dateList }); this.form.patchValue({ choices: this.dateChoiceList });
this.toastService.display(`les dates ont été ajoutées aux réponses possibles.`); this.toastService.display(`les dates ont été ajoutées aux réponses possibles.`);
} }
@ -662,8 +661,30 @@ export class PollService implements Resolve<Poll> {
}); });
} }
convertCalendarDatesToChoices(array_dates) { /**
return array_dates; * convertir les dates de la propriété Calendar en objets de saisie de texte
*/
convertCalendarToText() {
let converted = [];
for (let someDate of this.calendar) {
converted.push(this.DateUtilitiesService.convertToDateChoiceObject(someDate));
}
this.dateChoiceList = converted;
return converted;
}
/**
* convert the DateChoices to an arrray of Dates for calendar picker
*/
convertTextToCalendar() {
let converted = [];
for (let someDateChoice of this.dateChoiceList) {
converted.push(someDateChoice.date_object);
}
this.calendar = converted;
return;
} }
patchFormWithPoll(poll: Poll) { patchFormWithPoll(poll: Poll) {
@ -693,7 +714,7 @@ export class PollService implements Resolve<Poll> {
const field = form.value[pk]; const field = form.value[pk];
newpoll[pk] = field; newpoll[pk] = field;
} else { } else {
// console.log('manque pollKey', pk); console.log('newPollFromForm : manque pollKey', pk);
} }
} }
@ -720,11 +741,7 @@ export class PollService implements Resolve<Poll> {
for (let elem of this.calendar) { for (let elem of this.calendar) {
console.log('elem', elem); console.log('elem', elem);
let converted_day = { let converted_day = this.DateUtilitiesService.convertToDateChoiceObject(elem);
literal: this.DateUtilitiesService.formateDateToInputStringNg(elem),
timeSlices: [],
date_object: elem,
};
newpoll.dateChoices.push(converted_day); newpoll.dateChoices.push(converted_day);
} }
console.log('newpoll.dateChoices', newpoll.dateChoices); console.log('newpoll.dateChoices', newpoll.dateChoices);

View File

@ -6,14 +6,6 @@
{{ 'dates.add' | translate }} {{ 'dates.add' | translate }}
</button> </button>
</div> </div>
<div class="column">
<span class="count-dates title">
{{ dateChoices.length }}
</span>
<span>
{{ 'dates.count_dates' | translate }}
</span>
</div>
</div> </div>
<div <div
@ -35,9 +27,14 @@
{{ choice.date_object | date: 'E':'Europe/Paris':'fr_FR' }} {{ choice.date_object | date: 'E':'Europe/Paris':'fr_FR' }}
</span> </span>
</span> </span>
{{ choice.date_input | date }}
<pre>
{{ choice.date_object | json }}
</pre
>
<input <input
[(ngModel)]="choice.date_object" [(ngModel)]="choice.date_input"
(keyup)="keyOnChoice($event, id)"
class="date-choice-item" class="date-choice-item"
name="dateChoices_{{ id }}" name="dateChoices_{{ id }}"
id="dateChoices_{{ id }}" id="dateChoices_{{ id }}"
@ -55,7 +52,7 @@
<div class="text-right"> <div class="text-right">
<button (click)="addTimeToDate(choice, id)" class="btn btn--primary"> <button (click)="addTimeToDate(choice, id)" class="btn btn--primary">
<i class="fa fa-plus"></i> <i class="fa fa-plus"></i>
<!-- {{ 'dates.add_time' | translate }}--> {{ 'dates.add_time' | translate }}
<i class="fa fa-clock-o"></i> <i class="fa fa-clock-o"></i>
</button> </button>
</div> </div>

View File

@ -7,7 +7,8 @@ import { StorageService } from '../../../../../../core/services/storage.service'
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { ShortcutsHelpComponent } from '../../../../../shared/components/ui/shortcuts-help/shortcuts-help.component'; import { ShortcutsHelpComponent } from '../../../../../shared/components/ui/shortcuts-help/shortcuts-help.component';
import { DateChoice } from '../../../../../../core/models/dateChoice.model'; import { DateChoice } from '../../../../../../core/models/dateChoice.model';
import { PollService } from '../../../../../../core/services/poll.service';
import { DateUtilitiesService } from '../../../../../../core/services/date.utilities.service';
@Component({ @Component({
selector: 'app-day-list', selector: 'app-day-list',
templateUrl: './day-list.component.html', templateUrl: './day-list.component.html',
@ -27,6 +28,8 @@ export class DayListComponent {
constructor( constructor(
public dialog: MatDialog, public dialog: MatDialog,
private toastService: ToastService, private toastService: ToastService,
private pollService: PollService,
private dateUtilitiesService: DateUtilitiesService,
private cd: ChangeDetectorRef, private cd: ChangeDetectorRef,
@Inject(DOCUMENT) private document: any, @Inject(DOCUMENT) private document: any,
private storageService: StorageService private storageService: StorageService
@ -127,7 +130,7 @@ export class DayListComponent {
} }
addChoice(optionalLabel = ''): void { addChoice(optionalLabel = ''): void {
this.storageService.dateChoices.push({ this.pollService.dateChoiceList.push({
literal: '', literal: '',
timeSlices: [], timeSlices: [],
date_object: new Date(), date_object: new Date(),

View File

@ -2,11 +2,6 @@
<app-stepper [step_current]="3" [step_max]="5"></app-stepper> <app-stepper [step_current]="3" [step_max]="5"></app-stepper>
<app-errors-list [form]="pollService.form"></app-errors-list> <app-errors-list [form]="pollService.form"></app-errors-list>
<!-- choix spécialement pour les dates--> <!-- choix spécialement pour les dates-->
<span class="count-dates title">
{{ pollService.calendar.length }}
</span>
<span> - {{ 'dates.count_dates' | translate }} </span>
<div class="calendar" *ngIf="mode_calendar"> <div class="calendar" *ngIf="mode_calendar">
<p-calendar <p-calendar
[(ngModel)]="pollService.calendar" [(ngModel)]="pollService.calendar"
@ -20,8 +15,18 @@
[showWeek]="false" [showWeek]="false"
></p-calendar> ></p-calendar>
</div> </div>
<button class="button" (click)="mode_calendar = !mode_calendar" [ngClass]="{ 'is-primary': !mode_calendar }">
<i class="fa fa-pencil"></i> Saisir les dates manuellement <div class="text-date-list" *ngIf="!mode_calendar">
<app-day-list
[form]="pollService.form"
[dateChoices]="pollService.dateChoiceList"
[hasSeveralHours]="pollService.form.value.hasSeveralHours"
></app-day-list>
</div>
<button class="button" (click)="changeDateInputMode()" [ngClass]="{ 'is-primary': !mode_calendar }">
<span *ngIf="mode_calendar"> <i class="fa fa-pencil"></i> Saisir les dates manuellement </span>
<span *ngIf="!mode_calendar"> <i class="fa fa-calendar-o"></i> Saisir les dates dans le calendrier </span>
</button> </button>
</div> </div>
<div class="columns"> <div class="columns">

View File

@ -12,7 +12,7 @@ export class StepThreeComponent implements OnInit {
step_max: any; step_max: any;
@Input() @Input()
form: any; form: any;
public mode_calendar = true; public mode_calendar = false;
constructor(public pollService: PollService) { constructor(public pollService: PollService) {
this.pollService.step_current = 3; this.pollService.step_current = 3;
@ -23,4 +23,10 @@ export class StepThreeComponent implements OnInit {
drop(event: CdkDragDrop<string[]>) { drop(event: CdkDragDrop<string[]>) {
// moveItemInArray(this.pollService.choices, event.previousIndex, event.currentIndex); // moveItemInArray(this.pollService.choices, event.previousIndex, event.currentIndex);
} }
changeDateInputMode() {
this.mode_calendar ? this.pollService.convertCalendarToText() : this.pollService.convertTextToCalendar();
this.mode_calendar = !this.mode_calendar;
}
} }