added valid method and abstract save method in base form component
This commit is contained in:
parent
54d460682a
commit
3d50996b2e
@ -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>;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user