11import { Component , OnDestroy , OnInit } from '@angular/core'
22import { Subject } from 'rxjs'
33import { MatDialogRef } from '@angular/material/dialog'
4- import { takeUntil } from 'rxjs/operators'
4+ import { take , takeUntil } from 'rxjs/operators'
55import { RecordImportWizard } from '../../../../../types/record-peer-review-import.endpoint'
66import { ModalComponent } from '../../../../../cdk/modal/modal/modal.component'
77import { RecordWorksService } from '../../../../../core/record-works/record-works.service'
8+ import { TogglzService } from '../../../../../core/togglz/togglz.service'
9+ import { TogglzFlag } from '../../../../../types/config.endpoint'
810import { sortBy } from 'lodash'
911
1012@Component ( {
@@ -19,72 +21,80 @@ export class ModalWorksSearchLinkComponent implements OnInit, OnDestroy {
1921 loadingWorks = true
2022 recordImportWizardsOriginal : RecordImportWizard [ ]
2123 recordImportWizards : RecordImportWizard [ ]
22- workTypes = [ ]
23- geographicalAreas = [ ]
24+ workTypes : string [ ] = [ ]
25+ geographicalAreas : string [ ] = [ ]
2426 workTypeSelected = 'All'
2527 geographicalAreaSelected = 'All'
2628 total = 0
29+ /** When true, use new search-and-link endpoint and UI (no work type/geo filters, show isConnected). */
30+ useNewSearchLinkUi = false
2731
2832 constructor (
2933 public dialogRef : MatDialogRef < ModalComponent > ,
30- private _recordWorksService : RecordWorksService
34+ private _recordWorksService : RecordWorksService ,
35+ private _togglz : TogglzService
3136 ) { }
3237
3338 ngOnInit ( ) : void {
34- this . loadWorkImportWizardList ( )
39+ this . _togglz
40+ . getStateOf ( TogglzFlag . SEARCH_AND_LINK_WIZARD_WITH_CERTIFIED_AND_FEATURED_LINKS )
41+ . pipe ( take ( 1 ) , takeUntil ( this . $destroy ) )
42+ . subscribe ( ( enabled ) => {
43+ this . useNewSearchLinkUi = enabled
44+ this . loadWorkImportWizardList ( )
45+ } )
3546 }
3647
3748 loadWorkImportWizardList ( ) : void {
3849 this . _recordWorksService
3950 . loadWorkImportWizardList ( )
4051 . pipe ( takeUntil ( this . $destroy ) )
4152 . subscribe ( ( recordImportWizards ) => {
53+ recordImportWizards . forEach ( ( w ) => ( w . show = w . show ?? false ) )
4254 this . recordImportWizardsOriginal = sortBy ( recordImportWizards , 'name' )
43- this . recordImportWizards = this . recordImportWizardsOriginal
55+ this . recordImportWizards = [ ... this . recordImportWizardsOriginal ]
4456 recordImportWizards . forEach ( ( recordImportWizard ) => {
45- recordImportWizard . actTypes . forEach ( ( actType ) => {
57+ recordImportWizard . actTypes ? .forEach ( ( actType ) => {
4658 if ( ! this . workTypes . includes ( actType ) ) {
4759 this . workTypes . push ( actType )
4860 }
4961 } )
50-
51- recordImportWizard . geoAreas . forEach ( ( geoArea ) => {
62+ recordImportWizard . geoAreas ?. forEach ( ( geoArea ) => {
5263 if ( ! this . geographicalAreas . includes ( geoArea ) ) {
5364 this . geographicalAreas . push ( geoArea )
5465 }
5566 } )
5667 } )
5768 this . loadingWorks = false
58-
5969 this . total = this . recordImportWizardsOriginal . length
6070 } )
6171 }
6272
6373 searchAndLink ( ) {
64- this . recordImportWizards = [ ]
65- this . recordImportWizardsOriginal . forEach ( ( recordImportWizard ) => {
66- if (
67- this . workTypeSelected === 'All' &&
68- this . geographicalAreaSelected === 'All'
69- ) {
70- this . recordImportWizards = this . recordImportWizardsOriginal
71- } else if (
72- this . workTypeSelected === 'All' &&
73- recordImportWizard . geoAreas . includes ( this . geographicalAreaSelected )
74- ) {
75- this . recordImportWizards . push ( recordImportWizard )
76- } else if (
77- this . geographicalAreaSelected === 'All' &&
78- recordImportWizard . actTypes . includes ( this . workTypeSelected )
79- ) {
80- this . recordImportWizards . push ( recordImportWizard )
81- } else if (
82- recordImportWizard . actTypes . includes ( this . workTypeSelected ) &&
83- recordImportWizard . geoAreas . includes ( this . geographicalAreaSelected )
84- ) {
85- this . recordImportWizards . push ( recordImportWizard )
86- }
87- } )
74+ if (
75+ this . workTypeSelected === 'All' &&
76+ this . geographicalAreaSelected === 'All'
77+ ) {
78+ this . recordImportWizards = [ ... this . recordImportWizardsOriginal ]
79+ } else {
80+ this . recordImportWizards = this . recordImportWizardsOriginal . filter (
81+ ( recordImportWizard ) => {
82+ const matchWorkType =
83+ this . workTypeSelected === 'All' ||
84+ ( recordImportWizard . actTypes ?. length
85+ ? recordImportWizard . actTypes . includes ( this . workTypeSelected )
86+ : true )
87+ const matchGeo =
88+ this . geographicalAreaSelected === 'All' ||
89+ ( recordImportWizard . geoAreas ?. length
90+ ? recordImportWizard . geoAreas . includes (
91+ this . geographicalAreaSelected
92+ )
93+ : true )
94+ return matchWorkType && matchGeo
95+ }
96+ )
97+ }
8898 this . total = this . recordImportWizards . length
8999 }
90100
0 commit comments