[sidebar] Working on adding object
[u/erikhf/frm.git] / src / components / sidebar / sidebar.ts
CommitLineData
c1cf798d 1import {Component, NgFor, NgIf, NgModel, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
9bdb78d0 2import {Http, Headers} from 'angular2/http';
ebe03648 3
ebaf2f58 4declare var zone: Zone;
ebe03648
EHF
5
6@Component({
7 selector: 'mou-sidebar',
9bdb78d0
EHF
8 directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
9 templateUrl: './components/sidebar/sidebar.html',
10 styles: [`
11 .ng-valid[required] {
12 border-left: 5px solid #42A948; /* green */
13 }
14 .ng-invalid {
15 border-left: 5px solid #a94442; /* red */
16 }
17 `]
ebe03648
EHF
18})
19export class Sidebar {
9bdb78d0
EHF
20 http:Http;
21 activeOrgUnit:Object;
22 editmode:boolean;
ebaf2f58 23 active: boolean;
9bdb78d0 24 constructor(http:Http) {
ebe03648 25 this.http = http;
9ce2e2a9 26 this.editmode = false;
ebaf2f58
EHF
27 this.active = false;
28 this.activeOrgUnit = new Object();
c1cf798d
EHF
29 }
30
9bdb78d0 31 update(orgunitId) {
ebaf2f58 32 this.active = true;
c1cf798d
EHF
33 this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
34 .map(res => res.json())
35 .subscribe(res => this.activeOrgUnit = res)
ebe03648 36 }
9ce2e2a9 37
9bdb78d0
EHF
38 onSubmit() {
39 this.editmode = false;
40
41 let headers = new Headers();
42 headers.append('Accept', 'application/json');
43 headers.append('Content-Type', 'application/json');
44
45 if (this.activeOrgUnit.id) {
46 this.http.put(dhisAPI + "/api/organisationUnits/" + this.activeOrgUnit.id, JSON.stringify(this.activeOrgUnit), {
47 headers: headers
48 })
49 .map(res => res.json())
50 .subscribe(res => console.log(res));
51 }
52
ebaf2f58
EHF
53
54
9ce2e2a9 55 }
57c243b6
EHF
56
57 cancel(){
58 this.editmode = false;
59 if(this.activeOrgUnit.id){
60 this.update(this.activeOrgUnit.id);
61 }else{
ebaf2f58 62 this.activeOrgUnit = new Object();
57c243b6
EHF
63 }
64 }
ebaf2f58
EHF
65
66 addData(data){
67
68 this.active = true;
69 this.editmode = true;
70 console.log(data);
71 this.activeOrgUnit = new Object();
72 this.activeOrgUnit.name = "Erik";
73 console.log("faen");
74 zone.afterTask();
75 }
ebe03648
EHF
76}
77