[bugfix] If "featureType" is not present in PUT request, it will be set to "NONE...
[u/erikhf/frm.git] / src / components / sidebar / sidebar.ts
1 import {Component, NgFor, NgIf, EventEmitter, NgModel, Control, ControlGroup, ControlArray, Validators, FormBuilder, CORE_DIRECTIVES,FORM_DIRECTIVES} from 'angular2/angular2';
2 import {Http, Headers} from 'angular2/http';
3
4 declare var zone: Zone;
5
6 @Component({
7     selector: 'mou-sidebar',
8     directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgFor, NgModel, NgIf],
9     events: ['tempmarker'],
10     templateUrl: './components/sidebar/sidebar.html',
11     styles: [`
12         .ng-valid.ng-dirty {
13             border-left: 5px solid #42A948; /* green */
14         }
15         .ng-invalid {
16             border-left: 5px solid #a94442; /* red */
17         }
18         .form-background {
19             padding: 10px;
20             background: white;
21             width: 100%;
22             float: right;
23             height: 100vh;
24             z-index: 5;
25         }
26     `]
27 })
28
29 export class Sidebar {
30
31     form: ControlGroup;
32     http:Http;
33     newObject: boolean;
34     editmode:boolean;
35     active: boolean;
36     coordinatePoint: boolean;
37
38     id: Control = new Control("");
39     name: Control = new Control("", Validators.required);
40     shortName: Control = new Control("",Validators.required);
41     description: Control = new Control("");
42     code: Control = new Control("");
43     openingDate: Control = new Control("",Validators.required);
44     closedDate: Control = new Control("");
45     url: Control = new Control("");
46     lat: Control = new Control("");
47     lng: Control = new Control("");
48     parent: Control = new Control("");
49     contactPerson: Control = new Control("");
50     address: Control = new Control("");
51     email: Control = new Control("");
52     phoneNumber: Control = new Control("");
53     featureType: Control = new Control("");
54
55
56     constructor(http:Http, fb: FormBuilder) {
57         this.http = http;
58         this.editmode = false;
59         this.active = false;
60         this.coordinatePoint = false;
61         this.tempmarker = new EventEmitter();
62
63         this.form = fb.group({
64             "id": this.id,
65             "name": this.name,
66             "shortName": this.shortName,
67             "description": this.description,
68             "code": this.code,
69             "openingDate": this.openingDate,
70             "closedDate": this.closedDate,
71             "url": this.url,
72             "lat": this.lat,
73             "lng": this.lng,
74             "parent": this.parent,
75             "contactPerson": this.contactPerson,
76             "address": this.address,
77             "email": this.email,
78             "phoneNumber": this.phoneNumber,
79             "featureType": this.featureType
80         });
81         let instance = this;
82         this.lat.valueChanges.observer({
83             next: (value) => {
84                 if(instance.lng.value && value) {
85                     let pos = {lat: value, lng: instance.lng.value};
86                     this.tempmarker.next(pos);
87                 }
88             }
89         });
90         this.lng.valueChanges.observer({
91             next: (value) => {
92                 if(instance.lat.value && value) {
93                     let pos = {lat: instance.lat.value, lng: value};
94                     this.tempmarker.next(pos);
95                 }
96             }
97         });
98     }
99
100     update(orgunitId) {
101         console.log("Skjer det noe her? ");
102         this.active = true;
103         this.newObject = false;
104         this.http.get(dhisAPI + "/api/organisationUnits/" + orgunitId)
105             .map(res => res.json())
106             .subscribe(res => this.updateValues(res))
107     }
108
109     updateValues(res){
110
111         for(control in this.form.controls){
112             if(res[control] !== undefined) {
113                 this.form.controls[control].updateValue(res[control]);
114             }
115             else
116                 this.form.controls[control].updateValue("");
117         }
118
119         if(res.featureType === "POINT"){
120             this.coordinatePoint = true;
121             let coord = new Object();
122             coord = JSON.parse(res["coordinates"]);
123             this.form.controls.lat.updateValue(coord[0]);
124             this.form.controls.lng.updateValue(coord[1]);
125         }
126         else{
127             this.coordinatePoint = false;
128         }
129     }
130
131
132
133     onSubmit() {
134         this.editmode = false;
135
136         let headers = new Headers();
137         headers.append('Accept', 'application/json');
138
139         headers.append('Content-Type', 'application/json');
140
141         let jsonObject = this.form.value;
142
143         $.each(jsonObject, function(key, value){
144             if (value === "" || value === null){
145                 delete jsonObject[key];
146             }
147         });
148
149
150
151         console.log(this.form.value);
152
153         if (this.newObject) {
154             jsonObject.parent = {};
155             jsonObject.parent.id = this.form.controls.parent.value;
156             jsonObject.featureType="POINT";
157             jsonObject.coordinates="[" + this.form.controls.lat.value + ","+this.form.controls.lng.value+"]";
158             delete jsonObject["lat"];
159             delete jsonObject["lng"];
160             this.http.post(dhisAPI + "/api/organisationUnits/", JSON.stringify(jsonObject), {
161                     headers: headers
162                 })
163                 .map(res => res.json())
164                 .subscribe(res => console.log(res));
165         }else {
166             this.http.put(dhisAPI + "/api/organisationUnits/" + this.form.controls.id.value, JSON.stringify(jsonObject), {
167                     headers: headers
168                 })
169                 .map(res => res.json())
170                 .subscribe(res => console.log(res));
171         }
172
173     }
174
175     cancel(){
176         this.editmode = false;
177     }
178
179
180     add(data){
181         this.coordinatePoint = true;
182         this.newObject=true;
183         this.active = true;
184         this.editmode = true;
185
186         for(control in this.form.controls){
187             this.form.controls[control].updateValue("");
188         }
189
190         this.form.controls.lat.updateValue(data.location.lat);
191         this.form.controls.lng.updateValue(data.location.lng);
192         this.form.controls.parent.updateValue(data.parent);
193
194     }
195 }
196