diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal/command-modal.component.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal/command-modal.component.ts index 84c892e..4ef50b3 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal/command-modal.component.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/command-modal/command-modal.component.ts @@ -42,6 +42,7 @@ export class CommandModalComponent implements OnInit, OnDestroy { } attemptSave() { + debugger; this.loading = true; this.dataSource.executeCommandByName(this.command, this.commandModel) .pipe( diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts index cec95fb..9bc2866 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/command-modal/directives/command-modal.directive.ts @@ -22,11 +22,13 @@ export class CommandModalDirective { @Input() animated: boolean; @HostListener('click') - wasClicked() { + wasClicked() { + debugger; this.dataSource.resolveCommandModelByName({ command: this.command, model: this.model - }).subscribe(commandModel => { + }).subscribe(commandModel => { + debugger; const initialState = { dataSource: this.dataSource, command: this.command, diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts new file mode 100644 index 0000000..90cfdb2 --- /dev/null +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/directives/form-group-command-modal.directive.ts @@ -0,0 +1,108 @@ +import { Directive, Input, TemplateRef, HostListener, Output, EventEmitter } from '@angular/core'; +import { IDataSource } from '@poweredsoft/data'; +import { BsModalService } from 'ngx-bootstrap/modal'; +import { FormGroupCommandModalComponent } from '../form-group-command-modal/form-group-command-modal.component'; +import { FormGroup } from '@angular/forms'; + +export interface IModelFormCreateEvent +{ + shouldSetCommandModel: boolean; + viewModel: any; + commandName: string; + commandModel: any; + formGroup?: FormGroup; +} + +@Directive({ + selector: '[psbxFormGroupCommandModal]' +}) +export class FormGroupCommandModalDirective { + + + @Input() dataSource: IDataSource; + @Input() command: string; + @Input() model: any; + @Input() template: TemplateRef; + @Input() commandTitle: string; + @Input() animated: boolean; + @Input() refreshOnSuccess: boolean; + @Input() commandText: string; + @Input() cancelText: string; + + @Output() formCreate: EventEmitter = new EventEmitter(); + + constructor(private modalService: BsModalService) { } + + @HostListener('click') + wasClicked() { + this.dataSource.resolveCommandModelByName({ + command: this.command, + model: this.model + }).subscribe(commandModel => { + + + + const event = { + commandName: this.command, + viewModel: this.model, + commandModel: commandModel, + shouldSetCommandModel: true + } + + this.formCreate.emit(event); + + if (event.formGroup == null) + throw new Error('form group should be set, after form createEvent'); + + if (event.shouldSetCommandModel) + event.formGroup.patchValue(commandModel); + + const initialState = { + dataSource: this.dataSource, + command: this.command, + template: this.template, + title: this.commandTitle, + refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, + commandText: this.commandText || 'OK', + cancelText: this.cancelText || 'Cancel', + modelForm: event.formGroup + }; + + this.modalService.show(FormGroupCommandModalComponent, { + animated: this.animated === undefined ? true : this.animated, + initialState + }); + + }, error => { + + }); + } + + // @Input() + // set psbxFormGroupCommandModal(element:HTMLBaseElement){ + // element.addEventListener('click',()=>{ + // this.dataSource.resolveCommandModelByName({ + // command: this.command, + // model: this.model + // }).subscribe(commandModel => { + // const initialState = { + // dataSource: this.dataSource, + // command: this.command, + // commandModel: commandModel, + // template: this.template, + // title: this.commandTitle, + // refreshOnSuccess: this.refreshOnSuccess === undefined ? true : this.refreshOnSuccess, + // commandText: this.commandText || 'OK', + // cancelText: this.cancelText || 'Cancel' + // }; + // this.modalService.show(FormGroupCommandModalComponent, { + // animated: this.animated === undefined ? true : this.animated, + // initialState + // }) + // },error => { + + // }); + // }) + // } + +} diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal.module.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal.module.ts index d7d08b1..cb90bfe 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal.module.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal.module.ts @@ -1,13 +1,20 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormGroupCommandModalComponent } from './form-group-command-modal/form-group-command-modal.component'; +import { FormGroupCommandModalDirective } from './directives/form-group-command-modal.directive'; +import { ModalModule } from 'ngx-bootstrap/modal'; +import { ReactiveFormsModule } from '@angular/forms'; + @NgModule({ imports: [ - CommonModule + CommonModule, + ModalModule.forRoot(), + ReactiveFormsModule ], - declarations: [FormGroupCommandModalComponent] + declarations: [FormGroupCommandModalComponent, FormGroupCommandModalDirective], + exports: [FormGroupCommandModalDirective] }) export class FormGroupCommandModalModule { } diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html index feefece..a509523 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.html @@ -1 +1,31 @@ -

form-group-command-modal works!

+ + + + +
+ + + +
+ {{ errorMessage }} +
+
+ + + + \ No newline at end of file diff --git a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.ts b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.ts index 7ae5720..f5155a7 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/lib/form-group-command-modal/form-group-command-modal/form-group-command-modal.component.ts @@ -1,4 +1,9 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, TemplateRef } from '@angular/core'; +import { BsModalRef } from 'ngx-bootstrap/modal'; +import { IDataSource } from '@poweredsoft/data'; +import { finalize } from 'rxjs/operators'; +import { Subscription } from 'rxjs' +import { FormGroup, FormControl } from '@angular/forms'; @Component({ selector: 'ps-form-group-command-modal', @@ -7,9 +12,66 @@ import { Component, OnInit } from '@angular/core'; }) export class FormGroupCommandModalComponent implements OnInit { - constructor() { } - ngOnInit(): void { + modelForm: FormGroup; + title: string; + template: TemplateRef; + command: string; + dataSource: IDataSource; + refreshOnSuccess: boolean; + loading: boolean; + commandText: string; + cancelText: string; + errorMessage: string; + + private _notifyMessage: Subscription; + private _validationError: Subscription; + + constructor(public modalRef: BsModalRef) { } + + ngOnDestroy(): void { + /* + this._notifyMessage.unsubscribe(); + this._validationError.unsubscribe(); + */ } + ngOnInit(): void { + this.errorMessage = null; + // this._notifyMessage = this.dataSource.notifyMessage$.subscribe(message => { + + // }); + + // this._validationError = this.dataSource.validationError$.subscribe(validatorErrors => { + // console.log(validatorErrors); + // }); + } + + attemptSave() { + + this.errorMessage = null; + if (!this.modelForm.valid) + { + this.errorMessage = 'Form is not valid, please enter all required fields'; + return; + } + + const finalModel = this.modelForm.value; + //this.modelForm.setValue(this.commandModel) + this.loading = true; + this.dataSource.executeCommandByName(this.command, finalModel) + .pipe( + finalize(() => { + this.loading = false; + }) + ) + .subscribe(success => { + if (this.refreshOnSuccess) + this.dataSource.refresh(); + + this.modalRef.hide(); + }, fail => { + // you do not want to close on failure.. so just ignore.. + }); + } } diff --git a/projects/poweredsoft/ngx-bootstrap/src/public-api.ts b/projects/poweredsoft/ngx-bootstrap/src/public-api.ts index 871c113..9c101bf 100644 --- a/projects/poweredsoft/ngx-bootstrap/src/public-api.ts +++ b/projects/poweredsoft/ngx-bootstrap/src/public-api.ts @@ -3,4 +3,6 @@ */ export * from './lib/command-modal/command-modal.module'; -export * from './lib/command-modal/directives/command-modal.directive'; \ No newline at end of file +export * from './lib/command-modal/directives/command-modal.directive'; +export * from './lib/form-group-command-modal/form-group-command-modal.module'; +export * from './lib/form-group-command-modal/directives/form-group-command-modal.directive'; \ No newline at end of file diff --git a/src/app/command-modal-demo/command-modal-demo.module.ts b/src/app/command-modal-demo/command-modal-demo.module.ts index 53c9b27..9f7d907 100644 --- a/src/app/command-modal-demo/command-modal-demo.module.ts +++ b/src/app/command-modal-demo/command-modal-demo.module.ts @@ -3,8 +3,9 @@ import { CommonModule } from '@angular/common'; import { CommandModalDemoComponent } from './command-modal-demo/command-modal-demo.component'; import { CommandModalDemoRoutingModule } from './command-modal-demo-routing.module'; import { DataGridModule } from '@poweredsoft/ngx-cdk-ui'; -import { CommandModalModule } from 'projects/poweredsoft/ngx-bootstrap/src/public-api'; + import {FormsModule} from '@angular/forms'; +import { CommandModalModule } from '@poweredsoft/ngx-bootstrap'; @NgModule({ declarations: [CommandModalDemoComponent], imports: [ diff --git a/src/app/data/services/merchant.service.ts b/src/app/data/services/merchant.service.ts index 97560d0..9ebe7ec 100644 --- a/src/app/data/services/merchant.service.ts +++ b/src/app/data/services/merchant.service.ts @@ -27,7 +27,7 @@ export class MerchantService { (model) => model.id, { page: 1, - pageSize: 50, + pageSize: 150, }, true ); diff --git a/src/app/form-group-modal-demo/form-group-modal-demo.module.ts b/src/app/form-group-modal-demo/form-group-modal-demo.module.ts index 04af0f0..3c1f825 100644 --- a/src/app/form-group-modal-demo/form-group-modal-demo.module.ts +++ b/src/app/form-group-modal-demo/form-group-modal-demo.module.ts @@ -2,13 +2,21 @@ import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormGroupModalDemoComponent } from './form-group-modal-demo/form-group-modal-demo.component'; import { FormGroupModalDemoRoutingModule } from './form-group-modal-demo-routing.module' +import { DataGridModule } from '@poweredsoft/ngx-cdk-ui'; +import { FormGroupCommandModalModule,CommandModalModule } from '@poweredsoft/ngx-bootstrap'; +import { ReactiveFormsModule } from '@angular/forms'; + @NgModule({ declarations: [FormGroupModalDemoComponent], imports: [ CommonModule, - FormGroupModalDemoRoutingModule + FormGroupModalDemoRoutingModule, + DataGridModule, + FormGroupCommandModalModule, + ReactiveFormsModule + ] }) export class FormGroupModalDemoModule { } diff --git a/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.html b/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.html index aaa7d99..8688a9f 100644 --- a/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.html +++ b/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.html @@ -1 +1,36 @@ -

form-group-modal-demo works!

+

This is the demo for form-group modal!

+ + + + + +
ID
+
{{model.id}}
+
+ +
Name
+
{{model.name}}
+
+ +
Address
+
{{model.address}}
+
+ + Actions + + + + +
+ + +
+
+ +
+ +
+
+
+
\ No newline at end of file diff --git a/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.ts b/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.ts index 75c3a9a..773ec0a 100644 --- a/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.ts +++ b/src/app/form-group-modal-demo/form-group-modal-demo/form-group-modal-demo.component.ts @@ -1,4 +1,9 @@ import { Component, OnInit } from '@angular/core'; +import { IDataSource } from '@poweredsoft/data'; +import { IMerchant } from 'src/app/data/services/IMerchant'; +import { MerchantService } from 'src/app/data/services/merchant.service'; +import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms'; +import { IModelFormCreateEvent } from 'projects/poweredsoft/ngx-bootstrap/src/public-api'; @Component({ selector: 'ps-form-group-modal-demo', @@ -6,10 +11,42 @@ import { Component, OnInit } from '@angular/core'; styleUrls: ['./form-group-modal-demo.component.scss'] }) export class FormGroupModalDemoComponent implements OnInit { - - constructor() { } - - ngOnInit(): void { + + createDataSource(): IDataSource { + return this.merchantService.createDataSource(); } + + merchantDataSource: IDataSource; + columns = ['id','name', 'address', 'actions']; + constructor(private merchantService: MerchantService, private fb: FormBuilder) { + this.merchantDataSource = this.createDataSource(); + } + + ngOnInit(): void { + this.merchantDataSource.refresh(); + } + + onFormCreate(event: IModelFormCreateEvent) { + event.shouldSetCommandModel = false; + event.formGroup = this.fb.group({ + 'name': [event.commandModel.name, Validators.required] + }); + } + + + newMerchant(name: string) { + this.merchantDataSource.executeCommandByName('addMerchant', { + name: name + }).subscribe( + res => { + alert('it worked!'); + this.merchantDataSource.refresh(); + }, + err => { + console.log(err); + alert('failed'); + } + ); + } }