Skip to content
Next Next commit
refactor: TTL-887 validate required fields for internal apps
  • Loading branch information
mmaquina committed May 30, 2023
commit d18f5217b11deb5e421ef7cd68a71f7f570d1458
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ export class DetailsFieldsComponent implements OnChanges, OnInit {
}

onSubmit() {

if(this.entryForm.value.project_name.includes('ioet')){
if (!this.entryForm.value.uri && !this.entryForm.value.description){
this.toastrService.warning('Make sure to add a description and/or ticket number when working on an internal app');
return;
}
}

if (this.entryForm.invalid) {
this.toastrService.warning('Make sure to select a project and activity');
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
import { EntryActionTypes, LoadActiveEntry } from './../../store/entry.actions';
import { filter} from 'rxjs/operators';
import { filter } from 'rxjs/operators';
import { Component, OnDestroy, OnInit, ElementRef, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Store, ActionsSubject, select } from '@ngrx/store';
Expand All @@ -15,7 +15,7 @@ import { ToastrService } from 'ngx-toastr';
import { formatDate } from '@angular/common';
import { getTimeEntriesDataSource } from '../../store/entry.selectors';
import { DATE_FORMAT } from 'src/environments/environment';
import { Subscription, } from 'rxjs';
import { Subscription } from 'rxjs';

type Merged = TechnologyState & ProjectState & ActivityState;

Expand All @@ -25,7 +25,6 @@ type Merged = TechnologyState & ProjectState & ActivityState;
styleUrls: ['./entry-fields.component.scss'],
})
export class EntryFieldsComponent implements OnInit, OnDestroy {

@ViewChild('autofocus') autofocus!: ElementRef<HTMLSelectElement>;

entryForm: FormGroup;
Expand All @@ -44,7 +43,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
private formBuilder: FormBuilder,
private store: Store<Merged>,
private actionsSubject$: ActionsSubject,
private toastrService: ToastrService,
private toastrService: ToastrService
) {
this.entryForm = this.formBuilder.group({
description: '',
Expand All @@ -58,12 +57,14 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
ngOnInit(): void {
this.store.dispatch(new LoadActivities());
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
this.loadActivitiesSubscription = this.actionsSubject$
this.loadActivitiesSubscription = this.actionsSubject$
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
.subscribe((action) => {
this.activities = action.payload.filter((item) => item.status !== 'inactive').sort((a, b) => {
return (a.name).localeCompare(b.name);
});
this.activities = action.payload
.filter((item) => item.status !== 'inactive')
.sort((a, b) => {
return a.name.localeCompare(b.name);
});
this.store.dispatch(new LoadActiveEntry());
});

Expand Down Expand Up @@ -96,7 +97,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
uri: this.activeEntry.uri,
activity_id: this.activeEntry.activity_id,
start_date: this.activeEntry.start_date,
start_hour: formatDate(this.activeEntry.start_date, 'HH:mm', 'en')
start_hour: formatDate(this.activeEntry.start_date, 'HH:mm', 'en'),
};
this.activateFocus();
});
Expand All @@ -108,8 +109,8 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
return this.entryForm.get('start_hour');
}

activateFocus(){
if ((this.activities.length > 0) && (this.entryForm.value.activity_id === head(this.activities).id)){
activateFocus() {
if (this.activities.length > 0 && this.entryForm.value.activity_id === head(this.activities).id) {
this.autofocus.nativeElement.focus();
}
}
Expand All @@ -132,11 +133,16 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
}

entryFormIsValidate() {
return this.entryForm.valid;
let customerName = '';
this.store.pipe(select(getTimeEntriesDataSource)).subscribe((ds) => {
const dataToUse = ds.data.find((item) => item.project_id === this.activeEntry.project_id);
customerName = dataToUse.customer_name;
});
return this.requiredFieldsForInternalAppExist(customerName) && this.entryForm.valid;
}

onSubmit() {
if (this.entryFormIsValidate()){
if (this.entryFormIsValidate()) {
this.store.dispatch(new entryActions.UpdateEntryRunning({ ...this.newData, ...this.entryForm.value }));
}
}
Expand Down Expand Up @@ -192,4 +198,15 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
this.loadActiveEntrySubscription.unsubscribe();
this.actionSetDateSubscription.unsubscribe();
}

requiredFieldsForInternalAppExist(customerName) {
const emptyFields = this.entryForm.value.uri === '' && this.entryForm.value.description === '';
const isInternalApp = customerName.includes('ioet');
if (isInternalApp && emptyFields) {
const message = 'The description field or ticket field should not be empty';
this.toastrService.error(`Some fields are empty, ${message}.`);
return false;
}
return true;
}
}