Skip to content

Commit c92dfbf

Browse files
feat: TTL-886 check required fields for internal apps
1 parent b55931a commit c92dfbf

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions';
22
import { EntryActionTypes, LoadActiveEntry } from './../../store/entry.actions';
3-
import { filter} from 'rxjs/operators';
3+
import { filter } from 'rxjs/operators';
44
import { Component, OnDestroy, OnInit, ElementRef, ViewChild } from '@angular/core';
55
import { FormBuilder, FormGroup } from '@angular/forms';
66
import { Store, ActionsSubject, select } from '@ngrx/store';
@@ -15,7 +15,7 @@ import { ToastrService } from 'ngx-toastr';
1515
import { formatDate } from '@angular/common';
1616
import { getTimeEntriesDataSource } from '../../store/entry.selectors';
1717
import { DATE_FORMAT } from 'src/environments/environment';
18-
import { Subscription, } from 'rxjs';
18+
import { Subscription } from 'rxjs';
1919

2020
type Merged = TechnologyState & ProjectState & ActivityState;
2121

@@ -25,7 +25,6 @@ type Merged = TechnologyState & ProjectState & ActivityState;
2525
styleUrls: ['./entry-fields.component.scss'],
2626
})
2727
export class EntryFieldsComponent implements OnInit, OnDestroy {
28-
2928
@ViewChild('autofocus') autofocus!: ElementRef<HTMLSelectElement>;
3029

3130
entryForm: FormGroup;
@@ -44,7 +43,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
4443
private formBuilder: FormBuilder,
4544
private store: Store<Merged>,
4645
private actionsSubject$: ActionsSubject,
47-
private toastrService: ToastrService,
46+
private toastrService: ToastrService
4847
) {
4948
this.entryForm = this.formBuilder.group({
5049
description: '',
@@ -58,12 +57,14 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
5857
ngOnInit(): void {
5958
this.store.dispatch(new LoadActivities());
6059
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
61-
this.loadActivitiesSubscription = this.actionsSubject$
60+
this.loadActivitiesSubscription = this.actionsSubject$
6261
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
6362
.subscribe((action) => {
64-
this.activities = action.payload.filter((item) => item.status !== 'inactive').sort((a, b) => {
65-
return (a.name).localeCompare(b.name);
66-
});
63+
this.activities = action.payload
64+
.filter((item) => item.status !== 'inactive')
65+
.sort((a, b) => {
66+
return a.name.localeCompare(b.name);
67+
});
6768
this.store.dispatch(new LoadActiveEntry());
6869
});
6970

@@ -96,7 +97,7 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
9697
uri: this.activeEntry.uri,
9798
activity_id: this.activeEntry.activity_id,
9899
start_date: this.activeEntry.start_date,
99-
start_hour: formatDate(this.activeEntry.start_date, 'HH:mm', 'en')
100+
start_hour: formatDate(this.activeEntry.start_date, 'HH:mm', 'en'),
100101
};
101102
this.activateFocus();
102103
});
@@ -108,8 +109,8 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
108109
return this.entryForm.get('start_hour');
109110
}
110111

111-
activateFocus(){
112-
if ((this.activities.length > 0) && (this.entryForm.value.activity_id === head(this.activities).id)){
112+
activateFocus() {
113+
if (this.activities.length > 0 && this.entryForm.value.activity_id === head(this.activities).id) {
113114
this.autofocus.nativeElement.focus();
114115
}
115116
}
@@ -132,11 +133,16 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
132133
}
133134

134135
entryFormIsValidate() {
135-
return this.entryForm.valid;
136+
let projectName = '';
137+
this.store.pipe(select(getTimeEntriesDataSource)).subscribe((ds) => {
138+
const dataToUse = ds.data.find((item) => item.project_id === this.activeEntry.project_id);
139+
projectName = dataToUse.project_name;
140+
});
141+
return this.requiredFieldsForInternalAppExist(projectName) && this.entryForm.valid;
136142
}
137143

138144
onSubmit() {
139-
if (this.entryFormIsValidate()){
145+
if (this.entryFormIsValidate()) {
140146
this.store.dispatch(new entryActions.UpdateEntryRunning({ ...this.newData, ...this.entryForm.value }));
141147
}
142148
}
@@ -192,4 +198,15 @@ export class EntryFieldsComponent implements OnInit, OnDestroy {
192198
this.loadActiveEntrySubscription.unsubscribe();
193199
this.actionSetDateSubscription.unsubscribe();
194200
}
201+
202+
requiredFieldsForInternalAppExist(projectName) {
203+
const emptyFields = this.entryForm.value.uri === '' && this.entryForm.value.description === '';
204+
const isInternalApp = projectName.includes('(Applications)');
205+
if (isInternalApp && emptyFields) {
206+
const message = 'The description field or ticket field should not be empty';
207+
this.toastrService.error(`Some fields are empty, ${message}.`);
208+
return false;
209+
}
210+
return true;
211+
}
195212
}

0 commit comments

Comments
 (0)