Full crud with the delete
This commit is contained in:
@@ -2,8 +2,11 @@
|
|||||||
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
|
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
|
||||||
<span class="col col-form-label" *ngIf="loading$ | async">Loading...</span>
|
<span class="col col-form-label" *ngIf="loading$ | async">Loading...</span>
|
||||||
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
|
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
|
||||||
<button type="submit" class="btn btn-default">
|
<button color="primary" type="submit" class="btn btn-default">
|
||||||
{{resource_id === null ? "Create" : "Update"}}
|
{{ this.isCreateForm() ? "Create" : "Update" }}
|
||||||
|
</button>
|
||||||
|
<button *ngIf="!this.isCreateForm()" (click)="onDelete()" color="danger" type="" class="btn btn-default">
|
||||||
|
Delete
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import {Component, Input, OnInit} from '@angular/core';
|
import { Component, Input, OnInit } from '@angular/core';
|
||||||
import { FormGroup } from '@angular/forms';
|
import { FormGroup } from '@angular/forms';
|
||||||
import { FormlyFieldConfig } from '@ngx-formly/core'
|
import { FormlyFieldConfig } from '@ngx-formly/core'
|
||||||
import { FormlyJsonschema } from '@ngx-formly/core/json-schema';
|
import { FormlyJsonschema } from '@ngx-formly/core/json-schema';
|
||||||
import { Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
|
|
||||||
import { CrudService } from '../crud.service'
|
import { CrudService } from '../crud.service'
|
||||||
import {BehaviorSubject} from "rxjs";
|
import { BehaviorSubject } from "rxjs";
|
||||||
import {JsonschemasService} from "../jsonschemas.service";
|
import { JsonschemasService } from "../jsonschemas.service";
|
||||||
|
|
||||||
export interface Model {
|
export interface Model {
|
||||||
email: string;
|
email: string;
|
||||||
@@ -35,7 +35,8 @@ export class CardComponent implements OnInit {
|
|||||||
constructor(private crudService: CrudService,
|
constructor(private crudService: CrudService,
|
||||||
private jsonSchemasService: JsonschemasService,
|
private jsonSchemasService: JsonschemasService,
|
||||||
private formlyJsonschema: FormlyJsonschema,
|
private formlyJsonschema: FormlyJsonschema,
|
||||||
private router: Router
|
private router: Router,
|
||||||
|
private route: ActivatedRoute,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
@@ -70,6 +71,14 @@ export class CardComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onDelete() {
|
||||||
|
this._loading$.next(true);
|
||||||
|
this.crudService.delete(this.resource!, this.model).subscribe((model: any) => {
|
||||||
|
this._loading$.next(false);
|
||||||
|
this.router.navigate(['../'], {relativeTo: this.route});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
isCreateForm() {
|
isCreateForm() {
|
||||||
return this.resource_id === null;
|
return this.resource_id === null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,4 +45,10 @@ export class CrudService extends ApiService {
|
|||||||
model
|
model
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public delete(resource: string, model: any) {
|
||||||
|
return this.http.delete<{ menu: [{}] }>(
|
||||||
|
`${this.api_root}/${resource.toLowerCase()}/${model._id}`
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user