import { Component , Input , OnInit } from '@angular/core' ; import { ITask , IOrganizationProject , IEmployee , IOrganizationTeam , ITag } from '@gauzy/contracts' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { NbDialogRef } from '@nebular/theme' ; import { OrganizationProjectsService } from '../../../../@core/services/organization-projects.service' ; import { Store } from '../../../../@core/services/store.service' ; import { TranslateService } from '@ngx-translate/core' ; import { ErrorHandlingService } from '../../../../@core/services/error-handling.service' ; import { TranslationBaseComponent } from '../../../../@shared/language-base/translation-base.component' ; import * as moment from 'moment' ; import { OrganizationTeamsService } from '../../../../@core/services/organization-teams.service' ; import { ToastrService } from '../../../../@core/services/toastr.service' ; const initialTaskValue = { title : '' , project : null , status : '' , members : null , teams : null , estimate : null , dueDate : null , description : '' , tags : null } ; @ Component ( { selector : 'ngx-team-task-dialog' , templateUrl : './team-task-dialog.component.html' , styleUrls : [ './team-task-dialog.component.scss' ] } ) export class TeamTaskDialogComponent extends TranslationBaseComponent implements OnInit { form : FormGroup ; selectedTaskId : string ; projects : IOrganizationProject [ ] ; statuses : string [ ] = [ this . getTranslation ( 'TASKS_PAGE.TODO' ) , this . getTranslation ( 'TASKS_PAGE.IN_PROGRESS' ) , this . getTranslation ( 'TASKS_PAGE.FOR_TESTING' ) , this . getTranslation ( 'TASKS_PAGE.COMPLETED' ) ] ; employees : IEmployee [ ] = [ ] ; teams : IOrganizationTeam [ ] = [ ] ; selectedMembers : string [ ] ; selectedTeams : string [ ] ; selectedTask : ITask ; organizationId : string ; tenantId : string ; tags : ITag [ ] = [ ] ; @ Input ( ) task : Partial < ITask > = { } ; constructor ( public dialogRef , private fb , private store , private _organizationsStore , private organizationProjectsService , readonly translateService , private readonly toastrService , private errorHandler , private organizationTeamsService ) { super ( translateService ) ; } ngOnInit ( ) { this . tenantId = this . store . user . tenantId ; this . organizationId = this . _organizationsStore . selectedOrganization . id ; this . loadProjects ( ) ; this . loadTeams ( ) ; this . initializeForm ( Object . assign ( { } , initialTaskValue , this . selectedTask || this . task ) ) ; } private async loadProjects ( ) { const { organizationId , tenantId } = this ; const { items } = await this . organizationProjectsService . getAll ( [ ] , { organizationId , tenantId } ) ; if ( items ) this . projects = items ; } initializeForm ( { title , description , project , status , members , teams , estimate , dueDate , tags } ) { const duration = moment . duration ( estimate , 'seconds' ) ; this . selectedTeams = ( teams || [ ] ) . map ( ( team ) => team . id ) ; this . form = this . fb . group ( { title : [ title , Validators . required ] , project : [ project ] , projectId : ( project ) ? project . id : null , status : [ status ? status : this . getTranslation ( 'TASKS_PAGE.TODO' ) ] , members : [ members ] , estimateDays : [ duration . days ( ) || '' ] , estimateHours : [ duration . hours ( ) || '' , [ Validators . min ( 0 ) , Validators . max ( 23 ) ] ] , estimateMinutes : [ duration . minutes ( ) || '' , [ Validators . min ( 0 ) , Validators . max ( 59 ) ] ] , dueDate : [ dueDate ] , description : [ description ] , tags : [ tags ] , teams : [ this . selectedTeams ] } ) ; this . tags = this . form . get ( 'tags' ) . value || [ ] ; } addNewProject = ( name ) : Promise < IOrganizationProject > => { const { organizationId , tenantId } = this ; try { this . toastrService . success ( 'NOTES.ORGANIZATIONS.EDIT_ORGANIZATIONS_PROJECTS.ADD_PROJECT' , { name } ) ; return this . organizationProjectsService . create ( { name , organizationId , tenantId } ) ; } catch ( error ) { this . errorHandler . handleError ( error ) ; } } ; onSave ( ) { if ( this . form . valid ) { this . form . get ( 'teams' ) . setValue ( ( this . selectedTeams || [ ] ) . map ( ( id ) => this . teams . find ( ( e ) => e . id === id ) ) . filter ( ( e ) => ! ! e ) ) ; this . dialogRef . close ( this . form . value ) ; } } selectedTagsHandler ( currentSelection ) { this . form . get ( 'tags' ) . setValue ( currentSelection ) ; } selectedProject ( project ) { this . form . patchValue ( { project } ) ; } async loadTeams ( ) { const { organizationId , tenantId } = this ; if ( ! organizationId ) { return ; } this . teams = ( await this . organizationTeamsService . getMyTeams ( [ 'members' ] , { organizationId , tenantId } ) ) . items ; } onTeamsSelected ( teamsSelection ) { this . selectedTeams = teamsSelection ; } }