@@ -27,34 +27,14 @@ import SelectAsync from 'react-select/async';
2727import ValidationLabel from '../common/validation-label' ;
2828import _ from 'lodash' ;
2929import { faQuestionCircle } from '@fortawesome/free-solid-svg-icons' ;
30+ import { RecentlyUsed } from '../../unified-form/common/recently-used' ;
3031import { isValidBBID } from '../../../common/helpers/utils' ;
3132import makeImmutable from './make-immutable' ;
3233import request from 'superagent' ;
33- import { RecentlyUsed } from '../../unified-form/common/recently-used' ;
34-
3534
3635const ImmutableAsyncSelect = makeImmutable ( SelectAsync ) ;
3736
3837class EntitySearchFieldOption extends React . Component {
39- static entityTypeMappings = {
40- //Saving
41- 'Author' : 'authors' ,
42- 'Edition' : 'editions' ,
43- 'EditionGroup' : 'edition_groups' ,
44- 'Publisher' : 'publishers' ,
45- 'Series' : 'series' ,
46- 'Work' : 'works' ,
47- 'Area' : 'areas' ,
48- //Loading
49- 'authors' : 'Author' ,
50- 'editions' : 'Edition' ,
51- 'edition_groups' : 'EditionGroup' ,
52- 'publishers' : 'Publisher' ,
53- 'series' : 'Series' ,
54- 'works' : 'Work' ,
55- 'areas' : 'Area'
56- } ;
57-
5838 constructor ( props ) {
5939 super ( props ) ;
6040 this . selectRef = React . createRef ( ) ;
@@ -64,6 +44,25 @@ class EntitySearchFieldOption extends React.Component {
6444 this . entityToOption = this . entityToOption . bind ( this ) ;
6545 }
6646
47+ static entityTypeMappings = {
48+ // Saving
49+ Area : 'areas' ,
50+ Author : 'authors' ,
51+ Edition : 'editions' ,
52+ EditionGroup : 'editiongroups' ,
53+ Publisher : 'publishers' ,
54+ Series : 'series' ,
55+ Work : 'works' ,
56+ // Loading
57+ areas : 'Area' ,
58+ authors : 'Author' ,
59+ editiongroups : 'EditionGroup' ,
60+ editions : 'Edition' ,
61+ publishers : 'Publisher' ,
62+ series : 'Series' ,
63+ works : 'Work'
64+ } ;
65+
6766 /**
6867 * Determines whether an entity provided to the EntitySearch component is an
6968 * Area, using the present attributes.
@@ -106,16 +105,15 @@ class EntitySearchFieldOption extends React.Component {
106105 return entityOption ;
107106 }
108107
109- getRecentlyUsedOptions ( ) {
110- if ( ! this . props . recentlyUsedEntityType ) {
108+ getRecentlyUsedOptions ( ) {
109+ if ( ! this . props . recentlyUsedEntityType ) {
111110 return [ ] ;
112111 }
113- const entityTypes = Array . isArray ( this . props . recentlyUsedEntityType )
114- ? this . props . recentlyUsedEntityType : [ this . props . recentlyUsedEntityType ] ;
115- const validEntityTypes = entityTypes . filter ( type =>
116- type && typeof type === 'string' && type . trim ( ) . length > 0
117- ) ;
118- if ( validEntityTypes . length === 0 ) {
112+ const entityTypes = Array . isArray ( this . props . recentlyUsedEntityType ) ?
113+ this . props . recentlyUsedEntityType : [ this . props . recentlyUsedEntityType ] ;
114+ const validEntityTypes = entityTypes . filter ( type =>
115+ type && typeof type === 'string' && type . trim ( ) . length > 0 ) ;
116+ if ( validEntityTypes . length === 0 ) {
119117 return [ ] ;
120118 }
121119 const allRecentlyUsedItems = [ ] ;
@@ -130,24 +128,24 @@ class EntitySearchFieldOption extends React.Component {
130128 } ) ;
131129 }
132130 } ) ;
133- if ( allRecentlyUsedItems . length === 0 ) {
131+ if ( allRecentlyUsedItems . length === 0 ) {
134132 return [ ] ;
135133 }
136134 const uniqueItems = _ . uniqBy ( allRecentlyUsedItems , 'id' ) . slice ( 0 , 10 ) ;
137135 return uniqueItems . map ( item => {
138136 const singularType = EntitySearchFieldOption . entityTypeMappings [ item . _sourceType ] || item . _sourceType . replace ( / s $ / , '' ) ;
139137 return {
140138 id : item . id ,
141- text : item . name ,
142139 isRecentlyUsed : true ,
140+ text : item . name ,
143141 type : singularType
144142 } ;
145143 } ) ;
146144 }
147145
148- getDefaultOptions ( ) {
146+ getDefaultOptions ( ) {
149147 const recentOptions = this . getRecentlyUsedOptions ( ) ;
150- if ( recentOptions . length === 0 ) {
148+ if ( recentOptions . length === 0 ) {
151149 return true ;
152150 }
153151 return [ {
@@ -158,12 +156,11 @@ class EntitySearchFieldOption extends React.Component {
158156
159157 async fetchOptions ( query ) {
160158 const recentOptions = this . getRecentlyUsedOptions ( ) ;
161- const uniqueRecentOptions = _ . uniqBy ( recentOptions , 'id' ) . slice ( 0 , 10 ) ;
159+ const uniqueRecentOptions = _ . uniqBy ( recentOptions , 'id' ) . slice ( 0 , 10 ) ;
162160 if ( ! query ) {
163161 if ( uniqueRecentOptions . length === 0 ) {
164162 return [ ] ;
165163 }
166-
167164 return [ {
168165 label : 'Recently Used' ,
169166 options : uniqueRecentOptions
@@ -181,9 +178,8 @@ class EntitySearchFieldOption extends React.Component {
181178 if ( entity && typeof this . props . onChange === 'function' && ( _ . snakeCase ( entity . type ) === this . props . type ||
182179 ( _ . isArray ( this . props . type ) && this . props . type . includes ( entity . type ) ) ) ) {
183180 const entityOption = this . entityToOption ( entity ) ;
184- if ( entityOption . id && entityOption . text && entityOption . type ) {
181+ if ( entityOption . id && entityOption . text && entityOption . type ) {
185182 const storageKey = EntitySearchFieldOption . entityTypeMappings [ entityOption . type ] || `${ entityOption . type . toLowerCase ( ) } s` ;
186-
187183 RecentlyUsed . addItem ( storageKey , {
188184 id : entityOption . id ,
189185 name : entityOption . text
@@ -209,8 +205,8 @@ class EntitySearchFieldOption extends React.Component {
209205 ) ;
210206 const filteredOptions = response . body . filter ( combinedFilters ) ;
211207 const searchResults = filteredOptions . map ( this . entityToOption ) ;
212- if ( uniqueRecentOptions . length > 0 ) {
213- return [
208+ if ( uniqueRecentOptions . length > 0 ) {
209+ return [
214210 {
215211 label : 'Recently Used' ,
216212 options : uniqueRecentOptions
@@ -272,8 +268,8 @@ class EntitySearchFieldOption extends React.Component {
272268 const wrappedSelect = (
273269 < SelectWrapper
274270 { ...this . props }
275- cacheOptions
276271 blurInputOnSelect
272+ cacheOptions
277273 isClearable
278274 className = { `Select${ this . props . className ? ` ${ this . props . className } ` : '' } ` }
279275 classNamePrefix = "react-select"
@@ -319,11 +315,11 @@ EntitySearchFieldOption.propTypes = {
319315 isMulti : PropTypes . bool ,
320316 label : PropTypes . string ,
321317 languageOptions : PropTypes . array ,
318+ onChange : PropTypes . func . isRequired ,
322319 recentlyUsedEntityType : PropTypes . oneOfType ( [
323320 PropTypes . string ,
324321 PropTypes . arrayOf ( PropTypes . string )
325322 ] ) ,
326- onChange : PropTypes . func . isRequired ,
327323 tooltipText : PropTypes . string ,
328324 type : PropTypes . oneOfType ( [
329325 PropTypes . string ,
0 commit comments