import { Component , OnInit , ViewChild , OnDestroy , AfterViewInit , ChangeDetectorRef } from '@angular/core' ; import { FormGroup , FormBuilder , Validators } from '@angular/forms' ; import { ProposalStatusEnum , ITag , IOrganization , IEmployee , IEmployeeProposalTemplate , IOrganizationContact } from '@gauzy/contracts' ; import { filter , tap } from 'rxjs/operators' ; import { TranslateService } from '@ngx-translate/core' ; import { Router } from '@angular/router' ; import { UntilDestroy , untilDestroyed } from '@ngneat/until-destroy' ; import * as moment from 'moment' ; import { distinctUntilChange , isNotEmpty } from '@gauzy/common-angular' ; import { EmployeeSelectorComponent } from '../../../@theme/components/header/selectors/employee/employee.component' ; import { TranslationBaseComponent } from '../../../@shared/language-base/translation-base.component' ; import { ProposalsService , Store , ToastrService } from '../../../@core/services' ; import { ckEditorConfig } from "../../../@shared/ckeditor.config" ; import { UrlPatternValidator } from '../../../@core/validators' ; @ UntilDestroy ( { checkProperties : true } ) @ Component ( { selector : 'ga-proposal-register' , templateUrl : './proposal-register.component.html' , styleUrls : [ '././proposal-register.component.scss' ] } ) export class ProposalRegisterComponent extends TranslationBaseComponent implements OnInit , OnDestroy , AfterViewInit { @ ViewChild ( 'employeeSelector' ) employeeSelector : EmployeeSelectorComponent ; proposalTemplate : IEmployeeProposalTemplate ; proposalTemplateId : string ; organization : IOrganization ; organizationContact : IOrganizationContact ; tags : ITag [ ] = [ ] ; ckConfig : any = ckEditorConfig ; minDate = new Date ( moment ( ) . subtract ( 1 , 'days' ) . format ( 'YYYY-MM-DD' ) ) ; selectedEmployee : IEmployee ; public form : FormGroup = ProposalRegisterComponent . buildForm ( this . fb ) ; static buildForm ( fb ) { return fb . group ( { jobPostUrl : [ ] , valueDate : [ new Date ( ) , Validators . required ] , jobPostContent : [ '' , Validators . required ] , proposalContent : [ '' , Validators . required ] , tags : [ ] , organizationContact : [ ] } , { validators : [ UrlPatternValidator . websiteUrlValidator ( 'jobPostUrl' ) , ] } ) ; } constructor ( private readonly fb , private readonly store , private readonly router , private readonly proposalsService , private readonly toastrService , readonly translateService , private readonly cdRef ) { super ( translateService ) ; } ngOnInit ( ) { this . store . selectedOrganization$ . pipe ( distinctUntilChange ( ) , filter ( ( organization ) => ! ! organization ) , tap ( ( organization ) => this . organization = organization ) , untilDestroyed ( this ) ) . subscribe ( ) ; } ngOnDestroy ( ) { } onEmployeeChange ( item ) { this . selectedEmployee = item ; this . proposalTemplateId = null ; } onProposalTemplateChange ( item ) { if ( isNotEmpty ( item ) ) { this . form . patchValue ( { proposalContent : item . content } ) ; } else { this . form . patchValue ( { proposalContent : null } ) ; } } ngAfterViewInit ( ) { this . cdRef . detectChanges ( ) ; } public async registerProposal ( ) { if ( this . form . invalid ) { return ; } const { jobPostUrl , valueDate , jobPostContent , proposalContent , organizationContact , tags } = this . form . value ; const selectedEmployee = this . employeeSelector . selectedEmployee ; try { if ( selectedEmployee ) { const { tenantId } = this . store . user ; const { id : organizationId } = this . organization ; await this . proposalsService . create ( { employeeId : selectedEmployee . id , organizationId , tenantId , jobPostUrl , valueDate , jobPostContent , proposalContent , status : ProposalStatusEnum . SENT , tags , organizationContactId : organizationContact ? organizationContact . id : null } ) ; this . toastrService . success ( 'NOTES.PROPOSALS.REGISTER_PROPOSAL' ) ; this . router . navigate ( [ '/pages/sales/proposals' ] ) ; } else { this . toastrService . success ( 'NOTES.PROPOSALS.REGISTER_PROPOSAL_NO_EMPLOYEE_SELECTED' , null , 'TOASTR.MESSAGE.REGISTER_PROPOSAL_NO_EMPLOYEE_MSG' ) ; } } catch ( error ) { this . toastrService . danger ( error ) ; } } selectOrganizationContact ( $event ) { this . organizationContact = $event ; } selectedTagsEvent ( ev ) { this . form . patchValue ( { tags : ev } ) ; } }