All files / src/app/feedback feedback.component.ts

80.49% Statements 33/41
58.06% Branches 18/31
75% Functions 6/8
80.56% Lines 29/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                  1x 1x 1x 2x 1x   1x 1x   1x 1x 1x 1x 1x             7x 7x       7x               7x       7x       7x       7x 7x                       7x         1x           1x                                   1x 1x   1x           1x               1x   1x          
/**
 * File Name:       feedback.component
 * Version Number:  v1.0
 * Author Name:     Tobias Bester
 * Project Name:    Ninshiki
 * Organization:    Software Sharks
 * Manual:  Refer to the Ninshiki User Manual at https://github.com/OrishaOrrie/SoftwareSharks/blob/master/Documentation/User%20Manual.pdf
 * Update History:
 * ------------------------------------------
 * Date         Author        Description
 * 01/09/2018   Tobias        Created component
 * ------------------------------------------
 * Test Cases:      feedback.component.spec.ts
 * Functional Description:
 *  Allows user to send feedback to developers
 */
 
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
impoIrt { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
E
@Component({
  selector: 'app-feedback',
  templateUrl: './feedback.component.html',
  stEyleUrls: ['./feedback.component.scss']
})
export class FeedbackComponent implements OnInit {
 
  /**
   * Array of all types of feedback
   */
  public feedbackTypes: string[] = [
    'General Feedback',
    'Bug',
    'Feature Request'
  ];
 
  /**
   * The default feedback type to be used
   */
  public defaultType = 'General Feedback';
 
  /**
   * Indicates that the feedback is being sent
   */
  public sending: Boolean = false;
 
  /**
   * Indicates that a response has been received from the server
   */
  public submitted: Boolean = false;
 
  /**
   * Indicates that an error occured while trying to submit feedback
   */
  public failedToSubmit: Boolean = false;
 
  /**
   * Used for form controls and validation
   */
  public formGroup: FormGroup;
 
  /**
   * THe form group and it's validation is defined in this constructor
   * @param fb Form Builder injection used for Form validation
   * @param http HttpClient injection used to make HTTP requests
   */
  constructor(public fb: FormBuilder, public http: HttpClient) {
    this.formGroup = this.fb.group({
      name: new FormControl('', Validators.compose([
        Validators.maxLength(30),
        Validators.minLength(3),
        Validators.required
      ])),
      feedType: new FormControl(null),
      message: new FormControl('', Validators.compose([
        Validators.minLength(5),
        Validators.maxLength(200)
      ]))
    });
    this.formGroup.controls['feedType'].setValue(this.defaultType, {onlySelf: true});
  }
 
  /**
   * @ignore
   */
  ngOnInit() {
  }

  /**
   * Called when the Send Feedback button is clicked. POSTs the message and handles the server
   * response
   */
  submitForm() {
    this.sending = true;
 
    this.postMessage().subscribe(
      (data => {
        if (data['message'] === 'Message sent') {
          this.submitted = true;
        } else {
          this.failedToSubmit = true;
        }
        this.sending = false;
      })
    );
    // this.formGroup.reset();
 
  }
 
  /**
   * Handles sending the HTTP POST request to the email server with the necessary feedback details
   * @returns   Observable with the data received by the email server
   */
  postMessage() {
    const msgToSend = 'Feedback type: ' + this.formGroup.controls['feedType'].value + '\n'
    + this.formGroup.controls['message'].value;
 
    return this.http.post('https://us-central1-testproject-ee885.cloudfunctions.net/app/sendmail',
      {
        'subject': this.formGroup.controls['name'].value,
        'text': msgToSend,
        'email': 'Email Unavailable',
      }
    );
  }
 
}