11import * as ng from 'angular2/angular2' ;
22import * as router from 'angular2/router' ;
33import * as models from '../../../models/models' ;
4- import { Http , HTTP_BINDINGS , Headers } from 'angular2/http' ;
4+ import { Http , HTTP_BINDINGS , Headers , Response } from 'angular2/http' ;
55import { AlbumDeletePrompt } from '../album-delete-prompt/album-delete-prompt' ;
66import { FormField } from '../form-field/form-field' ;
77
8- const ContentTypeJson = 'application/json' ;
9-
108@ng . Component ( {
119 selector : 'album-edit'
1210} )
@@ -19,6 +17,7 @@ export class AlbumEdit {
1917 public artists : models . Artist [ ] ;
2018 public genres : models . Genre [ ] ;
2119 public originalAlbum : models . Album ;
20+ public changesSaved : boolean ;
2221
2322 private _http : Http ;
2423
@@ -52,6 +51,10 @@ export class AlbumEdit {
5251 Price : fb . control ( '' , ng . Validators . compose ( [ ng . Validators . required , AlbumEdit . _validatePrice ] ) ) ,
5352 AlbumArtUrl : fb . control ( '' , ng . Validators . required )
5453 } ) ;
54+
55+ this . form . valueChanges . observer ( {
56+ next : _ => { this . changesSaved = false ; }
57+ } ) ;
5558 }
5659
5760 public onSubmitModelBased ( ) {
@@ -63,30 +66,22 @@ export class AlbumEdit {
6366 if ( this . form . valid ) {
6467 var controls = this . form . controls ;
6568 var albumId = this . originalAlbum . AlbumId ;
66- ( < any > window ) . fetch ( `/api/albums/${ albumId } /update` , {
67- method : 'put' ,
68- headers : { 'Content-Type' : ContentTypeJson } ,
69- body : JSON . stringify ( this . form . value )
70- } ) . then ( response => {
71- if ( response . status >= 200 && response . status < 300 ) {
72- // TODO: Display success message
69+
70+ this . _putJson ( `/api/albums/${ albumId } /update` , this . form . value ) . then ( response => {
71+ if ( response . status === 200 ) {
72+ this . changesSaved = true ;
7373 } else {
74- if ( response . headers . get ( 'Content-Type' ) . indexOf ( ContentTypeJson ) === 0 ) {
75- return response . json ( ) . then ( ( responseJson : ValidationResponse ) => {
76- Object . keys ( responseJson . ModelErrors ) . forEach ( key => {
77- responseJson . ModelErrors [ key ] . forEach ( errorMessage => {
78- // TODO: There has to be a better API for this
79- if ( ! this . form . controls [ key ] . errors ) {
80- ( < any > this . form . controls [ key ] ) . _errors = { } ;
81- }
82-
83- this . form . controls [ key ] . errors [ errorMessage ] = true ;
84- } ) ;
85- } ) ;
74+ var errors = ( < ValidationResponse > ( response . json ( ) ) ) . ModelErrors ;
75+ Object . keys ( errors ) . forEach ( key => {
76+ errors [ key ] . forEach ( errorMessage => {
77+ // TODO: There has to be a better API for this
78+ if ( ! this . form . controls [ key ] . errors ) {
79+ ( < any > this . form . controls [ key ] ) . _errors = { } ;
80+ }
81+
82+ this . form . controls [ key ] . errors [ errorMessage ] = true ;
8683 } ) ;
87- } else {
88- // TODO: Display generic 'unknown error'
89- }
84+ } ) ;
9085 }
9186 } ) ;
9287 }
@@ -95,6 +90,14 @@ export class AlbumEdit {
9590 private static _validatePrice ( control : ng . Control ) : { [ key : string ] : boolean } {
9691 return / ^ \d + \. \d + $ / . test ( control . value ) ? null : { Price : true } ;
9792 }
93+
94+ private _putJson ( url : string , body : any ) : Promise < Response > {
95+ return new Promise ( ( resolve , reject ) => {
96+ this . _http . put ( url , JSON . stringify ( body ) , {
97+ headers : new Headers ( { 'Content-Type' : 'application/json' } )
98+ } ) . subscribe ( resolve ) ;
99+ } ) ;
100+ }
98101}
99102
100103interface ValidationResponse {
0 commit comments