allerta-vvf/frontend/src/app/_routes/edit-service/edit-service.component.ts

272 lines
9.3 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AbstractControl, UntypedFormBuilder, ValidationErrors, Validators } from '@angular/forms';
import { ApiClientService } from 'src/app/_services/api-client.service';
import { AuthService } from 'src/app/_services/auth.service';
2022-01-06 22:14:16 +01:00
import { ToastrService } from 'ngx-toastr';
2022-03-01 00:07:51 +01:00
import { TranslateService } from '@ngx-translate/core';
@Component({
selector: 'app-edit-service',
templateUrl: './edit-service.component.html',
styleUrls: ['./edit-service.component.scss']
})
export class EditServiceComponent implements OnInit {
addingService = false;
2022-01-08 00:41:28 +01:00
serviceId: string | undefined;
loadedService = {
start: '',
end: '',
code: '',
chief: '',
drivers: [],
crew: [],
2023-09-01 13:23:44 +02:00
lat: -1,
lon: -1,
provinceCode: '',
municipalityCode: '',
address: '',
notes: '',
type: ''
};
2022-02-24 23:41:46 +01:00
loadedServiceLat = "";
loadedServiceLng = "";
usingMapSelector = true;
2022-01-08 00:41:28 +01:00
users: any[] = [];
types: any[] = [];
2023-09-01 13:23:44 +02:00
2022-01-08 00:41:28 +01:00
addingType = false;
newType = "";
2022-01-09 14:31:19 +01:00
serviceForm: any;
private formSubmitAttempt: boolean = false;
2022-01-11 13:33:39 +01:00
submittingForm = false;
2022-01-09 14:31:19 +01:00
get start() { return this.serviceForm.get('start'); }
get end() { return this.serviceForm.get('end'); }
get code() { return this.serviceForm.get('code'); }
get chief() { return this.serviceForm.get('chief'); }
get drivers() { return this.serviceForm.get('drivers'); }
get crew() { return this.serviceForm.get('crew'); }
get lat() { return this.serviceForm.get('place.lat'); }
get lon() { return this.serviceForm.get('place.lon'); }
get address() { return this.serviceForm.get('place.address'); }
2022-01-09 14:31:19 +01:00
get type() { return this.serviceForm.get('type'); }
ngOnInit() {
this.serviceForm = this.fb.group({
start: [this.loadedService.start, [Validators.required]],
end: [this.loadedService.end, [Validators.required]],
code: [this.loadedService.code, [Validators.required, Validators.minLength(3)]],
chief: [this.loadedService.chief, [Validators.required]],
drivers: [this.loadedService.drivers, []],
crew: [this.loadedService.crew, [Validators.required]],
place: this.fb.group({
lat: [this.loadedService.lat, this.usingMapSelector ?
[Validators.required, (control: AbstractControl): ValidationErrors | null => {
const valid = control.value >= -90 && control.value <= 90;
return valid ? null : { 'invalidLatitude': { value: control.value } };
}] : []
],
lon: [this.loadedService.lon, this.usingMapSelector ?
[Validators.required, (control: AbstractControl): ValidationErrors | null => {
const valid = control.value >= -180 && control.value <= 180;
return valid ? null : { 'invalidLongitude': { value: control.value } };
}] : []
],
provinceCode: [this.loadedService.provinceCode, this.usingMapSelector ?
[] : [Validators.required, Validators.minLength(3)]],
municipalityCode: [this.loadedService.municipalityCode, this.usingMapSelector ?
[] : [Validators.required, Validators.minLength(3)]],
address: [this.loadedService.address, this.usingMapSelector ?
[] : [Validators.required, Validators.minLength(3)]]
}),
notes: [this.loadedService.notes],
type: [this.loadedService.type, [Validators.required, Validators.minLength(1)]]
2022-01-09 14:31:19 +01:00
});
}
2022-01-06 22:14:16 +01:00
constructor(
private route: ActivatedRoute,
private api: ApiClientService,
public auth: AuthService,
2022-01-08 00:41:28 +01:00
private toastr: ToastrService,
2023-02-21 18:06:05 +01:00
private fb: UntypedFormBuilder,
2022-03-01 00:07:51 +01:00
private translate: TranslateService
2022-01-06 22:14:16 +01:00
) {
this.usingMapSelector = this.auth.profile.getOption("service_place_selection_use_map_picker", true);
this.route.paramMap.subscribe(params => {
this.serviceId = params.get('id') || undefined;
2023-09-01 13:23:44 +02:00
if (this.serviceId === "new") {
this.addingService = true;
} else {
this.api.get(`services/${this.serviceId}`).then((service) => {
this.loadedService = service;
2023-09-01 13:23:44 +02:00
this.loadedServiceLat = service.place.lat;
this.loadedServiceLng = service.place.lon;
this.chief.setValue(service.chief_id);
console.log(service);
let patch = Object.assign({}, service);
2023-09-01 13:23:44 +02:00
patch.start = new Date(patch.start);
patch.end = new Date(patch.end);
patch.chief = patch.chief_id;
patch.drivers = patch.drivers.map((e: any) => e.pivot.user_id+"");
patch.crew = patch.crew.map((e: any) => e.pivot.user_id+"");
patch.type = patch.type_id;
this.serviceForm.patchValue(patch);
2023-09-04 16:22:40 +02:00
}).catch((err) => {
2023-09-07 14:35:29 +02:00
this.translate.get('edit_service.service_load_failed').subscribe((res: string) => {
this.toastr.error(res);
});
});
}
console.log(this.serviceId);
});
this.api.get("list").then((users) => {
this.users = users;
console.log(this.users);
2023-09-04 16:22:40 +02:00
}).catch((err) => {
2023-09-07 14:35:29 +02:00
this.translate.get('edit_service.users_load_failed').subscribe((res: string) => {
this.toastr.error(res);
});
});
2022-01-06 22:14:16 +01:00
this.loadTypes();
}
loadTypes() {
this.api.get("service_types").then((types) => {
console.log(types);
this.types = types;
2023-09-04 16:22:40 +02:00
}).catch((err) => {
2023-09-07 14:35:29 +02:00
this.translate.get('edit_service.types_load_failed').subscribe((res: string) => {
this.toastr.error(res);
});
2022-01-06 22:14:16 +01:00
});
}
2022-01-06 22:14:16 +01:00
addType() {
2023-09-01 13:23:44 +02:00
if (this.newType.length < 2) {
2024-01-08 23:19:37 +01:00
this.translate.get('validation.type_must_be_two_characters_long').subscribe((res: string) => {
2022-03-01 00:07:51 +01:00
this.toastr.error(res);
});
2022-01-06 22:14:16 +01:00
return;
}
2023-09-01 13:23:44 +02:00
if (this.types.find(t => t.name == this.newType)) {
2024-01-08 23:19:37 +01:00
this.translate.get('validation.type_already_exists').subscribe((res: string) => {
2022-03-01 00:07:51 +01:00
this.toastr.error(res);
});
2022-01-06 22:14:16 +01:00
return;
}
this.api.post("service_types", {
name: this.newType
}).then((type) => {
this.addingType = false;
this.newType = "";
console.log(type);
2023-09-01 13:23:44 +02:00
if (type.name) {
2022-03-01 00:07:51 +01:00
this.translate.get('edit_service.type_added_successfully').subscribe((res: string) => {
2023-09-01 13:23:44 +02:00
this.toastr.success(res);
2022-03-01 00:07:51 +01:00
});
this.loadTypes();
}
2023-09-04 16:22:40 +02:00
}).catch((err) => {
2023-09-07 14:35:29 +02:00
this.translate.get('edit_service.type_add_failed').subscribe((res: string) => {
this.toastr.error(res);
});
2022-01-06 22:14:16 +01:00
});
}
2022-01-07 18:37:00 +01:00
2022-01-09 14:31:19 +01:00
onDriversCheckboxChange(event: any) {
if (event.target.checked) {
this.drivers.setValue([...this.drivers.value, event.target.value]);
} else {
this.drivers.setValue(this.drivers.value.filter((x: number) => x !== event.target.value));
}
}
isDriverSelected(id: number) {
return this.drivers.value.find((x: number) => x == id);
}
2022-01-09 14:31:19 +01:00
onCrewCheckboxChange(event: any) {
if (event.target.checked) {
this.crew.setValue([...this.crew.value, event.target.value]);
} else {
this.crew.setValue(this.crew.value.filter((x: number) => x !== event.target.value));
}
}
isCrewSelected(id: number) {
return this.crew.value.find((x: number) => x == id);
}
2022-01-09 14:31:19 +01:00
setPlaceMap(lat: number, lng: number) {
this.lat.setValue(lat);
this.lon.setValue(lng);
console.log("Place selected", lat, lng);
2022-01-09 14:31:19 +01:00
}
setPlace(provinceCode: string, municipalityCode: string, address: string) {
this.serviceForm.get('place.provinceCode').setValue(provinceCode);
this.serviceForm.get('place.municipalityCode').setValue(municipalityCode);
this.address.setValue(address);
console.log("Place selected", provinceCode, municipalityCode, address);
}
2022-01-09 14:31:19 +01:00
//https://loiane.com/2017/08/angular-reactive-forms-trigger-validation-on-submit/
isFieldValid(field: string) {
if(!this.formSubmitAttempt) return true;
if(this.serviceForm.get(field) == null) return false;
return this.serviceForm.get(field).valid;
2022-01-07 18:37:00 +01:00
}
2022-01-08 00:41:28 +01:00
formSubmit() {
console.log("form values", this.serviceForm.value);
2022-01-09 14:31:19 +01:00
this.formSubmitAttempt = true;
2023-09-01 13:23:44 +02:00
if (this.serviceForm.valid) {
2022-01-11 13:33:39 +01:00
this.submittingForm = true;
2022-01-11 14:47:16 +01:00
let values = Object.assign({}, this.serviceForm.value);
2022-01-09 14:31:19 +01:00
values.start = values.start.getTime();
values.end = values.end.getTime();
console.log(values);
2023-09-01 13:23:44 +02:00
if (this.serviceId !== "new") {
values.id = this.serviceId;
this.api.post("services", values).then((res) => {
console.log(res);
this.translate.get('edit_service.service_updated_successfully').subscribe((res: string) => {
2023-09-01 13:23:44 +02:00
this.toastr.success(res);
});
this.submittingForm = false;
}).catch((err) => {
console.error(err);
this.translate.get('edit_service.service_update_failed').subscribe((res: string) => {
2023-09-01 13:23:44 +02:00
this.toastr.error(res);
});
this.submittingForm = false;
2022-03-01 00:07:51 +01:00
});
2023-09-01 13:23:44 +02:00
} else {
this.api.post("services", values).then((res) => {
console.log(res);
this.translate.get('edit_service.service_added_successfully').subscribe((res: string) => {
this.toastr.success(res);
});
this.submittingForm = false;
}).catch((err) => {
console.error(err);
this.translate.get('edit_service.service_add_failed').subscribe((res: string) => {
this.toastr.error(res);
});
this.submittingForm = false;
2022-03-01 00:07:51 +01:00
});
2023-09-01 13:23:44 +02:00
}
2022-01-09 14:31:19 +01:00
}
}
formReset() {
this.formSubmitAttempt = false;
this.serviceForm.reset();
2022-01-08 00:41:28 +01:00
}
}