<commit_before>import { Component, OnInit, Input, ViewChildren } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
 
import { ActivatedRoute, Params, Router } from '@angular/router';
import { CustomValidators } from 'ng2-validation';
import { SprintService } from '../../../shared/services/sprint.service';
import { Sprint } from '../../../shared/models/sprint';
import { SprintValidator } from './sprint.validator'

import { FieldErrorsComponent } from '../../../shared/components/field-errors/field-errors.component'

@Component({
  selector: 'app-sprint-detail',
  templateUrl: './sprint-detail.component.html',
  styleUrls: ['./sprint-detail.component.css']
})
export class SprintDetailComponent implements OnInit {
  
  title: String;
  sprintForm: FormGroup;
  validator: SprintValidator = new SprintValidator();
  
  @Input() sprint: Sprint;
  error: any;
  
  constructor(
    private router: Router,
    private fb: FormBuilder,
    private sprintService: SprintService,
    private route: ActivatedRoute) {}

  ngOnInit(): void {
    this.createEmptyForm();
    this.loadSprintForEdit();
  }

  loadSprintForEdit(): void {

    this.route.params.forEach((params: Params) => {
      if (params['id'] !== undefined) {
        let id = params['id'];
        this.sprintService
            .findById(id)
            .then(sprint =>
              {
                this.sprint = sprint;
                this.setFormValues();
              }
            );
      } else {
        this.setFormValues();
      }
    });
  }

  createEmptyForm(): void {

    this.sprint = new Sprint();
    this.sprint.dateStart = null;
    this.sprint.dateFinish = null;
    this.sprint.initialAmount = null;

    this.sprintForm = this.fb.group({
      dateStart: [this.sprint.dateStart, [Validators.required]],
      dateFinish: [this.sprint.dateFinish, [Validators.required]],
      initialAmount: [this.sprint.initialAmount, [Validators.required, CustomValidators.gt(0)]]
    });
  }

  setTitle(): void {
    if (this.sprint._id){
      this.title = "Editar período";
    } else {
      this.title = "Inserir período";
    }
  }

  setFormValues(): void {
    
    this.setTitle();

    this.sprintForm.setValue({
      dateStart: this.sprint.dateStart, 
      dateFinish: this.sprint.dateFinish,
      initialAmount: this.sprint.initialAmount
    });

    this.validator = new SprintValidator(this.sprintForm, this.sprintService, this.sprint);
  }

  setModelValues(formValues: Sprint): void {
     formValues._id = this.sprint._id;
     this.sprint = formValues;

     const offset =  new Date().getTimezoneOffset() * 60000;
     const dateStart = new Date(this.sprint.dateStart + "T00:00:00.000Z")
     this.sprint.dateStart = new Date(dateStart.getTime() + offset);
     const dateFinish = new Date(this.sprint.dateFinish + "T23:59:59.999Z")
     this.sprint.dateFinish = new Date(dateFinish.getTime() + offset);
  }

  onSubmit({ value, valid }: { value: Sprint, valid: boolean }) {
    if (valid){
      this.validator.clearErrors();
      this.save(value);
    } else {
      this.validator.showErrors();
    }
  }

  save(formValues: Sprint): void {
    this.setModelValues(formValues);

    this.sprintService
        .save(this.sprint)
        .then(sprint => {
          this.goBack();
        })
        .catch(error  => this.error = error);
  }

  goBack(): void {
    this.router.navigateByUrl('/sprints');
  }
}<commit_msg>Fix timezone during insertion of sprints. Use timzone offset from selected date.<commit_after>import { Component, OnInit, Input, ViewChildren } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
 
import { ActivatedRoute, Params, Router } from '@angular/router';
import { CustomValidators } from 'ng2-validation';
import { SprintService } from '../../../shared/services/sprint.service';
import { Sprint } from '../../../shared/models/sprint';
import { SprintValidator } from './sprint.validator'

import { FieldErrorsComponent } from '../../../shared/components/field-errors/field-errors.component'

@Component({
  selector: 'app-sprint-detail',
  templateUrl: './sprint-detail.component.html',
  styleUrls: ['./sprint-detail.component.css']
})
export class SprintDetailComponent implements OnInit {
  
  title: String;
  sprintForm: FormGroup;
  validator: SprintValidator = new SprintValidator();
  
  @Input() sprint: Sprint;
  error: any;
  
  constructor(
    private router: Router,
    private fb: FormBuilder,
    private sprintService: SprintService,
    private route: ActivatedRoute) {}

  ngOnInit(): void {
    this.createEmptyForm();
    this.loadSprintForEdit();
  }

  loadSprintForEdit(): void {

    this.route.params.forEach((params: Params) => {
      if (params['id'] !== undefined) {
        let id = params['id'];
        this.sprintService
            .findById(id)
            .then(sprint =>
              {
                this.sprint = sprint;
                this.setFormValues();
              }
            );
      } else {
        this.setFormValues();
      }
    });
  }

  createEmptyForm(): void {

    this.sprint = new Sprint();
    this.sprint.dateStart = null;
    this.sprint.dateFinish = null;
    this.sprint.initialAmount = null;

    this.sprintForm = this.fb.group({
      dateStart: [this.sprint.dateStart, [Validators.required]],
      dateFinish: [this.sprint.dateFinish, [Validators.required]],
      initialAmount: [this.sprint.initialAmount, [Validators.required, CustomValidators.gt(0)]]
    });
  }

  setTitle(): void {
    if (this.sprint._id){
      this.title = "Editar período";
    } else {
      this.title = "Inserir período";
    }
  }

  setFormValues(): void {
    
    this.setTitle();

    this.sprintForm.setValue({
      dateStart: this.sprint.dateStart, 
      dateFinish: this.sprint.dateFinish,
      initialAmount: this.sprint.initialAmount
    });

    this.validator = new SprintValidator(this.sprintForm, this.sprintService, this.sprint);
  }

  setModelValues(formValues: Sprint): void {
     formValues._id = this.sprint._id;
     this.sprint = formValues;

     const dateStart = new Date(this.sprint.dateStart + "T00:00:00.000Z")
     this.sprint.dateStart = new Date(dateStart.getTime() + dateStart.getTimezoneOffset() * 60000);
     const dateFinish = new Date(this.sprint.dateFinish + "T23:59:59.999Z")
     this.sprint.dateFinish = new Date(dateFinish.getTime() + dateFinish.getTimezoneOffset() * 60000);
  }

  onSubmit({ value, valid }: { value: Sprint, valid: boolean }) {
    if (valid){
      this.validator.clearErrors();
      this.save(value);
    } else {
      this.validator.showErrors();
    }
  }

  save(formValues: Sprint): void {
    this.setModelValues(formValues);

    this.sprintService
        .save(this.sprint)
        .then(sprint => {
          this.goBack();
        })
        .catch(error  => this.error = error);
  }

  goBack(): void {
    this.router.navigateByUrl('/sprints');
  }
}
{'fraction_non_alphanumeric': 0.1415778251599147, 'fraction_numerical': 0.007818052594171997, 'mean_word_length': 2.844808743169399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4824687', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2168, 'n_words': 420}