File

src/app/signup/signup.component.ts

Implements

OnInit

Metadata

Index

Properties
Methods

Constructor

constructor(db: DbConnectionService, route: Router, image: ImageService)
Parameters :
Name Type Optional
db DbConnectionService No
route Router No
image ImageService No

Methods

fileSelected
fileSelected(ev)
Parameters :
Name Optional
ev No
Returns : void
ngOnInit
ngOnInit()
Returns : void
signUp
signUp()
Returns : void

Properties

error
Type : string
Default value : ""
fileName
Type : string
form
Type : UntypedFormGroup
imgError
Type : string
imgSrc
Type : string
import { Component, OnInit } from '@angular/core';
import { UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { DbConnectionService } from '../services/db-connection.service';
import { ImageService } from '../services/image.service';
import { User, UserService } from '../services/user.service';

@Component({
  selector: 'app-signup',
  templateUrl: './signup.component.html',
  styleUrls: ['./signup.component.scss']
})
export class SignupComponent implements OnInit {

  form: UntypedFormGroup;
  error: string= "";

  fileName: string;
  imgSrc: string;
  imgError: string;

  constructor(private db: DbConnectionService,
    private route: Router,
    private image: ImageService) {
      // initialize form fields
      this.form = new UntypedFormGroup({
        firstName: new UntypedFormControl(),
        lastName: new UntypedFormControl(),
        email: new UntypedFormControl(),
        userName: new UntypedFormControl(),
        gender: new UntypedFormControl(),
        address: new UntypedFormControl(),
        birthDate: new UntypedFormControl(),
        phoneNumber: new UntypedFormControl(),
        password: new UntypedFormControl(),
        repeatPassword: new UntypedFormControl()
      });
   }

  ngOnInit(): void {
  }

  // onSubmit function
  signUp(){
    // collect form values
    let v = this.form.getRawValue();
    delete v.repeatPassword; // only used for clientside verification
    // add profile picture
    v['profilePicture'] = this.imgSrc;
    // send data
    this.db.signUp(v)
      .then(_ => {
        // go to login page
        this.route.navigateByUrl('/login')
      })
      .catch(r => this.error = r.error.message);
  }

  // select file
  fileSelected(ev){
    // no files selected
    if (ev.target.files.length === 0)
      return;
    // reset variables
    this.imgError = undefined;
    this.imgSrc = undefined;
    let f: File = <File>ev.target.files[0];
    this.fileName = f.name;
    // convert image to standard format
    this.image.convertFileToJpegBase64(f, (c) => {
      this.imgSrc = c;
    }, (err) => {
      this.imgError = err;
    }, 300, 300)
  }
}
<div style="margin: 10px;">
  <div class="container">
    <div class="row">
      <div class="offset-md-2 col-md-8 offset-lg-3 col-lg-6">

        <!-- Title -->
        <h1>Sign Up</h1>

        <!-- Form -->
        <div class="card">
          <div class="card-body">
            <form [formGroup]="form" (ngSubmit)="signUp()" *ngIf="form.getRawValue() as f">

              <!-- First Name -->
              <div class="form-group">
                <label for="inputFirstName">
                  First Name
                </label>
                <input
                  type="text"
                  class="form-control"
                  id="inputFirstName"
                  placeholder="Enter First Name"
                  formControlName="firstName">
              </div>

              <!-- Last Name -->
              <div class="form-group">
                <label for="inputLastName">
                  Last Name
                </label>
                <input
                  type="text"
                  class="form-control"
                  id="inputLastName"
                  placeholder="Enter Last Name"
                  formControlName="lastName">
              </div>

              <!-- Email Address -->
              <div class="form-group">
                <label for="inputEmail">
                  Email Address <i style="color: red;">*</i>
                </label>
                <input
                  type="email"
                  class="form-control"
                  id="inputEmail"
                  placeholder="Enter email"
                  formControlName="email">
              </div>

              <!-- Username -->
              <div class="form-group">
                <label for="inputUsername">
                  Username <i style="color: red;">*</i>
                </label>
                <input
                  type="text"
                  class="form-control"
                  id="inputUsername"
                  placeholder="Enter username"
                  formControlName="userName">
              </div>

              <!-- Gender -->
              <div class="form-group">
                <label for="inputGender">
                  Gender
                </label>
                <input
                  type="text"
                  class="form-control"
                  id="inputGender"
                  placeholder="Enter Gender"
                  formControlName="gender">
              </div>

              <!-- Address -->
              <div class="form-group">
                <label for="inputAddress">
                  Address
                </label>
                <input
                  type="text"
                  class="form-control"
                  id="inputAddress"
                  placeholder="Enter Address"
                  formControlName="address">
              </div>

              <!-- Birth Date -->
              <div class="form-group">
                <label for="inputBirthDate">
                  Birth Date
                </label>
                <input
                  type="date"
                  class="form-control"
                  id="inputBirthDate"
                  placeholder="Enter Birth Date"
                  formControlName="birthDate">
              </div>

              <!-- Phone number -->
              <div class="form-group">
                <label for="inputPhoneNumber">
                  Phone number
                </label>
                <input
                  type="text"
                  class="form-control"
                  id="inputPhoneNumber"
                  placeholder="Enter Phone Number"
                  formControlName="phoneNumber">
              </div>

              <!-- Profile picture -->
              <div class="form-group">

                <!-- Image Input -->
                <label for="inputImage">
                  Profile picture
                </label>
                <div class="custom-file" id="inputImage">
                  <input
                    type="file"
                    class="custom-file-input"
                    id="customFile"
                    (change)="fileSelected($event)">
                  <label class="custom-file-label" for="customFile">
                    {{fileName ? fileName : 'Choose Image'}}
                  </label>
                </div>

                <!-- Display selected Image -->
                <div *ngIf="imgSrc" style="display: flex;justify-content: center;">
                  <img style="width: 200px;height: 200px;border-radius: 50%;" [src]="imgSrc">
                </div>

                <!-- Remove Image Button -->
                <button
                  *ngIf="imgSrc"
                  type="button"
                  class="btn btn-outline-danger btn-block"
                  (click)="imgSrc = null">
                    Remove picture
                </button>

                <!-- Image Error -->
                <small *ngIf="imgError" class="form-text">
                  <i style="color: red;">{{imgError}}</i>
                </small>

              </div>

              <!-- Password -->
              <div class="form-group">
                <label for="inputPassword">
                  Password <i style="color: red;">*</i>
                </label>
                <input
                  type="password"
                  class="form-control"
                  id="inputPassword"
                  placeholder="Password"
                  formControlName="password">

                <!-- Password strength -->
                <small *ngIf="f.password" class="form-text">
                  <i
                    *ngIf="user.passwordStrength(f.password) as strength"
                    [style]="'color: ' + ['red', 'red', 'orange', 'green'][strength - 1]">
                      {{'•'.repeat(strength) + " " + ['Very weak', 'Weak', 'Medium', 'Strong'][strength -1]}}
                  </i>
                </small>

              </div>

              <!-- Password Confirmation -->
              <div class="form-group">

                <label for="inputRepeatPassword">
                  Repeat Password <i style="color: red;">*</i>
                </label>
                <input
                  type="password"
                  class="form-control"
                  id="inputRepeatPassword"
                  placeholder="Repeat Password"
                  formControlName="repeatPassword">

                <!-- Repeated password errors -->
                <small *ngIf="f.password && f.repeatPassword && f.password !== f.repeatPassword" class="form-text">
                  <i style="color: red;">Passwords don't match</i>
                </small>
              </div>


              <small class="form-text">
                <i style="color: red;">*</i> Required
              </small>
              <!-- Sign Up Button -->
              <button
                type="submit"
                class="btn btn-primary btn-block"
                [disabled]="!(f.userName && f.email && f.password && f.repeatPassword && f.password === f.repeatPassword)">
                  SIGN UP
              </button>

              <!-- Sign up errors-->
              <small *ngIf="error" class="form-text">
                <i style="color: red;">{{error}}</i>
              </small>

              <!-- Link to login page -->
              <a routerLink="/login">
                Already have an account? Log In
              </a>

            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

./signup.component.scss

Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""