2018-08-08 21:24:48 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import history from 'lib/history';
|
|
|
|
import { parse, stringify } from 'qs';
|
2018-09-17 02:26:34 +00:00
|
|
|
import { uniq } from 'lodash';
|
2018-08-08 21:24:48 +00:00
|
|
|
|
|
|
|
/**
|
2018-09-17 16:50:20 +00:00
|
|
|
* Returns a string with the site's wp-admin URL appended. JS version of `admin_url`.
|
2018-08-08 21:24:48 +00:00
|
|
|
*
|
2018-09-17 16:50:20 +00:00
|
|
|
* @param {String} path Relative path.
|
|
|
|
* @return {String} Full admin URL.
|
2018-08-08 21:24:48 +00:00
|
|
|
*/
|
2018-09-17 16:50:20 +00:00
|
|
|
export const getAdminLink = path => wcSettings.adminUrl + path;
|
2018-08-08 21:24:48 +00:00
|
|
|
|
|
|
|
/**
|
2018-09-17 16:50:20 +00:00
|
|
|
* Get the current path from history.
|
2018-08-08 21:24:48 +00:00
|
|
|
*
|
2018-09-17 16:50:20 +00:00
|
|
|
* @return {String} Current path.
|
2018-08-08 21:24:48 +00:00
|
|
|
*/
|
2018-09-17 16:50:20 +00:00
|
|
|
export const getPath = () => history.location.pathname;
|
2018-08-08 21:24:48 +00:00
|
|
|
|
|
|
|
/**
|
2018-09-17 16:50:20 +00:00
|
|
|
* Converts a query object to a query string.
|
2018-08-08 21:24:48 +00:00
|
|
|
*
|
2018-09-17 16:50:20 +00:00
|
|
|
* @param {Object} query parameters to be converted.
|
|
|
|
* @return {String} Query string.
|
2018-08-08 21:24:48 +00:00
|
|
|
*/
|
2018-09-17 16:50:20 +00:00
|
|
|
export const stringifyQuery = query => ( query ? '?' + stringify( query ) : '' );
|
2018-08-08 21:24:48 +00:00
|
|
|
|
2018-09-03 02:08:13 +00:00
|
|
|
/**
|
2018-09-17 16:50:20 +00:00
|
|
|
* Get an array of IDs from a comma-separated query parameter.
|
2018-09-03 02:08:13 +00:00
|
|
|
*
|
2018-09-17 16:50:20 +00:00
|
|
|
* @param {string} queryString string value extracted from URL.
|
|
|
|
* @return {Array} List of IDs converted to numbers.
|
2018-09-03 02:08:13 +00:00
|
|
|
*/
|
2018-09-17 16:50:20 +00:00
|
|
|
export function getIdsFromQuery( queryString = '' ) {
|
2018-09-17 02:26:34 +00:00
|
|
|
return uniq(
|
|
|
|
queryString
|
|
|
|
.split( ',' )
|
|
|
|
.map( id => parseInt( id, 10 ) )
|
|
|
|
.filter( Boolean )
|
|
|
|
);
|
2018-09-17 16:50:20 +00:00
|
|
|
}
|
2018-09-03 02:08:13 +00:00
|
|
|
|
2018-08-08 21:24:48 +00:00
|
|
|
/**
|
|
|
|
* Return a URL with set query parameters.
|
|
|
|
*
|
|
|
|
* @param {Object} query object of params to be updated.
|
|
|
|
* @param {String} path Relative path (defaults to current path).
|
|
|
|
* @param {Object} currentQuery object of current query params (defaults to current querystring).
|
|
|
|
* @return {String} Updated URL merging query params into existing params.
|
|
|
|
*/
|
2018-09-17 16:50:20 +00:00
|
|
|
export function getNewPath( query, path = getPath(), currentQuery = getQuery() ) {
|
2018-09-03 02:08:13 +00:00
|
|
|
const queryString = stringifyQuery( { ...currentQuery, ...query } );
|
|
|
|
return `${ path }${ queryString }`;
|
2018-09-17 16:50:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current query string, parsed into an object, from history.
|
|
|
|
*
|
|
|
|
* @return {Object} Current query object, defaults to empty object.
|
|
|
|
*/
|
|
|
|
export function getQuery() {
|
|
|
|
const search = history.location.search;
|
|
|
|
if ( search.length ) {
|
|
|
|
return parse( search.substring( 1 ) ) || {};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns an event handler for the given `param`
|
|
|
|
*
|
|
|
|
* @param {string} param The parameter in the querystring which should be updated (ex `page`, `per_page`)
|
|
|
|
* @param {string} path Relative path (defaults to current path).
|
|
|
|
* @param {string} query object of current query params (defaults to current querystring).
|
|
|
|
* @return {function} A callback which will update `param` to the passed value when called.
|
|
|
|
*/
|
|
|
|
export function onQueryChange( param, path = getPath(), query = getQuery() ) {
|
|
|
|
switch ( param ) {
|
|
|
|
case 'sort':
|
|
|
|
return ( key, dir ) => updateQueryString( { orderby: key, order: dir }, path, query );
|
|
|
|
case 'compare':
|
|
|
|
return ( key, ids ) =>
|
|
|
|
updateQueryString( { filter: `compare-${ key }`, [ key ]: ids }, path, query );
|
|
|
|
default:
|
|
|
|
return value => updateQueryString( { [ param ]: value }, path, query );
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 21:24:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the query parameters of the current page.
|
|
|
|
*
|
|
|
|
* @param {Object} query object of params to be updated.
|
2018-08-21 19:03:20 +00:00
|
|
|
* @param {String} path Relative path (defaults to current path).
|
|
|
|
* @param {Object} currentQuery object of current query params (defaults to current querystring).
|
2018-08-08 21:24:48 +00:00
|
|
|
*/
|
2018-09-17 16:50:20 +00:00
|
|
|
export function updateQueryString( query, path = getPath(), currentQuery = getQuery() ) {
|
2018-08-21 19:03:20 +00:00
|
|
|
const newPath = getNewPath( query, path, currentQuery );
|
2018-08-08 21:24:48 +00:00
|
|
|
history.push( newPath );
|
2018-09-17 16:50:20 +00:00
|
|
|
}
|