<commit_before>import { Component, OnInit, ViewChild } from '@angular/core';
import { DateAdapter } from '@angular/material';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { MdSnackBar } from '@angular/material';

import { BoatUsageService } from '../boat-usage.service'
import { UsageInfo } from '../objects/usageInfo'

const NUMBER_REGEX = /[0-9]+/;

@Component({
  selector: 'app-report-usage',
  templateUrl: './report-usage.component.html',
  styleUrls: ['./report-usage.component.css']
})
export class ReportUsageComponent {
  constructor(
    private dateAdapter: DateAdapter<Date>,
    private usageService: BoatUsageService,
    private fb: FormBuilder,
    public snackBar: MdSnackBar
    )
    {
      this.dateAdapter.setLocale('en-nz');
      this.createForm();
    }

  title = "Report Boat Usage";
  maxDate = new Date();
  boats = [
    '1', '2', '3', '4', 'New 5', 'New 6', 'New 7', 'New 8'
  ];
  usageForm: FormGroup;

  /** Build the form */
  private createForm() {
    this.usageForm = this.fb.group({
      boatID: ['', Validators.required],
      duration: ['', [Validators.required, Validators.pattern(/[0-9]+/), Validators.min(0)]],
      date: new FormControl({value:this.maxDate,disabled:true},Validators.required)
    });

    this.usageForm.valueChanges
      .subscribe(data => this.onValueChanged(data));

    this.onValueChanged(); // (re)set validation messages now
  }

  /** Update error messages due to validation */
  private onValueChanged(data?: any) {
    if (!this.usageForm) { return; }
    const form = this.usageForm;

    for (const field in this.formErrors) {
      // clear previous error message (if any)
      this.formErrors[field] = '';
      const control = form.get(field);

      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }
  }

  formErrors = {
    'boatID': '',
    'duration': '',
    'date': ''
  };

  validationMessages = {
    'boatID': {
      'required': 'Boat is required.'
    },
    'duration': {
      'required': 'Duration is required.',
      'min': 'Duration must be at least 0',
      'pattern': 'Duration must be at least 0'
    },
    'date': {
      'required': 'Date is required.'
    }
  };

  /** Build BreakageInfo Object from submited data */
  private onSubmit() {
    if (this.usageForm.valid) {
      var usage = new UsageInfo(this.usageForm.get("boatID").value, this.usageForm.get("duration").value, this.usageForm.get("date").value)
      this.usageService.addUsageInfo(usage).then(
        () => (
          this.snackBar.open("Usage Succesfully Submited", "Close", {
            duration: 2000,<commit_msg>localisation<commit_after>import { Component, OnInit, ViewChild } from '@angular/core';
import { DateAdapter } from '@angular/material';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
import { MdSnackBar } from '@angular/material';

import { BoatUsageService } from '../boat-usage.service'
import { UsageInfo } from '../objects/usageInfo'

const NUMBER_REGEX = /[0-9]+/;

@Component({
  selector: 'app-report-usage',
  templateUrl: './report-usage.component.html',
  styleUrls: ['./report-usage.component.css']
})
export class ReportUsageComponent {
  constructor(
    private dateAdapter: DateAdapter<Date>,
    private usageService: BoatUsageService,
    private fb: FormBuilder,
    public snackBar: MdSnackBar
    )
    {
      this.dateAdapter.setLocale('en-nz');
      this.createForm();
    }

  title = "Report Boat Usage";
  maxDate = new Date();
  boats = [
    '1', '2', '3', '4', 'New 5', 'New 6', 'New 7', 'New 8'
  ];
  usageForm: FormGroup;

  /** Build the form */
  private createForm() {
    this.usageForm = this.fb.group({
      boatID: ['', Validators.required],
      duration: ['', [Validators.required, Validators.pattern(/[0-9]+/), Validators.min(0)]],
      date: new FormControl({value:this.maxDate,disabled:true},Validators.required)
    });

    this.usageForm.valueChanges
      .subscribe(data => this.onValueChanged(data));

    this.onValueChanged(); // (re)set validation messages now
  }

  /** Update error messages due to validation */
  private onValueChanged(data?: any) {
    if (!this.usageForm) { return; }
    const form = this.usageForm;

    for (const field in this.formErrors) {
      // clear previous error message (if any)
      this.formErrors[field] = '';
      const control = form.get(field);

      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }
  }

  formErrors = {
    'boatID': '',
    'duration': '',
    'date': ''
  };

  validationMessages = {
    'boatID': {
      'required': 'Boat is required.'
    },
    'duration': {
      'required': 'Duration is required.',
      'min': 'Duration must be at least 0',
      'pattern': 'Duration must be at least 0'
    },
    'date': {
      'required': 'Date is required.'
    }
  };

  /** Build BreakageInfo Object from submited data */
  private onSubmit() {
    if (this.usageForm.valid) {
      let usage = new UsageInfo(this.usageForm.get("boatID").value, this.usageForm.get("duration").value, this.usageForm.get("date").value)
      this.usageService.addUsageInfo(usage).then(
        () => (
          this.snackBar.open("Usage Succesfully Submited", "Close", {
            duration: 2000,
{'fraction_non_alphanumeric': 0.15654329732601382, 'fraction_numerical': 0.0067292367628829465, 'mean_word_length': 3.014214641080313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4034483', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1717, 'n_words': 407}