Format date fields without patch form

This commit is contained in:
Jose Alberto Hernandez
2022-06-10 10:34:19 +02:00
committed by Arnold Gálovics
parent 9d43181cea
commit 6ea3d97538
191 changed files with 1408 additions and 1173 deletions
@@ -2,11 +2,11 @@
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { DatePipe } from '@angular/common';
/** Custom Services */
import { OrganizationService } from '../../organization.service';
import { SettingsService } from 'app/settings/settings.service';
import { Dates } from 'app/core/utils/dates';
/**
* Create Office component.
@@ -34,14 +34,14 @@ export class CreateOfficeComponent implements OnInit {
* @param {SettingsService} settingsService Settings Service.
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {DatePipe} datePipe Date Pipe to format date.
* @param {Dates} dateUtils Date Utils to format date.
*/
constructor(private formBuilder: FormBuilder,
private organizationService: OrganizationService,
private settingsService: SettingsService,
private router: Router,
private route: ActivatedRoute,
private datePipe: DatePipe) {
private dateUtils: Dates) {
this.route.data.subscribe((data: { offices: any }) => {
this.officeData = data.offices;
});
@@ -68,16 +68,19 @@ export class CreateOfficeComponent implements OnInit {
* if successful redirects to offices
*/
submit() {
const prevOpeningDate: Date = this.officeForm.value.openingDate;
// TODO: Update once language and date settings are setup
const officeFormData = this.officeForm.value;
const locale = this.settingsService.language.code;
const dateFormat = this.settingsService.dateFormat;
this.officeForm.patchValue({
openingDate: this.datePipe.transform(prevOpeningDate, dateFormat)
});
const office = this.officeForm.value;
office.locale = this.settingsService.language.code;
office.dateFormat = dateFormat;
this.organizationService.createOffice(office).subscribe(response => {
const prevOpeningDate: Date = this.officeForm.value.openingDate;
if (officeFormData.openingDate instanceof Date) {
officeFormData.openingDate = this.dateUtils.formatDate(prevOpeningDate, dateFormat);
}
const data = {
...officeFormData,
dateFormat,
locale
};
this.organizationService.createOffice(data).subscribe(response => {
this.router.navigate(['../'], { relativeTo: this.route });
});
}
@@ -40,7 +40,7 @@
<mat-form-field>
<mat-label>External ID</mat-label>
<input matInput formControlName="externalId" formControlName="externalId">
<input matInput formControlName="externalId">
</mat-form-field>
</div>
@@ -2,7 +2,7 @@
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { DatePipe } from '@angular/common';
import { Dates } from 'app/core/utils/dates';
/** Custom Services */
import { OrganizationService } from 'app/organization/organization.service';
@@ -35,14 +35,14 @@ export class EditOfficeComponent implements OnInit {
* @param {ActivatedRoute} route Activated Route.
* @param {Router} router Router for navigation.
* @param {MatDialog} dialog Dialog reference.
* @param {DatePipe} datepipe Convert Date.
* @param {Dates} dateUtils Date Utils
*/
constructor(private organizationService: OrganizationService,
private settingsService: SettingsService,
private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private datepipe: DatePipe) {
private dateUtils: Dates) {
this.route.data.subscribe((data: { officeTemplate: any }) => {
this.officeData = data.officeTemplate;
});
@@ -70,15 +70,19 @@ export class EditOfficeComponent implements OnInit {
* Submits the edit office form.
*/
submit() {
const openedOn: Date = this.officeForm.value.openingDate;
const officeFormData = this.officeForm.value;
const locale = this.settingsService.language.code;
const dateFormat = this.settingsService.dateFormat;
this.officeForm.patchValue({
openingDate: this.datepipe.transform(openedOn, dateFormat)
});
const office = this.officeForm.value;
office.locale = this.settingsService.language.code;
office.dateFormat = dateFormat;
this.organizationService.updateOffice(this.officeData.id, office).subscribe((response: any) => {
const prevOpenedOn: Date = this.officeForm.value.openingDate;
if (officeFormData.openingDate instanceof Date) {
officeFormData.openingDate = this.dateUtils.formatDate(prevOpenedOn, dateFormat);
}
const data = {
...officeFormData,
dateFormat,
locale
};
this.organizationService.updateOffice(this.officeData.id, data).subscribe((response: any) => {
this.router.navigate(['../'], { relativeTo: this.route });
});
}
@@ -50,7 +50,7 @@ export class MultiRowComponent implements OnInit, OnChanges {
/**
* Fetches office Id from parent route params.
* @param {ActivatedRoute} route Activated Route.
* @param {DatePipe} datePipe Date Pipe.
* @param {DatePipe} datePipe Date Utils.
* @param {OrganizationService} organizationService Organization Service.
* @param {SettingsService} settingsService Settings Service.
* @param {MatDialog} dialog Mat Dialog.
@@ -40,7 +40,7 @@ export class SingleRowComponent implements OnInit {
/**
* Fetches office Id from parent route params.
* @param {ActivatedRoute} route Activated Route.
* @param {DatePipe} datePipe Date Pipe.
* @param {DatePipe} datePipe Date Utils.
* @param {OrganizationService} organizationService Organization Service.
* @param {SettingsService} settingsService Settings Service.
* @param {MatDialog} dialog Mat Dialog.
@@ -150,7 +150,7 @@ export class SingleRowComponent implements OnInit {
/**
* Maps API response to data table form fields.
* @param {any} columns Data Table Columns.
* @param {string[]} dateTransformColumns Columns transformed with date pipe.
* @param {string[]} dateTransformColumns Columns transformed with Date Utils.
* @param {any} dataTableEntryObject Additional data table details.
*/
getFormfields(columns: any, dateTransformColumns: string[], dataTableEntryObject: any) {