Skip to content

Commit

Permalink
Fix salesagility#399 - Date Components Update
Browse files Browse the repository at this point in the history
  • Loading branch information
mpuyosa91 committed Nov 20, 2023
1 parent 62c7479 commit 6958469
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
[class.is-invalid]="field.formControl.invalid && field.formControl.touched"
[formControl]="field.formControl"
[startDate]="dateModel"
(ngModelChange)="setModel($event)"
#datepicker="ngbDatepicker">
<span class="input-group-append align-items-end">
<scrm-button [config]="getOpenButton(datepicker)">
Expand Down
48 changes: 33 additions & 15 deletions core/app/core/src/lib/fields/date/templates/edit/date.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
* the words "Supercharged by SuiteCRM".
*/

import {isEqual} from 'lodash-es';
import {Component, OnDestroy, OnInit,} from '@angular/core';
import {NgbDateAdapter, NgbDateParserFormatter, NgbDateStruct, NgbInputDatepicker} from '@ng-bootstrap/ng-bootstrap';
import {ButtonInterface, isEmptyString, isVoid} from 'common';
import {BaseDateTimeComponent} from '../../../base/datetime/base-datetime.component';
import {DataTypeFormatter} from '../../../../services/formatters/data-type.formatter.service';
import {DateParserFormatter} from '../../../base/datetime/date/date-parser-formatter.service';
import {DateFormatter} from '../../../../services/formatters/datetime/date-formatter.service';
Expand Down Expand Up @@ -62,32 +62,22 @@ export class DateEditFieldComponent extends BaseDateComponent implements OnInit,
}

ngOnInit(): void {

// Note: handle NgbDatePicker default validation
// Note: convert empty form value to null for the ngb date validator to pass it
if (isVoid(this.field.value) || isEmptyString(this.field.value)) {
this.field.formControl.setValue(null);
} else {
this.field.formControl.setValue(this.formatter.toUserFormat(this.field.value, {toFormat: this.getDateFormat()}));
}
this.setFormControlValue(this.field.value);

const adapter = this.dateAdapter as DateAdapter;
adapter.setUserFormat(this.getDateFormat());
const parserFormatter = this.dateParserFormatter as DateParserFormatter;
parserFormatter.setUserFormat(this.getDateFormat());
this.dateModel = this.formatter.dateFormatToStruct(this.field.value, this.formatter.getInternalFormat());

this.setDateModel(this.field.value);

this.subscribeValueChanges();
}

ngOnDestroy(): void {
this.unsubscribeAll();
}

setModel(value: any): void {
this.field.value = this.formatter.toInternalFormat(value, {fromFormat: this.getDateFormat()});
this.dateModel = this.formatter.dateFormatToStruct(value, this.getDateFormat());
}

