-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathedit-field.component.ts
More file actions
210 lines (192 loc) · 6.28 KB
/
edit-field.component.ts
File metadata and controls
210 lines (192 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import {
Component,
OnInit,
TemplateRef,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import { HeaderService } from 'src/app/service/header.service';
import { HeaderConfig } from 'src/app/models/HeaderConfig.interface';
import { FormArray, FormControl, FormGroup, Validators } from '@angular/forms';
import { formatDate, Location } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { FieldDataService } from 'src/app/service/FieldDataService';
import { Guid } from 'guid-typescript';
import { SENSORS_MOCK_DATA } from './../../sensors/sensor-data';
import { Field } from 'src/app/models/Field';
import { MatLegacyDialog as MatDialog } from '@angular/material/legacy-dialog';
import { MatLegacySnackBar as MatSnackBar } from '@angular/material/legacy-snack-bar';
import { SensorListComponent } from './../sensor-list/sensor-list.component';
import { CropDataService } from 'src/app/service/CropDataService';
import { forkJoin, from } from 'rxjs';
import {CropInfoResp} from "../../../../models/api/CropInfoResp";
@Component({
selector: 'app-edit-field',
templateUrl: './edit-field.component.html',
styleUrls: ['./edit-field.component.scss'],
})
export class EditFieldComponent implements OnInit {
@ViewChild('dialogTemplate') dialogTemplate: TemplateRef<any>;
headerConfig: HeaderConfig = {
headerTitle: 'Edit Field',
leftIconName: 'arrow_back',
rightIconName: 'save',
leftBtnClick: this.handleLeftClick.bind(this),
rightBtnClick: this.save.bind(this),
};
id: string;
fieldForm: FormGroup;
fieldDetails: any;
sensorsData = SENSORS_MOCK_DATA;
sensors: any[] = [];
cropsList;
cropValue;
progress: boolean = false;
constructor(
private headerService: HeaderService,
private location: Location,
private router: Router,
private route: ActivatedRoute,
private fieldService: FieldDataService,
private cropService: CropDataService,
public dialog: MatDialog,
private _snackBar: MatSnackBar
) {}
ngOnInit(): void {
this.headerService.updateHeader(this.headerConfig);
this.loadForm();
this.loadCropData();
this.id = this.route.snapshot.queryParamMap.get('id');
this.getFieldDetails(this.id);
}
loadForm() {
this.fieldForm = new FormGroup({
fieldName: new FormControl(null, [Validators.required]),
description: new FormControl(null),
crop: new FormControl(null, [Validators.required]),
plantDate: new FormControl(null, [Validators.required]),
cropSelect: new FormControl(),
});
const cropForm = this.fieldForm.get('crop');
cropForm.disable();
}
loadCropData() {
this.progress = true;
const cropForm = this.fieldForm.get('crop');
forkJoin({
cropsListData: this.cropService.getCropListFromApi(),
myCrops: from(this.cropService.getLocalStorageMyCrops()),
}).subscribe(
(results) => {
const cropsListData = results.cropsListData;
this.cropsList = cropsListData;
cropForm.enable();
this.progress = false;
},
(error) => {
alert('Could not get crop list: ' + error);
cropForm.enable();
this.progress = false;
}
);
}
public handleLeftClick(data: string) {
if (!this.fieldForm.dirty) {
this.openDialog(this.dialogTemplate);
} else {
this.location.back();
}
}
openDialog(dialogTemplate: TemplateRef<any>): void {
this.dialog.open(dialogTemplate, {
height: '300px',
width: '400px',
});
}
openCropDialog(dialogTemplate: TemplateRef<any>): void {
this.dialog.open(dialogTemplate, {
height: '430px',
width: '400px',
});
}
clickCropNext() {
this.cropService.getCropInfo(this.cropValue.id).subscribe(
(resp: CropInfoResp)=>{
this.cropValue.id = resp.data.docs[0]._id;
this.cropValue.cropName = resp.data.docs[0].cropName;
this.cropValue.facts = resp.data.docs[0];
},
(error) =>{
alert('clickCropNext Could not get crop info: ' + error);
console.error('clickCropNext Error getting CropInfo:', error);
}
);
this.fieldForm.patchValue({
crop: this.fieldForm.get('cropSelect').value.cropName,
});
this.cropValue = this.fieldForm.get('cropSelect').value;
}
backedClicked() {
this.location.back();
}
public patchFieldValue() {
this.fieldForm.patchValue({
fieldName: this.fieldDetails.fieldName,
description: this.fieldDetails.description,
crop: this.fieldDetails.crop.cropName,
plantDate: this.fieldDetails.plantDate,
});
this.sensors = this.fieldDetails.sensors;
this.cropValue = this.fieldDetails.crop;
}
public async getFieldDetails(id: string) {
this.fieldDetails = await this.fieldService.getFieldFromMyFieldById(id);
this.patchFieldValue();
}
public openFullViewDialog(): void {
const dialogRef = this.dialog.open(SensorListComponent, {
width: '80%',
height: '50%',
});
dialogRef.afterClosed().subscribe((result) => {
if (result) {
const data = this.sensorsData.find((item) => item.id === result);
this.sensors.push(data);
}
});
}
public removeSensor(id: String) {
const data = this.sensors.find((item) => item.id === id);
const index = this.sensors.indexOf(data);
this.sensors.splice(index, 1);
}
public save() {
if (!this.fieldForm.valid) {
this._snackBar.open('Please Fill up the Form', 'Ok', {
duration: 3000,
});
return;
}
let formattedDate, description;
const name = this.fieldForm.get('fieldName').value;
if (this.fieldForm.get('description').value) {
description = this.fieldForm.get('description').value;
}
const crop = this.fieldForm.get('crop').value;
const plantDateValue = this.fieldForm.get('plantDate').value;
formattedDate = formatDate(plantDateValue, 'yyyy-MM-dd', 'en-US');
const sensorList = this.sensors;
const id = this.id;
this.cropValue.seedingDate = new Date(formattedDate);
const params: Field = {
id,
fieldName: name,
description: description || undefined,
crop: this.cropValue,
plantDate: new Date(formattedDate),
sensors: sensorList,
};
this.fieldService.storeFieldsInLocalStorage(params);
this.router.navigate([`/dashboard/fields`]);
}
}