@@ -9,6 +9,27 @@ import { TC_API_URL } from '../../../../config/constants'
99
1010let cachedOptions
1111
12+ /**
13+ * If `categoriesMapping` is defined - filter options using selected categories.
14+ * Otherwise returns all `options`.
15+ *
16+ * @param {Object } categoriesMapping form data and API model categories mapping
17+ * @param {Array } selectedCategories selected categories in the form
18+ * @param {Array } options all possible options
19+ *
20+ * @returns {Array } available options
21+ */
22+ const getAvailableOptions = ( categoriesMapping , selectedCategories , options ) => {
23+ if ( categoriesMapping ) {
24+ const mappedCategories = _ . map ( selectedCategories , ( category ) => categoriesMapping [ category ] ? categoriesMapping [ category ] . toLowerCase ( ) : null )
25+ const availableOptions = options . filter ( option => _ . intersection ( ( option . categories || [ ] ) . map ( c => c . toLowerCase ( ) ) , mappedCategories ) . length > 0 )
26+
27+ return availableOptions
28+ }
29+
30+ return options
31+ }
32+
1233class SkillsQuestion extends React . Component {
1334 constructor ( props ) {
1435 super ( props )
@@ -56,12 +77,9 @@ class SkillsQuestion extends React.Component {
5677 const selectedCategories = _ . get ( currentProjectData , categoriesField , [ ] )
5778
5879 if ( selectedCategories . length !== prevSelectedCategories . length ) {
59- const mappedPrevSelectedCategories = _ . map ( prevSelectedCategories , ( category ) => categoriesMapping [ category ] ? categoriesMapping [ category ] . toLowerCase ( ) : null )
60- const mappedSelectedCategories = _ . map ( selectedCategories , ( category ) => categoriesMapping [ category ] ? categoriesMapping [ category ] . toLowerCase ( ) : null )
61-
6280 const currentValues = getValue ( ) || [ ]
63- const prevAvailableOptions = options . filter ( option => _ . intersection ( ( option . categories || [ ] ) . map ( c => c . toLowerCase ( ) ) , mappedPrevSelectedCategories ) . length > 0 )
64- const nextAvailableOptions = options . filter ( option => _ . intersection ( ( option . categories || [ ] ) . map ( c => c . toLowerCase ( ) ) , mappedSelectedCategories ) . length > 0 )
81+ const prevAvailableOptions = getAvailableOptions ( categoriesMapping , prevSelectedCategories , options )
82+ const nextAvailableOptions = getAvailableOptions ( categoriesMapping , selectedCategories , options )
6583 const prevValues = currentValues . filter ( skill => _ . some ( prevAvailableOptions , skill ) )
6684 const nextValues = currentValues . filter ( skill => _ . some ( nextAvailableOptions , skill ) )
6785
@@ -112,13 +130,10 @@ class SkillsQuestion extends React.Component {
112130 const { options, customOptionValue } = this . state
113131
114132 const selectedCategories = _ . get ( currentProjectData , categoriesField , [ ] )
115- const mappedCategories = categoriesMapping && _ . map ( selectedCategories , ( category ) => categoriesMapping [ category ] ? categoriesMapping [ category ] . toLowerCase ( ) : null )
116133
117134 // if have a mapping for categories, then filter options, otherwise use all options
118- const availableOptions = ( categoriesMapping
119- ? options . filter ( option => _ . intersection ( ( option . categories || [ ] ) . map ( c => c . toLowerCase ( ) ) , mappedCategories ) . length > 0 )
120- : options
121- ) . map ( option => _ . pick ( option , [ 'id' , 'name' ] ) )
135+ const availableOptions = getAvailableOptions ( categoriesMapping , selectedCategories , options )
136+ . map ( option => _ . pick ( option , [ 'id' , 'name' ] ) )
122137
123138 let currentValues = getValue ( ) || [ ]
124139 // remove from currentValues not available options but still keep created custom options without id
0 commit comments