getOpenButton(datepicker: NgbInputDatepicker): ButtonInterface {
return {
klass: 'btn btn-sm btn-outline-secondary m-0 border-0',
Expand All @@ -104,4 +94,32 @@ export class DateEditFieldComponent extends BaseDateComponent implements OnInit,
return ['bottom-left', 'bottom-right', 'top-left', 'top-right'];
}

protected setDateModel(newValue: string): void {
this.dateModel = this.formatter.dateFormatToStruct(newValue, this.getDateFormat());
}

protected setFieldValue(newValue: string): void {
this.field.value = this.formatter.toInternalFormat(newValue);

this.setDateModel(newValue);
}

protected setFormControlValue(newValue: string): void {
// Note: handle NgbDatePicker default validation
// Note: convert empty form value to null for the ngb date validator to pass it

const trimmedNewValue: string | null = newValue?.trim();

let newFormattedValue: string | null = null;
if (!isVoid(trimmedNewValue) && !isEmptyString(trimmedNewValue)) {
newFormattedValue = this.formatter.toUserFormat(trimmedNewValue, {toFormat: this.getDateFormat()});
}

if (isEqual(this.field.formControl.value, newFormattedValue)) {
return;
}

this.field.formControl.setValue(newFormattedValue);
this.field.formControl.markAsDirty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
[class.is-invalid]="field.formControl.invalid && field.formControl.touched"
[formControl]="field.formControl"
[startDate]="dateModel"
(ngModelChange)="setModel($event)"
#datepicker="ngbDatepicker">
<span class="input-group-append align-items-end">
<scrm-button [config]="getOpenButton(datepicker)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* the words "Supercharged by SuiteCRM".
*/

import {isEmpty, isEqual} from 'lodash-es';
import {Component, OnDestroy, OnInit,} from '@angular/core';
import {NgbDateAdapter, NgbDateParserFormatter, NgbDateStruct, NgbInputDatepicker} from '@ng-bootstrap/ng-bootstrap';
import {ButtonInterface, isEmptyString, isVoid} from 'common';
import {PlacementArray} from '@ng-bootstrap/ng-bootstrap/util/positioning';
import {BaseDateTimeComponent} from '../../../base/datetime/base-datetime.component';
import {DataTypeFormatter} from '../../../../services/formatters/data-type.formatter.service';
import {DateParserFormatter} from '../../../base/datetime/date/date-parser-formatter.service';
import {DateFormatter} from '../../../../services/formatters/datetime/date-formatter.service';
Expand Down Expand Up @@ -61,38 +61,22 @@ export class DateFilterFieldComponent extends BaseDateComponent implements OnIni
}

ngOnInit(): void {
super.ngOnInit();

let current = null;
if (this.field.criteria.values && this.field.criteria.values.length > 0) {
if (!isEmpty(this.field.criteria.values)) {
current = this.field.criteria.values[0];
if (isVoid(current) || isEmptyString(current)) {
current = null;
}
}
this.setFormControlValue(current);

let formattedValue = null;
if (!isVoid(current) && !isEmptyString(current)) {
current = current.trim();
formattedValue = this.typeFormatter.toUserFormat(this.field.type, current, {mode: 'edit'});
}
this.field.value = current ?? '';
this.field.formControl.setValue(formattedValue);
this.field.formControl.markAsDirty();

if (!isVoid(current)) {
this.setModel(current);
}
this.subscribeValueChanges();
}

ngOnDestroy(): void {
this.unsubscribeAll();
}

setModel(value: any): void {
this.field.formControl.setValue(value);
this.field.value = this.formatter.toInternalFormat(value);
this.dateModel = this.formatter.dateFormatToStruct(value, this.formatter.getUserFormat());
}

getOpenButton(datepicker: NgbInputDatepicker): ButtonInterface {
return {
klass: 'btn btn-sm btn-outline-secondary m-0 border-0',
Expand All @@ -109,10 +93,36 @@ export class DateFilterFieldComponent extends BaseDateComponent implements OnIni
return ['bottom-left', 'bottom-right', 'top-left', 'top-right'];
}

protected setFieldValue(newValue): void {
this.field.value = newValue;
protected setCriteria(newValue: string): void {
this.field.criteria.operator = '=';
this.field.criteria.values = [newValue];
}

protected setDateModel(newValue: string): void{
this.dateModel = this.formatter.dateFormatToStruct(newValue, this.formatter.getUserFormat());
}

protected setFieldValue(newValue: string): void {
this.field.value = this.formatter.toInternalFormat(newValue);

this.setDateModel(newValue);
this.setCriteria(newValue);
}

protected setFormControlValue(newValue: string | null): void {
const trimmedNewValue: string | null = newValue?.trim();

let newFormattedValue: string | null = null;
if (!isVoid(trimmedNewValue) && !isEmptyString(trimmedNewValue)) {
newFormattedValue = this.typeFormatter.toUserFormat(this.field.type, trimmedNewValue, {mode: 'edit'});
}

if (isEqual(this.field.formControl.value, newFormattedValue)) {
return;
}

this.field.formControl.setValue(newFormattedValue);
this.field.formControl.markAsDirty();
}

}

0 comments on commit 6958469

Please sign in to comment.