@@ -7,6 +7,7 @@ import { APP_BASE_HREF } from '@angular/common';
77
88import { Http , URLSearchParams } from '@angular/http' ;
99import { ORIGIN_URL } from '../../shared/constants/baseurl.constants' ;
10+ import { TransferHttp } from '../../../modules/transfer-http/transfer-http' ;
1011
1112@Component ( {
1213 selector : 'fetchdata' ,
@@ -33,16 +34,24 @@ export class UsersComponent implements OnInit {
3334
3435 // Use "constructor"s only for dependency injection
3536 constructor (
36- private http : Http ,
37+ private transferHttp : TransferHttp , // Use only for GETS that you want re-used between Server render -> Client render
38+ private http : Http , // Use for everything else
3739 @Inject ( ORIGIN_URL ) private baseUrl : string
3840 ) { }
3941
4042 // Here you want to handle anything with @Input ()'s @Output()'s
4143 // Data retrieval / etc - this is when the Component is "ready" and wired up
4244 ngOnInit ( ) {
43- this . newUserName = "" ;
44- this . http . get ( `${ this . baseUrl } /api/user/all` ) . map ( res => res . json ( ) ) . subscribe ( result => {
45- console . log ( result ) ;
45+
46+ this . newUserName = '' ;
47+
48+ // ** TransferHttp example / concept **
49+ // - Here we make an Http call on the server, save the result on the window object and pass it down with the SSR,
50+ // The Client then re-uses this Http result instead of hitting the server again!
51+
52+ // NOTE : transferHttp also automatically does .map(res => res.json()) for you, so no need for these calls
53+ this . transferHttp . get ( `${ this . baseUrl } /api/user/all` ) . subscribe ( result => {
54+ console . log ( 'TransferHttp [GET] /api/user/allresult' , result ) ;
4655 this . users = result as IUser [ ] ;
4756 } ) ;
4857 }
@@ -52,9 +61,8 @@ export class UsersComponent implements OnInit {
5261 if ( result . ok ) {
5362 let position = this . users . indexOf ( user ) ;
5463 this . users . splice ( position , 1 ) ;
55- }
56- else {
57- alert ( "There was an issue, Could not delete user" ) ;
64+ } else {
65+ alert ( 'There was an issue, Could not delete user' ) ;
5866 }
5967 } ) ;
6068 }
@@ -65,8 +73,9 @@ export class UsersComponent implements OnInit {
6573 urlSearchParams . append ( 'name' , user . name ) ;
6674
6775 this . http . put ( `${ this . baseUrl } /api/user/update` , urlSearchParams ) . subscribe ( result => {
68- if ( ! result . ok ) {
69- alert ( "There was an issue, Could not edit user" ) ;
76+ console . log ( 'result: ' , result ) ;
77+ if ( ! result ) {
78+ alert ( 'There was an issue, Could not edit user' ) ;
7079 }
7180 } ) ;
7281 }
@@ -76,12 +85,11 @@ export class UsersComponent implements OnInit {
7685 urlSearchParams . append ( 'name' , newUserName ) ;
7786
7887 this . http . post ( `${ this . baseUrl } /api/user/insert` , urlSearchParams ) . subscribe ( result => {
79- if ( result . ok ) {
88+ if ( result ) {
8089 this . users . push ( result . json ( ) ) ;
81- this . newUserName = "" ;
82- }
83- else {
84- alert ( "There was an issue, Could not edit user" ) ;
90+ this . newUserName = '' ;
91+ } else {
92+ alert ( 'There was an issue, Could not edit user' ) ;
8593 }
8694 } ) ;
8795 }
0 commit comments