@ -11,24 +11,23 @@ import * as api from '../../api'
import { apiPath , siteBasePath } from '../../util/url'
export const FILTER _GROUPS = {
'page' : [ 'page' ],
'page' : [ 'page' , 'entry_page' , 'exit_page' ],
'source' : [ 'source' , 'referrer' ] ,
'location' : [ 'country' , 'region' , 'city' ] ,
'screen' : [ 'screen' ] ,
'browser' : [ 'browser' , 'browser_version' ] ,
'os' : [ 'os' , 'os_version' ] ,
'utm' : [ 'utm_medium' , 'utm_source' , 'utm_campaign' , 'utm_term' , 'utm_content' ] ,
'entry_page' : [ 'entry_page' ] ,
'exit_page' : [ 'exit_page' ] ,
'goal' : [ 'goal' ]
}
function getFormState ( filterGroup , query ) {
return FILTER _GROUPS [ filterGroup ] . reduce ( ( result , filter ) => {
const filterValue = query . filters [ filter ] || ''
let filterName = filterValue
const rawFilterValue = query . filters [ filter ] || ''
const type = toFilterType ( rawFilterValue )
const filterValue = valueWithoutPrefix ( rawFilterValue )
const type = filterValue [ 0 ] === '!' ? 'is_not' : 'is'
let filterName = filterValue
if ( filter === 'country' && filterValue !== '' ) {
filterName = ( new URLSearchParams ( window . location . search ) ) . get ( 'country_name' )
@ -43,6 +42,38 @@ function getFormState(filterGroup, query) {
} , { } )
}
const FILTER _TYPES = {
isNot : 'is not' ,
contains : 'contains' ,
is : 'is'
} ;
const FILTER _PREFIXES = {
[ FILTER _TYPES . isNot ] : '!' ,
[ FILTER _TYPES . contains ] : '~' ,
[ FILTER _TYPES . is ] : ''
} ;
export function toFilterType ( value ) {
return Object . keys ( FILTER _PREFIXES )
. find ( type => FILTER _PREFIXES [ type ] === value [ 0 ] ) || FILTER _TYPES . is ;
}
export function valueWithoutPrefix ( value ) {
return [ FILTER _TYPES . isNot , FILTER _TYPES . contains ] . includes ( toFilterType ( value ) )
? value . substring ( 1 )
: value ;
}
function toFilterQuery ( value , type ) {
const prefix = FILTER _PREFIXES [ type ] ;
return prefix + value . trim ( ) ;
}
function supportsContains ( filterName ) {
return [ 'page' , 'entry_page' , 'exit_page' ] . includes ( filterName )
}
function withIndefiniteArticle ( word ) {
if ( word . startsWith ( 'UTM' ) ) {
return ` a ${ word } `
@ -113,10 +144,7 @@ class FilterModal extends React.Component {
if ( filterKey === 'region' ) { res . push ( { filter : 'region_name' , value : name } ) }
if ( filterKey === 'city' ) { res . push ( { filter : 'city_name' , value : name } ) }
let finalFilterValue = value
finalFilterValue = ( type === 'is_not' ? '!' : '' ) + finalFilterValue . trim ( )
res . push ( { filter : filterKey , value : finalFilterValue } )
res . push ( { filter : filterKey , value : toFilterQuery ( value , type ) } )
return res
} , [ ] )
@ -239,34 +267,9 @@ class FilterModal extends React.Component {
className = "z-10 origin-top-left absolute left-0 mt-2 w-24 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none"
>
< div className = "py-1" >
< Menu . Item >
{ ( { active } ) => (
< span
onClick = { ( ) => this . setFilterType ( filterName , 'is' ) }
className = { classNames (
active ? 'bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-100' : 'text-gray-700 dark:text-gray-200' ,
'cursor-pointer block px-4 py-2 text-sm'
) }
>
is
< / s p a n >
) }
< / M e n u . I t e m >
{ filterName !== 'goal' && (
< Menu . Item >
{ ( { active } ) => (
< span
onClick = { ( ) => this . setFilterType ( filterName , 'is_not' ) }
className = { classNames (
active ? 'bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-100' : 'text-gray-700 dark:text-gray-200' ,
'cursor-pointer block px-4 py-2 text-sm'
) }
>
is not
< / s p a n >
) }
< / M e n u . I t e m >
) }
{ this . renderTypeItem ( filterName , FILTER _TYPES . is , true ) }
{ this . renderTypeItem ( filterName , FILTER _TYPES . isNot , filterName !== 'goal' ) }
{ this . renderTypeItem ( filterName , FILTER _TYPES . contains , supportsContains ( filterName ) ) }
< / d i v >
< / M e n u . I t e m s >
< / T r a n s i t i o n >
@ -276,9 +279,29 @@ class FilterModal extends React.Component {
)
}
renderBody ( ) {
const { selectedFilterGroup , query } = this . state ;
const showClear = FILTER _GROUPS [ selectedFilterGroup ] . some ( ( filterName ) => query . filters [ filterName ] )
renderTypeItem ( filterName , type , shouldDisplay ) {
return (
shouldDisplay && (
< Menu . Item >
{ ( { active } ) => (
< span
onClick = { ( ) => this . setFilterType ( filterName , type ) }
className = { classNames (
active ? "bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-100" : "text-gray-700 dark:text-gray-200" ,
"cursor-pointer block px-4 py-2 text-sm"
) }
>
{ type }
< / s p a n >
) }
< / M e n u . I t e m >
)
) ;
}
renderBody ( ) {
const { selectedFilterGroup , query } = this . state ;
const showClear = FILTER _GROUPS [ selectedFilterGroup ] . some ( ( filterName ) => query . filters [ filterName ] )
return (
< >