|
|
@@ -1,7 +1,8 @@
|
|
|
import { Component, OnInit } from '@angular/core';
|
|
|
-import { FormGroup, FormBuilder, Validators } from '@angular/forms';
|
|
|
+import { FormGroup, FormBuilder, Validators, FormArray } from '@angular/forms';
|
|
|
import { UserService } from '@app/services/user.service';
|
|
|
import Swal from 'sweetalert2';
|
|
|
+import { OrganizationsService } from '@app/services/organizations.service';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-new-user',
|
|
|
@@ -12,24 +13,74 @@ export class NewUserComponent implements OnInit {
|
|
|
title:string = "Nuevo usuario";
|
|
|
userForm: FormGroup;
|
|
|
submitted: boolean= false;
|
|
|
+ isLoadingOrganization: boolean;
|
|
|
+ listOrganization: any;
|
|
|
+ selectedHobbiesNames: [string];
|
|
|
|
|
|
|
|
|
- constructor(private userService: UserService, private formBuilder: FormBuilder) {
|
|
|
+ constructor(private userService: UserService, private formBuilder: FormBuilder, private orgService: OrganizationsService) {
|
|
|
}
|
|
|
|
|
|
- ngOnInit() {
|
|
|
+
|
|
|
+ get organizationsFormArr(): FormArray {
|
|
|
+ return this.f && <FormArray>this.f.organizationsFormArr
|
|
|
+ }
|
|
|
+
|
|
|
+ get organizationsFormGroup(): FormGroup {
|
|
|
+ return this.f && <FormGroup>this.f.organizationsFormGroup
|
|
|
+ }
|
|
|
+
|
|
|
+ get organizationsFormGroupSelectedIds(): string[] {
|
|
|
+ let ids: string[] = [];
|
|
|
+ for (var key in this.organizationsFormGroup.controls) {
|
|
|
+ if (this.organizationsFormGroup.controls[key].value) {
|
|
|
+ ids.push(key);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ids = ids.filter(id => id !== key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+
|
|
|
+ ngOnInit() {
|
|
|
+ this.isLoadingOrganization = true;
|
|
|
this.userForm = this.formBuilder.group({
|
|
|
- first_name: ['', Validators.required],
|
|
|
- last_name: ['', Validators.required],
|
|
|
- email: ['', Validators.required],
|
|
|
- role: ['', Validators.required],
|
|
|
+ first_name: [''],
|
|
|
+ last_name: [''],
|
|
|
+ email: [''],
|
|
|
+ role: [''],
|
|
|
});
|
|
|
+
|
|
|
+ this.orgService.getAllOrganizations().subscribe(ans => {
|
|
|
+ this.isLoadingOrganization = false;
|
|
|
+ this.listOrganization = ans["data"]["organizations"];
|
|
|
+ this.userForm.addControl("organizationsFormGroup", this.buildOrganizationFormGroup(this.listOrganization));
|
|
|
+
|
|
|
+ }, (err) => {
|
|
|
+ Swal.fire({
|
|
|
+ type: 'error',
|
|
|
+ title: 'Error en el servidor',
|
|
|
+ text: "No su pudo obtener la informacion"
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- get f() { return this.userForm.controls; }
|
|
|
+ buildOrganizationFormGroup(organizations:any, selectedOrganizationIds: string[] = []): FormGroup {
|
|
|
+ let group = this.formBuilder.group({});
|
|
|
+ organizations.forEach(category => {
|
|
|
+ let isSelected = selectedOrganizationIds.some(id => id === category.id);
|
|
|
+ group.addControl(category.id, this.formBuilder.control(isSelected));
|
|
|
+ })
|
|
|
+ return group;
|
|
|
+ }
|
|
|
|
|
|
+ get f() { return this.userForm.controls; }
|
|
|
+
|
|
|
createUser() {
|
|
|
this.submitted = true;
|
|
|
|
|
|
@@ -37,31 +88,33 @@ export class NewUserComponent implements OnInit {
|
|
|
if (this.userForm.invalid) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- this.userService.createUser(
|
|
|
- {
|
|
|
- first_name: this.f.first_name.value,
|
|
|
- last_name: this.f.last_name.value,
|
|
|
- email: this.f.email.value,
|
|
|
- role: +this.f.role.value,
|
|
|
- }
|
|
|
- )
|
|
|
- .subscribe(success => {
|
|
|
- if (success) {
|
|
|
- Swal.fire({
|
|
|
- allowOutsideClick: false,
|
|
|
- type: 'success',
|
|
|
- showCancelButton: false,
|
|
|
- title: 'Exito',
|
|
|
- confirmButtonText: 'El usuario ha sido creado'
|
|
|
- }).then((result) => {
|
|
|
- if (result.value) {
|
|
|
- window.location.href="#/users";
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
+
|
|
|
+
|
|
|
+ this.userService.createUser(
|
|
|
+ {
|
|
|
+ first_name: this.f.first_name.value,
|
|
|
+ last_name: this.f.last_name.value,
|
|
|
+ email: this.f.email.value,
|
|
|
+ role: +this.f.role.value,
|
|
|
+ organizations: this.organizationsFormGroupSelectedIds
|
|
|
+ }
|
|
|
+ )
|
|
|
+ .subscribe(success => {
|
|
|
+ if (success) {
|
|
|
+ Swal.fire({
|
|
|
+ allowOutsideClick: false,
|
|
|
+ type: 'success',
|
|
|
+ showCancelButton: false,
|
|
|
+ title: 'Exito',
|
|
|
+ confirmButtonText: 'El usuario ha sido creado'
|
|
|
+ }).then((result) => {
|
|
|
+ if (result.value) {
|
|
|
+ window.location.href="#/users";
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
|