]> git.uio.no Git - u/erikhf/frm.git/blob - src/components/sidebar/sidebar.ts
[sidebar] Functionality to cancel editing
[u/erikhf/frm.git] / src / components / sidebar / sidebar.ts
1 import {Component, NgFor, NgIf, NgModel, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
2 import {Http, Headers} from 'angular2/http';
3
4
5 @Component({
6     selector: 'mou-sidebar',
7     directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
8     templateUrl: './components/sidebar/sidebar.html',
9     styles: [`
10         .ng-valid[required] {
11             border-left: 5px solid #42A948; /* green */
12         }
13         .ng-invalid {
14             border-left: 5px solid #a94442; /* red */
15         }
16     `]
17 })
18 export class Sidebar {
19     http:Http;
20     activeOrgUnit:Object;
21     editmode:boolean;
22     submitted = false;
23
24     constructor(http:Http) {
25         this.http = http;
26         this.editmode = false;
27     }
28
29     update(orgunitId) {
30         this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
31             .map(res => res.json())
32             .subscribe(res => this.activeOrgUnit = res)
33     }
34
35     onSubmit() {
36         this.editmode = false;
37
38         let headers = new Headers();
39         headers.append('Accept', 'application/json');
40         headers.append('Content-Type', 'application/json');
41
42         if (this.activeOrgUnit.id) {
43             this.http.put(dhisAPI + "/api/organisationUnits/" + this.activeOrgUnit.id, JSON.stringify(this.activeOrgUnit), {
44                     headers: headers
45                 })
46                 .map(res => res.json())
47                 .subscribe(res => console.log(res));
48         }
49
50     }
51
52     cancel(){
53         this.editmode = false;
54         if(this.activeOrgUnit.id){
55             this.update(this.activeOrgUnit.id);
56         }else{
57             this.activeOrgUnit = null;
58         }
59     }
60 }
61