LP1879517 Survey Start and End Dates Error collab/khuckins/lp1879517-start-end-date-error-rebase
authorMike Risher <mrisher@catalyte.io>
Thu, 18 Feb 2021 00:23:32 +0000 (00:23 +0000)
committerKyle Huckins <khuckins@catalyte.io>
Sun, 14 Mar 2021 23:44:27 +0000 (23:44 +0000)
Change the surveys interface so that an error is raised when putting
the end date before the start date.  Also, list start date before end
date so they're less likely to be mixed up.

Signed-off-by: Mike Risher <mrisher@catalyte.io>
Changes to be committed:
modified:   Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html
modified:   Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts
modified:   Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey-edit.component.html
modified:   Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey-edit.component.ts
modified:   Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey.component.html
modified:   Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey.component.ts

Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.html
Open-ILS/src/eg2/src/app/share/fm-editor/fm-editor.component.ts
Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey-edit.component.html
Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey-edit.component.ts
Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey.component.html
Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey.component.ts

index af9f5f0..3919a2f 100644 (file)
     </ng-container>
 
     <button type="button" class="btn btn-info" 
-      [disabled]="fmEditForm.invalid" *ngIf="mode != 'view'"
+      [disabled]="fmEditForm.invalid || fieldIsInvalid()" *ngIf="mode != 'view'"
       (click)="save()" i18n>Save</button>
   </div>
 </ng-template>
index 4ac2e2e..ac6874a 100644 (file)
@@ -77,6 +77,10 @@ export interface FmFieldOptions {
     // from the default set of form inputs.
     customTemplate?: CustomFieldTemplate;
 
+    // This will disable the save button if any field has this set to false. It
+    // can be used along with a customTemplate to show an error message for non-valid input
+    isDataValid?: boolean;
+
     // help text to display via a popover
     helpText?: StringComponent;
 }
@@ -630,6 +634,17 @@ export class FmRecordEditorComponent
         );
     }
 
+    fieldIsInvalid() {
+        let inv = false;
+        for (const f in this.fieldOptions) {
+            if (this.fieldOptions[f].isDataValid === false) {
+                inv = true;
+                break;
+            }
+        }
+        return inv;
+    }
+
     remove() {
         this.confirmDel.open().subscribe(confirmed => {
             if (!confirmed) { return; }
index 86f2f20..11389b4 100644 (file)
                         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},
+                    isDataValid:!illegalEndDate}}"
                     [record]="surveyObj">
                 </eg-fm-record-editor>
+
+                <ng-template #startDateTemplate let-field="field" let-record="record">
+                    <eg-datetime-select
+                        initialIso="{{record['start_date'](startDate)}}"    
+                        (onChangeAsIso)="setStartDate($event)">
+                    </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>
index 73b3a7b..13287b3 100644 (file)
@@ -19,6 +19,9 @@ export class SurveyEditComponent implements OnInit {
     newAnswerArray: object[];
     newQuestionText: string;
     surveyTab: string;
+    illegalEndDate = false;
+    startDate: string;
+    endDate: string;
 
     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
 
@@ -79,6 +82,9 @@ export class SurveyEditComponent implements OnInit {
         ).subscribe(res => {
             this.surveyObj = res;
             this.buildLocalArray(res);
+            this.startDate = this.surveyObj.start_date();
+            this.endDate = this.surveyObj.end_date();
+            this.checkForValidDates();
             return res;
         });
     }
@@ -303,5 +309,26 @@ export class SurveyEditComponent implements OnInit {
         }
         return false;
     }
+
+    setEndDate(event): any {
+        this.endDate = event;
+        this.checkForValidDates();
+    }
+
+    setStartDate(event): any {
+        this.startDate = event;
+        this.checkForValidDates();
+    }
+
+    checkForValidDates(): any {
+        const d1: any = new Date(this.startDate);
+        const d2: any = new Date(this.endDate);
+        if (d1 - d2 >= 0) {
+            this.illegalEndDate = true;
+        } else {
+            this.illegalEndDate = false;
+        }
+    }
+
 }
 
index 394d837..8f070b8 100644 (file)
 </eg-grid>
 
 <eg-fm-record-editor 
