add post method

This commit is contained in:
Zuhdan Ubay 2020-05-19 23:41:00 +07:00
parent 436db5333b
commit 02368778a4
3 changed files with 24 additions and 10 deletions

View file

@ -5,7 +5,7 @@
<nb-card-body>
<ng2-smart-table [settings]="settings" [source]="source" (deleteConfirm)="onDeleteConfirm($event)"
(userRowSelect)="openWindowForm($event)" (createConfirm)="onCreateConfirm($event)">
(createConfirm)="onCreateConfirm($event)" (userRowSelect)="openWindowForm($event)">
</ng2-smart-table>
</nb-card-body>
</nb-card>

View file

@ -16,7 +16,7 @@ export class PromotionComponent implements OnInit {
addButtonContent: '<i class="nb-plus"></i>',
createButtonContent: '<i class="nb-checkmark"></i>',
cancelButtonContent: '<i class="nb-close"></i>',
createConfirm: true,
confirmCreate: true,
},
edit: {
editButtonContent: '<i class="nb-edit"></i>',
@ -59,9 +59,10 @@ export class PromotionComponent implements OnInit {
});
}
onCreateConfirm(event): void {
onCreateConfirm(event) {
if (window.confirm('Are you sure you want to save?')) {
event.confirm.resolve();
this.service.postPromotion(event.newData).subscribe();
event.confirm.resolve(event.newData);
} else {
event.confirm.reject();
}

View file

@ -1,8 +1,8 @@
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable, throwError} from 'rxjs';
import {map, catchError} from 'rxjs/operators';
import {HttpErrorResponse} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpErrorResponse } from '@angular/common/http';
import { PromotionList } from '../../@core/data/promotion';
@Injectable({
@ -13,13 +13,26 @@ export class PromotionService {
}
getPromotion(): Observable<any> {
const url = 'http://34.87.6.140:8011/api/promotions/all';
const url = 'http://localhost:8011/api/promotions/all';
return this.http.get(url).pipe(
map(this.extractData),
catchError(this.handleError),
);
}
postPromotion(data: PromotionList): Observable<any> {
console.log(JSON.stringify(data));
let headers = new HttpHeaders({
'Content-Type': 'application/json',
'X-Requested-Method': 'POST',
});
let options = { headers: headers };
const url = 'http://localhost:8011/api/promotions/add';
return this.http.post(url, JSON.stringify(data), options).pipe(
catchError(this.handleError),
);
}
private extractData(body: any): PromotionList[] {
return Object.assign(body.promotionList);
}