End Survey Now
</button>
</div>
- <eg-fm-record-editor displayMode="inline"
+ <eg-fm-record-editor
+ #editDialog
+ displayMode="inline"
hiddenFieldsList="id"
datetimeFieldsList="start_date,end_date"
idlClass="asv"
mode="update"
+ fieldOrder="description,start_date,end_date"
+ [fieldOptions]="{end_date:{customTemplate:{template:endDateTemplate}},
+ start_date:{customTemplate:{template:startDateTemplate}}}
+ "
[record]="surveyObj">
+ <!-- start_date:{isDataValid:areDatesValid}, -->
</eg-fm-record-editor>
+ <ng-template #startDateTemplate let-field="field" let-record="record">
+ <eg-datetime-select
+ initialIso="{{record['start_date'](startDate)}}"
+ (onChangeAsIso)="setStartDate($event)"
+ >
+ <!--
+ initialIso="{{record[field.name]()}}"
+ domId="endTime"
+ [showTZ]="editDialog.timezone"
+ [timezone]="editDialog.timezone"
+ [egNotBeforeMoment]="momentizeIsoString(record['start_date'](), editDialog.timezone)"
+ [readOnly]="field.readOnly"
+ -->
+ </eg-datetime-select>
+ </ng-template>
+
+ <ng-template #endDateTemplate let-field="field" let-record="record">
+ <eg-datetime-select
+ initialIso="{{record['end_date'](endDate)}}"
+ (onChangeAsIso)="setEndDate($event)">
+ </eg-datetime-select>
+ <div *ngIf="illegalEndDate" class="mt-3 alert alert-danger" i18n>
+ End date must be after start date.
+ </div>
+ </ng-template>
+
</div>
</ng-template>
</ngb-tab>
import {AuthService} from '@eg/core/auth.service';
import {IdlObject, IdlService } from '@eg/core/idl.service';
import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
+import * as moment from 'moment-timezone';
+import {FormatService} from '@eg/core/format.service';
@Component({
templateUrl: './survey-edit.component.html'
newAnswerArray: object[];
newQuestionText: string;
surveyTab: string;
+ illegalEndDate: boolean = false;
+ startDate: any;
+ endDate: any;
- @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
+ @ViewChild('editDialog', { static: false }) editDialog: FmRecordEditorComponent;
@ViewChild('createAnswerString', { static: true })
createAnswerString: StringComponent;
private route: ActivatedRoute,
private toast: ToastService,
private idl: IdlService,
+ private format: FormatService,
) {
}
).subscribe(res => {
this.surveyObj = res;
this.buildLocalArray(res);
+ this.startDate = this.surveyObj.start_date();
+ this.endDate = this.surveyObj.end_date();
+ // this.editDialog.fields['start_date'] = this.startDate;
+ // this.editDialog.fields['end_date'] = this.endDate;
return res;
});
}
}
return false;
}
+
+ setEndDate(event): any {
+ console.log("set end")
+ // this.checkForValidDates();
+ // console.log(this.editDialog)
+ this.editDialog.fields['end_date'] = event;
+ this.checkForValidDates();
+ }
+
+ setStartDate(event): any {
+ console.log("set start")
+ // this.checkForValidDates();
+ // console.log(this.editDialog)
+ this.editDialog.fields['start_date'] = event;
+ this.checkForValidDates();
+ }
+
+ areDatesValid = (): boolean => {
+ console.log("** are dates valid?" + this.illegalEndDate)
+ return this.illegalEndDate;
+ }
+
+ checkForValidDates(): any {
+ console.log("** check")
+ console.log(this.editDialog)
+ // console.log(this.editDialog['fmEditForm'])
+ console.log(this.editDialog.fields['start_date']);
+ console.log(this.editDialog.fields['end_date'])
+ // console.log(typeof this.editDialog.fields['start_date']);
+ const d1:any = new Date(this.editDialog.fields['start_date']);
+ const d2:any = new Date(this.editDialog.fields['end_date']);
+ // console.log("** check2")
+ // console.log(d1);
+ // console.log(d2);
+ // console.log(d1 - d2);
+ if (d1-d2 >= 0) {
+ console.log("illegal date")
+ this.illegalEndDate = true;
+ // console.log(this.editDialog['fmEditForm'])
+ // this.editDialog.fmEditForm.setErrors({'invalid': true})
+ } else {
+ this.illegalEndDate = false;
+ }
+ // console.log("start / end dates:")
+ // console.log(this.editDialog.fields['start_date'])
+ // console.log(typeof this.editDialog.fields['start_date'])
+ // console.log(this.editDialog.fields['end_date'])
+ // console.log(typeof this.editDialog.fields['end_date'])
+ }
+
+ momentizeIsoString(isoString: string, timezone: string): moment.Moment {
+ if (!isoString) {
+ return null;
+ }
+ // console.log("@@@ momentize")
+ // console.log(isoString)
+ // console.log(timezone)
+ // console.log(this.format.momentizeIsoString(isoString, timezone))
+ return this.format.momentizeIsoString(isoString, timezone);
+ }
+
+
}