+    #editDialog
     datetimeFieldsList="start_date,end_date"
     hiddenFieldsList="id"
-    #editDialog 
+    fieldOrder="description,start_date,end_date"
+    [fieldOptions]="{end_date:{customTemplate:{template:endDateTemplate}},
+        start_date:{customTemplate:{template:startDateTemplate},
+        isDataValid:!illegalEndDate}}"
     idlClass="asv">
 </eg-fm-record-editor>
 
+<ng-template #startDateTemplate let-field="field" let-record="record">
+    <eg-datetime-select
+        initialIso="{{record['start_date'](startDate)}}"    
+        (onChangeAsIso)="setStartDate($event)">
+    </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>
+
 <eg-string #createString i18n-text text="New Survey Added"></eg-string>
 <eg-string #createErrString i18n-text text="Failed to Create New Survey">
     </eg-string>
index c51af57..a0f147a 100644 (file)
@@ -3,7 +3,7 @@ import {Component, OnInit, Input, ViewChild} from '@angular/core';
 import {GridComponent} from '@eg/share/grid/grid.component';
 import {GridDataSource} from '@eg/share/grid/grid';
 import {Router} from '@angular/router';
-import {IdlObject} from '@eg/core/idl.service';
+import {IdlService, IdlObject} from '@eg/core/idl.service';
 import {PcrudService} from '@eg/core/pcrud.service';
 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
 import {StringComponent} from '@eg/share/string/string.component';
@@ -18,6 +18,9 @@ import {AuthService} from '@eg/core/auth.service';
 export class SurveyComponent implements OnInit {
 
     gridDataSource: GridDataSource;
+    illegalEndDate: boolean;
+    startDate: string;
+    endDate: string;
 
     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
     @ViewChild('grid', { static: true }) grid: GridComponent;
@@ -31,15 +34,14 @@ export class SurveyComponent implements OnInit {
     @ViewChild('endSurveySuccessString', { static: true }) endSurveySuccessString: StringComponent;
 
     @Input() sortField: string;
-    @Input() idlClass = 'asv';
-    @Input() dialogSize: 'sm' | 'lg' = 'lg';
 
     constructor(
         private auth: AuthService,
         private net: NetService,
         private pcrud: PcrudService,
         private toast: ToastService,
-        private router: Router
+        private router: Router,
+        private idl: IdlService,
     ) {
         this.gridDataSource = new GridDataSource();
     }
@@ -49,10 +51,10 @@ export class SurveyComponent implements OnInit {
             const orderBy: any = {};
             if (sort.length) {
                 // Sort specified from grid
-                orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
+                orderBy['asv'] = sort[0].name + ' ' + sort[0].dir;
             } else if (this.sortField) {
                 // Default sort field
-                orderBy[this.idlClass] = this.sortField;
+                orderBy['asv'] = this.sortField;
             }
 
             const searchOps = {
@@ -121,7 +123,14 @@ export class SurveyComponent implements OnInit {
     createNew = () => {
         this.editDialog.mode = 'create';
         this.editDialog.datetimeFields = 'start_date,end_date';
-        this.editDialog.open({size: this.dialogSize}).subscribe(
+        const today = new Date();
+        const d1 = new Date(today);
+        const d2 = new Date(today);
+        d1.setDate(today.getDate() + 1);  // default start is in 1 day
+        d2.setDate(today.getDate() + 30);  // default end is in 30 days
+        this.startDate = d1.toISOString();
+        this.endDate = d2.toISOString();
+        this.editDialog.open({size: 'lg'}).subscribe(
             ok => {
                 this.createString.current()
                     .then(str => this.toast.success(str));
@@ -135,4 +144,25 @@ export class SurveyComponent implements OnInit {
             }
         );
     }
+
+    setEndDate(event): any {
+        this.endDate = event;
+        this.checkForValidDates();
+    }
+
+    setStartDate(event): any {
+        this.startDate = event;
+        this.checkForValidDates();
+    }
+
+    checkForValidDates(): any {
+        const d1: any = new Date(this.startDate);
+        const d2: any = new Date(this.endDate);
+        if (d1 - d2 >= 0) {
+            this.illegalEndDate = true;
+        } else {
+            this.illegalEndDate = false;
+        }
+    }
+
 }