2019-02-05 18:12:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { find, get } from 'lodash';
|
|
|
|
import { flattenFilters } from '@woocommerce/navigation';
|
|
|
|
|
|
|
|
export const DEFAULT_FILTER = 'all';
|
|
|
|
|
|
|
|
export function getSelectedFilter( filters, query, selectedFilterArgs = {} ) {
|
2019-03-06 20:12:28 +00:00
|
|
|
if ( ! filters || filters.length === 0 ) {
|
2019-02-05 18:12:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:12:28 +00:00
|
|
|
const clonedFilters = filters.slice( 0 );
|
|
|
|
const filterConfig = clonedFilters.pop();
|
2019-02-05 18:12:58 +00:00
|
|
|
|
|
|
|
if ( filterConfig.showFilters( query, selectedFilterArgs ) ) {
|
|
|
|
const allFilters = flattenFilters( filterConfig.filters );
|
2020-02-14 02:23:21 +00:00
|
|
|
const value =
|
|
|
|
query[ filterConfig.param ] ||
|
|
|
|
filterConfig.defaultValue ||
|
|
|
|
DEFAULT_FILTER;
|
2019-03-06 20:12:28 +00:00
|
|
|
return find( allFilters, { value } );
|
2019-02-05 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:12:28 +00:00
|
|
|
return getSelectedFilter( clonedFilters, query, selectedFilterArgs );
|
2019-02-05 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:12:28 +00:00
|
|
|
export function getChartMode( selectedFilter, query ) {
|
|
|
|
if ( selectedFilter && query ) {
|
2020-02-14 02:23:21 +00:00
|
|
|
const selectedFilterParam = get( selectedFilter, [
|
|
|
|
'settings',
|
|
|
|
'param',
|
|
|
|
] );
|
|
|
|
|
|
|
|
if (
|
|
|
|
! selectedFilterParam ||
|
|
|
|
Object.keys( query ).includes( selectedFilterParam )
|
|
|
|
) {
|
2019-03-06 20:12:28 +00:00
|
|
|
return get( selectedFilter, [ 'chartMode' ] );
|
|
|
|
}
|
2019-02-05 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:12:28 +00:00
|
|
|
return null;
|
2019-02-05 18:12:58 +00:00
|
|
|
}
|