-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from AmpersandTarski/feat/forms-michiel-liwei
Feat/forms michiel liwei
- Loading branch information
Showing
11 changed files
with
121 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 13 additions & 29 deletions
42
src/app/shared/atomic-components/atomic-alphanumeric/atomic-alphanumeric.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,31 @@ | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { BaseAtomicComponent } from '../BaseAtomicComponent.class'; | ||
import { FormControl } from '@angular/forms'; | ||
import { debounceTime, distinctUntilChanged, map, tap } from 'rxjs'; | ||
import { Resource } from '../../interfacing/resource.interface'; | ||
|
||
@Component({ | ||
selector: 'app-atomic-alphanumeric', | ||
templateUrl: './atomic-alphanumeric.component.html', | ||
styleUrls: ['./atomic-alphanumeric.component.css'], | ||
}) | ||
export class AtomicAlphanumericComponent extends BaseAtomicComponent<string> implements OnInit { | ||
// make generic and put in BaseAtomicComponent class | ||
formControl!: FormControl<string | null>; | ||
newItemControl: FormControl<string> = new FormControl<string>('', { nonNullable: true, updateOn: 'blur' }); | ||
|
||
@Input() | ||
// The type of the T for Resource<T> is not relevant nor to determine here; therefore unknow | ||
// We require a Resource, that implements the required methods (like patch) | ||
// Most likely this is a top-level component for a specific application interface (e.g. ProjectComponent) | ||
resource!: Resource<unknown>; // TODO move to the base class. It is needed for all atomic components | ||
|
||
@Input() | ||
propertyName!: string; // TODO move to the base class. It is needed for all atomic components | ||
|
||
override ngOnInit(): void { | ||
super.ngOnInit(); | ||
this.formControl = new FormControl(this.data[0], { nonNullable: false, updateOn: 'blur' }); | ||
|
||
this.formControl.valueChanges | ||
.pipe( | ||
debounceTime(300), | ||
distinctUntilChanged(), | ||
map((x) => (x === '' ? null : x)), // transform empty string to null value | ||
) | ||
.subscribe((x) => | ||
this.resource | ||
.patch([ | ||
{ | ||
op: 'replace', | ||
path: this.propertyName, // FIXME: this must be relative to path of this.resource | ||
value: x, | ||
}, | ||
]) | ||
.subscribe(), | ||
); | ||
this.formControl.valueChanges.subscribe((x) => | ||
this.resource | ||
.patch([ | ||
{ | ||
op: 'replace', | ||
path: this.propertyName, // FIXME: this must be relative to path of this.resource | ||
value: x, | ||
}, | ||
]) | ||
.subscribe(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
src/app/shared/atomic-components/atomic-bigalphanumeric/atomic-bigalphanumeric.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,32 @@ | ||
import { Component } from '@angular/core'; | ||
import { Component, OnInit } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { BaseAtomicComponent } from '../BaseAtomicComponent.class'; | ||
|
||
@Component({ | ||
selector: 'app-atomic-bigalphanumeric', | ||
templateUrl: './atomic-bigalphanumeric.component.html', | ||
styleUrls: ['./atomic-bigalphanumeric.component.css'], | ||
}) | ||
export class AtomicBigalphanumericComponent extends BaseAtomicComponent<string> { | ||
newItem!: string; | ||
export class AtomicBigalphanumericComponent extends BaseAtomicComponent<string> implements OnInit { | ||
formControl!: FormControl<string | null>; | ||
newItemControl: FormControl<string> = new FormControl<string>('', { nonNullable: true, updateOn: 'blur' }); | ||
|
||
// TODO: make FormGroup for when !isUni | ||
|
||
override ngOnInit(): void { | ||
super.ngOnInit(); | ||
this.formControl = new FormControl(this.data[0], { nonNullable: false, updateOn: 'blur' }); | ||
|
||
this.formControl.valueChanges.subscribe((x) => | ||
this.resource | ||
.patch([ | ||
{ | ||
op: 'replace', | ||
path: this.propertyName, // FIXME: this must be relative to path of this.resource | ||
value: x, | ||
}, | ||
]) | ||
.subscribe(), | ||
); | ||
} | ||
} |
7 changes: 1 addition & 6 deletions
7
src/app/shared/atomic-components/atomic-boolean/atomic-boolean.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 29 additions & 3 deletions
32
src/app/shared/atomic-components/atomic-boolean/atomic-boolean.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
import { Component, EventEmitter, Output } from '@angular/core'; | ||
import { Component, EventEmitter, OnInit, Output } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { BaseAtomicComponent } from '../BaseAtomicComponent.class'; | ||
|
||
@Component({ | ||
selector: 'app-atomic-boolean', | ||
templateUrl: './atomic-boolean.component.html', | ||
styleUrls: ['./atomic-boolean.component.css'], | ||
}) | ||
export class AtomicBooleanComponent extends BaseAtomicComponent<boolean> { | ||
export class AtomicBooleanComponent extends BaseAtomicComponent<boolean> implements OnInit { | ||
@Output() state = new EventEmitter(); | ||
|
||
getState() { | ||
formControl!: FormControl<boolean | null>; | ||
newItemControl: FormControl<boolean> = new FormControl<boolean>(false, { | ||
nonNullable: true, | ||
updateOn: 'change', | ||
}); | ||
|
||
override ngOnInit(): void { | ||
super.ngOnInit(); | ||
this.formControl = new FormControl(this.data[0], { nonNullable: false, updateOn: 'blur' }); | ||
if (!this.canUpdate()) { | ||
this.formControl.disable(); | ||
} | ||
this.formControl.valueChanges.subscribe((x) => | ||
this.resource | ||
.patch([ | ||
{ | ||
op: 'replace', | ||
path: this.propertyName, // FIXME: this must be relative to path of this.resource | ||
value: x, | ||
}, | ||
]) | ||
.subscribe(), | ||
); | ||
} | ||
|
||
public getState() { | ||
this.state.emit(this.property); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 25 additions & 5 deletions
30
src/app/shared/atomic-components/atomic-date/atomic-date.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,40 @@ | ||
import { DatePipe } from '@angular/common'; | ||
import { Component, Input } from '@angular/core'; | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { BaseAtomicComponent } from '../BaseAtomicComponent.class'; | ||
|
||
@Component({ | ||
selector: 'app-atomic-date', | ||
templateUrl: './atomic-date.component.html', | ||
styleUrls: ['./atomic-date.component.css'], | ||
}) | ||
export class AtomicDateComponent extends BaseAtomicComponent<string> { | ||
export class AtomicDateComponent extends BaseAtomicComponent<string> implements OnInit { | ||
// Possible formats can be found at https://www.primefaces.org/primeng/calendar. | ||
// Scroll down to DateFormat for the documentation | ||
@Input() format: string = 'yy-mm-dd'; | ||
|
||
public onDateChange(date: Date): void { | ||
console.log(date); | ||
formControl!: FormControl<string | null>; | ||
newItemControl: FormControl<string> = new FormControl<string>('', { nonNullable: true, updateOn: 'change' }); | ||
|
||
override ngOnInit(): void { | ||
super.ngOnInit(); | ||
this.formControl = new FormControl(this.data[0], { nonNullable: false, updateOn: 'change' }); | ||
|
||
this.formControl.valueChanges.subscribe((x) => | ||
this.resource | ||
.patch([ | ||
{ | ||
op: 'replace', | ||
path: this.propertyName, // FIXME: this must be relative to path of this.resource | ||
value: this.formatDate(x!), | ||
}, | ||
]) | ||
.subscribe(), | ||
); | ||
} | ||
|
||
public formatDate(date: string): string { | ||
let datePipe: DatePipe = new DatePipe('en-US'); | ||
this.property = datePipe.transform(date, 'yyyy-MM-dd'); | ||
return datePipe.transform(date, 'yyyy-MM-dd')!; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export interface PatchReplace { | ||
op: 'replace'; | ||
path: string; | ||
value: string | null; | ||
// make generic | ||
value: string | boolean | null; | ||
} |