import { Component , OnInit , Input } from '@angular/core' ; import { ITask , IOrganizationProject , IEmployee , IOrganizationTeam , ITag , TaskParticipantEnum , IOrganization } from '@gauzy/contracts' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { NbDialogRef } from '@nebular/theme' ; import { TranslateService } from '@ngx-translate/core' ; import * as moment from 'moment' ; import { filter , tap } from 'rxjs/operators' ; import { firstValueFrom } from 'rxjs' ; import { TranslationBaseComponent } from '../../../@shared/language-base/translation-base.component' ; import { EmployeesService , OrganizationTeamsService , Store , TasksService } from '../../../@core/services' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ngx-add-task-dialog' , templateUrl : './add-task-dialog.component.html' , styleUrls : [ './add-task-dialog.component.scss' ] } ) export class AddTaskDialogComponent extends TranslationBaseComponent implements OnInit { employees : IEmployee [ ] = [ ] ; teams : IOrganizationTeam [ ] = [ ] ; selectedMembers : string [ ] ; selectedTeams : string [ ] ; selectedTask : ITask ; organization : IOrganization ; taskParticipantEnum = TaskParticipantEnum ; participants = TaskParticipantEnum . EMPLOYEES ; @ Input ( ) createTask = false ; _task : ITask ; get task ( ) : ITask { return this . _task ; } @ Input ( ) set task ( value ) { this . selectedTask = value ; this . _task = value ; } public form : FormGroup = AddTaskDialogComponent . buildForm ( this . fb , this ) ; static buildForm ( fb , self ) { return fb . group ( { title : [ '' , Validators . required ] , project : [ ] , projectId : [ ] , status : [ self . getTranslation ( 'TASKS_PAGE.TODO' ) ] , members : [ ] , estimateDays : [ ] , estimateHours : [ '' , [ Validators . min ( 0 ) , Validators . max ( 23 ) ] ] , estimateMinutes : [ '' , [ Validators . min ( 0 ) , Validators . max ( 59 ) ] ] , dueDate : [ ] , description : [ ] , tags : [ ] , teams : [ ] } ) ; } constructor ( public readonly dialogRef , private readonly fb , private readonly store , readonly translateService , private readonly employeesService , private readonly tasksService , private readonly organizationTeamsService ) { super ( translateService ) ; } ngOnInit ( ) { this . store . selectedOrganization$ . pipe ( filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => this . organization = organization ) , tap ( ( ) => this . loadEmployees ( ) ) , tap ( ( ) => this . loadTeams ( ) ) , tap ( ( ) => this . initializeForm ( ) ) , untilDestroyed ( this ) ) . subscribe ( ) ; } initializeForm ( ) { if ( this . selectedTask ) { const { description , dueDate , estimate , members , project , status , tags , teams , title } = this . selectedTask ; const duration = moment . duration ( estimate , 'seconds' ) ; this . selectedMembers = ( members || [ ] ) . map ( ( member ) => member . id ) ; this . selectedTeams = ( teams || [ ] ) . map ( ( team ) => team . id ) ; if ( teams && teams . length > 0 ) { this . participants = TaskParticipantEnum . TEAMS ; } this . form . patchValue ( { title , project , projectId : ( project ) ? project . id : null , status , estimateDays : duration . days ( ) , estimateHours : duration . hours ( ) , estimateMinutes : duration . minutes ( ) , dueDate : moment ( dueDate ) . toDate ( ) , description , tags , teams : this . selectedTeams } ) ; } } onSave ( ) { if ( this . form . valid ) { this . form . get ( 'members' ) . setValue ( ( this . selectedMembers || [ ] ) . map ( ( id ) => this . employees . find ( ( e ) => e . id === id ) ) . filter ( ( e ) => ! ! e ) ) ; this . form . get ( 'teams' ) . setValue ( ( this . selectedTeams || [ ] ) . map ( ( id ) => this . teams . find ( ( e ) => e . id === id ) ) . filter ( ( e ) => ! ! e ) ) ; const { estimateDays , estimateHours , estimateMinutes } = this . form . value ; const estimate = estimateDays * 24 * 60 * 60 + estimateHours * 60 * 60 + estimateMinutes * 60 ; estimate ? ( this . form . value . estimate = estimate ) : ( this . form . value . estimate = null ) ; if ( this . createTask ) { firstValueFrom ( this . tasksService . createTask ( this . form . value ) ) . then ( ( task ) => { this . dialogRef . close ( task ) ; } ) ; } else { this . dialogRef . close ( this . form . value ) ; } } } selectedTagsHandler ( currentSelection ) { this . form . patchValue ( { tags : currentSelection } ) ; } selectedProject ( project ) { this . form . patchValue ( { project } ) ; } async loadEmployees ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await firstValueFrom ( this . employeesService . getAll ( [ 'user' ] , { organizationId , tenantId } ) ) ; this . employees = items ; } onMembersSelected ( members ) { this . selectedMembers = members ; } async loadTeams ( ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; const { items = [ ] } = await this . organizationTeamsService . getAll ( [ 'members' ] , { organizationId , tenantId } ) ; this . teams = items ; } onParticipantsChange ( participants ) { this . selectedMembers = [ ] ; this . selectedTeams = [ ] ; this . form . get ( 'members' ) . setValue ( [ ] ) ; this . form . get ( 'teams' ) . setValue ( [ ] ) ; this . participants = participants ; } onTeamsSelected ( teamsSelection ) { this . selectedTeams = teamsSelection ; } }