2019-02-05 18:12:58 +00:00
|
|
|
/** @format */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { find, get } from 'lodash';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce dependencies
|
|
|
|
*/
|
|
|
|
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 );
|
2019-05-07 20:46:58 +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 ) {
|
|
|
|
const selectedFilterParam = get( selectedFilter, [ 'settings', 'param' ] );
|
|
|
|
|
|
|
|
if ( ! selectedFilterParam || Object.keys( query ).includes( selectedFilterParam ) ) {
|
|
|
|
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
|
|
|
}
|