added valid method and abstract save method in base form component

This commit is contained in:
Valentin 2022-01-24 13:07:03 +02:00
parent 54d460682a
commit 3d50996b2e
2 changed files with 34 additions and 22 deletions

View File

@ -1,14 +1,24 @@
import { Directive } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { hasFormChanged } from '@iqser/common-ui';
import { AutoUnsubscribe, hasFormChanged } from '@iqser/common-ui';
@Directive()
export class BaseFormComponent {
export abstract class BaseFormComponent extends AutoUnsubscribe {
form!: FormGroup;
initialFormValue;
get changed() {
constructor() {
super();
}
get changed(): boolean {
return hasFormChanged(this.form, this.initialFormValue);
}
get valid(): boolean {
return this.form?.valid;
}
abstract save(): Promise<void>;
}

View File

@ -38,30 +38,32 @@ export function trackBy<T extends ITrackable>() {
export function hasFormChanged(form: FormGroup, initialFormValue): boolean {
for (const key of Object.keys(form.getRawValue())) {
const initialValue = initialFormValue[key];
const updatedValue = form.get(key).value;
if (form && initialFormValue) {
for (const key of Object.keys(form.getRawValue())) {
const initialValue = initialFormValue[key];
const updatedValue = form.get(key).value;
if (initialValue == null && updatedValue != null) {
const updatedValueType = typeof updatedValue;
if (updatedValueType !== 'string' && updatedValueType !== 'boolean') {
return true;
} else if (updatedValueType === 'string' && updatedValue.length > 0) {
return true;
} else if (updatedValueType === 'boolean' && updatedValue === true) {
return true;
}
} else if (initialValue !== updatedValue) {
if (Array.isArray(updatedValue)) {
if (JSON.stringify(initialValue) !== JSON.stringify(updatedValue)) {
if (initialValue == null && updatedValue != null) {
const updatedValueType = typeof updatedValue;
if (updatedValueType !== 'string' && updatedValueType !== 'boolean') {
return true;
} else if (updatedValueType === 'string' && updatedValue.length > 0) {
return true;
} else if (updatedValueType === 'boolean' && updatedValue === true) {
return true;
}
} else if (updatedValue instanceof moment) {
if (!moment(updatedValue).isSame(moment(initialValue))) {
} else if (initialValue !== updatedValue) {
if (Array.isArray(updatedValue)) {
if (JSON.stringify(initialValue) !== JSON.stringify(updatedValue)) {
return true;
}
} else if (updatedValue instanceof moment) {
if (!moment(updatedValue).isSame(moment(initialValue))) {
return true;
}
} else {
return true;
}
} else {
return true;
}
}
}