Fix issue with patching value with a date

This commit is contained in:
Giuseppe Digilio
2021-05-19 15:04:12 +02:00
parent d3466c3e82
commit d06b76af3f
2 changed files with 8 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ import {
import { JsonPatchOperationPathObject } from './json-patch-operation-path-combiner'; import { JsonPatchOperationPathObject } from './json-patch-operation-path-combiner';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { hasNoValue, hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util'; import { hasNoValue, hasValue, isEmpty, isNotEmpty } from '../../../shared/empty.util';
import { dateToISOFormat } from '../../../shared/date.util'; import { dateToISOFormat, dateToString, isNgbDateStruct } from '../../../shared/date.util';
import { VocabularyEntry } from '../../submission/vocabularies/models/vocabulary-entry.model'; import { VocabularyEntry } from '../../submission/vocabularies/models/vocabulary-entry.model';
import { FormFieldMetadataValueObject } from '../../../shared/form/builder/models/form-field-metadata-value.model'; import { FormFieldMetadataValueObject } from '../../../shared/form/builder/models/form-field-metadata-value.model';
import { FormFieldLanguageValueObject } from '../../../shared/form/builder/models/form-field-language-value.model'; import { FormFieldLanguageValueObject } from '../../../shared/form/builder/models/form-field-language-value.model';
@@ -136,6 +136,8 @@ export class JsonPatchOperationsBuilder {
operationValue = new FormFieldMetadataValueObject(value.value, value.language); operationValue = new FormFieldMetadataValueObject(value.value, value.language);
} else if (value.hasOwnProperty('authority')) { } else if (value.hasOwnProperty('authority')) {
operationValue = new FormFieldMetadataValueObject(value.value, value.language, value.authority); operationValue = new FormFieldMetadataValueObject(value.value, value.language, value.authority);
} else if (isNgbDateStruct(value)) {
operationValue = new FormFieldMetadataValueObject(dateToString(value));
} else if (value.hasOwnProperty('value')) { } else if (value.hasOwnProperty('value')) {
operationValue = new FormFieldMetadataValueObject(value.value); operationValue = new FormFieldMetadataValueObject(value.value);
} else { } else {

View File

@@ -3,7 +3,7 @@ import { NgbDateStruct } from '@ng-bootstrap/ng-bootstrap';
import { isObject } from 'lodash'; import { isObject } from 'lodash';
import * as moment from 'moment'; import * as moment from 'moment';
import { isNull } from './empty.util'; import { isNull, isUndefined } from './empty.util';
/** /**
* Returns true if the passed value is a NgbDateStruct. * Returns true if the passed value is a NgbDateStruct.
@@ -27,8 +27,9 @@ export function isNgbDateStruct(value: object): boolean {
* @return string * @return string
* the formatted date * the formatted date
*/ */
export function dateToISOFormat(date: Date | NgbDateStruct): string { export function dateToISOFormat(date: Date | NgbDateStruct | string): string {
const dateObj: Date = (date instanceof Date) ? date : ngbDateStructToDate(date); const dateObj: Date = (date instanceof Date) ? date :
((typeof date === 'string') ? ngbDateStructToDate(stringToNgbDateStruct(date)) : ngbDateStructToDate(date));
let year = dateObj.getFullYear().toString(); let year = dateObj.getFullYear().toString();
let month = (dateObj.getMonth() + 1).toString(); let month = (dateObj.getMonth() + 1).toString();
@@ -80,7 +81,7 @@ export function stringToNgbDateStruct(date: string): NgbDateStruct {
* the NgbDateStruct object * the NgbDateStruct object
*/ */
export function dateToNgbDateStruct(date?: Date): NgbDateStruct { export function dateToNgbDateStruct(date?: Date): NgbDateStruct {
if (isNull(date)) { if (isNull(date) || isUndefined(date)) {
date = new Date(); date = new Date();
} }