File

src/app/components/admin/create-user-form/create-user-form.component.ts

Metadata

selector app-create-user-form
styleUrls create-user-form.component.css
templateUrl create-user-form.component.html

Constructor

constructor(apiService: any, router: Router)

Methods

onUserSubmit
onUserSubmit()
Returns: void

Properties

createUserForm
createUserForm: FormGroup<{ username: any; role: any; }, {}>
isLoading
isLoading: boolean
Default value: false
roles
roles: string[]
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ApiService } from 'src/app/core/services/admin/api.admin.service';
import { User } from 'src/app/core/models/user.model';
import { Router } from '@angular/router';
import { Message } from '@app/core/models/message.model';


@Component({
  selector: 'app-create-user-form',
  templateUrl: './create-user-form.component.html',
  styleUrls: ['./create-user-form.component.css']
})
export class CreateUserFormComponent {

  constructor(
    private apiService:ApiService, 
    private router:Router,
    ){
  }

  roles = [
    "student",
    "professor"
  ]
  
  createUserForm = new FormGroup({
    username: new FormControl('', [
      Validators.required
    ]),
    role: new FormControl(this.roles[0], [
      Validators.required
    ]),
  })
  isLoading = false

  onUserSubmit() {
    if(this.createUserForm.get("username")?.invalid || this.createUserForm.get("role")?.invalid ) {
      return;
    }
    
    this.isLoading = true
    const username = this.createUserForm.value.username;
    const role = this.createUserForm.value.role;
    console.log(role)
    const newUser = new User(username!, role!);
    this.apiService
    .createUser(newUser)
    .subscribe({
      next: (msg: Message) => {
        alert(msg.message); 
        this.isLoading = false;
        this.router.navigate(["/users"])
      },
      error: (msg) => {
        alert(msg)
        this.isLoading = false;
      }
    });
  }
}

results matching ""

    No results matching ""