-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint fixes (+3 squashed commits) (#55773)
Squashed commits: [b107c819e2] Feedback application [8ba71aaab4] Post rebase conflicts [13eb412e35] Add: Ability to crate custom DataViews.
- Loading branch information
1 parent
ce3ef3e
commit bc704e1
Showing
6 changed files
with
406 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
packages/edit-site/src/components/sidebar-dataviews/add-new-view.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Modal, | ||
TextControl, | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
Button, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useDispatch, resolveSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useState } from '@wordpress/element'; | ||
import { plus } from '@wordpress/icons'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SidebarNavigationItem from '../sidebar-navigation-item'; | ||
import DEFAULT_VIEWS from './default-views'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
const { useHistory, useLocation } = unlock( routerPrivateApis ); | ||
|
||
function AddNewItemModalContent( { type, setIsAdding } ) { | ||
const { | ||
params: { path }, | ||
} = useLocation(); | ||
const history = useHistory(); | ||
const { saveEntityRecord } = useDispatch( coreStore ); | ||
const [ title, setTitle ] = useState( '' ); | ||
const [ isSaving, setIsSaving ] = useState( false ); | ||
return ( | ||
<form | ||
onSubmit={ async ( event ) => { | ||
event.preventDefault(); | ||
setIsSaving( true ); | ||
const { getEntityRecords } = resolveSelect( coreStore ); | ||
let dataViewTaxonomyId; | ||
const dataViewTypeRecords = await getEntityRecords( | ||
'taxonomy', | ||
'wp_dataviews_type', | ||
{ slug: type } | ||
); | ||
if ( dataViewTypeRecords && dataViewTypeRecords.length > 0 ) { | ||
dataViewTaxonomyId = dataViewTypeRecords[ 0 ].id; | ||
} else { | ||
const record = await saveEntityRecord( | ||
'taxonomy', | ||
'wp_dataviews_type', | ||
{ name: type } | ||
); | ||
if ( record && record.id ) { | ||
dataViewTaxonomyId = record.id; | ||
} | ||
} | ||
const savedRecord = await saveEntityRecord( | ||
'postType', | ||
'wp_dataviews', | ||
{ | ||
title, | ||
status: 'publish', | ||
wp_dataviews_type: dataViewTaxonomyId, | ||
content: JSON.stringify( | ||
DEFAULT_VIEWS[ type ][ 0 ].view | ||
), | ||
} | ||
); | ||
history.push( { | ||
path, | ||
activeView: savedRecord.id, | ||
isCustom: 'true', | ||
} ); | ||
setIsSaving( false ); | ||
setIsAdding( false ); | ||
} } | ||
> | ||
<VStack spacing="5"> | ||
<TextControl | ||
__nextHasNoMarginBottom | ||
label={ __( 'Name' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
placeholder={ __( 'My view' ) } | ||
className="patterns-create-modal__name-input" | ||
/> | ||
<HStack justify="right"> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => { | ||
setIsAdding( false ); | ||
} } | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
|
||
<Button | ||
variant="primary" | ||
type="submit" | ||
aria-disabled={ ! title || isSaving } | ||
isBusy={ isSaving } | ||
> | ||
{ __( 'Create' ) } | ||
</Button> | ||
</HStack> | ||
</VStack> | ||
</form> | ||
); | ||
} | ||
|
||
export default function AddNewItem( { type } ) { | ||
const [ isAdding, setIsAdding ] = useState( false ); | ||
return ( | ||
<> | ||
<SidebarNavigationItem | ||
icon={ plus } | ||
onClick={ () => { | ||
setIsAdding( true ); | ||
} } | ||
className="dataviews__siderbar-content-add-new-item" | ||
> | ||
{ __( 'New view' ) } | ||
</SidebarNavigationItem> | ||
{ isAdding && ( | ||
<Modal | ||
title={ __( 'Add new view' ) } | ||
onRequestClose={ () => { | ||
setIsAdding( false ); | ||
} } | ||
overlayClassName="" | ||
> | ||
<AddNewItemModalContent | ||
type={ type } | ||
setIsAdding={ setIsAdding } | ||
/> | ||
</Modal> | ||
) } | ||
</> | ||
); | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/edit-site/src/components/sidebar-dataviews/custom-dataviews-list.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { __experimentalItemGroup as ItemGroup } from '@wordpress/components'; | ||
import { useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import DataViewItem from './dataview-item'; | ||
import AddNewItem from './add-new-view'; | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
function CustomDataViewItem( { dataviewId, isActive } ) { | ||
const { dataview } = useSelect( | ||
( select ) => { | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
return { | ||
dataview: getEditedEntityRecord( | ||
'postType', | ||
'wp_dataviews', | ||
dataviewId | ||
), | ||
}; | ||
}, | ||
[ dataviewId ] | ||
); | ||
const type = useMemo( () => { | ||
const viewContent = JSON.parse( dataview.content ); | ||
return viewContent.type; | ||
}, [ dataview.content ] ); | ||
return ( | ||
<DataViewItem | ||
title={ dataview.title } | ||
type={ type } | ||
isActive={ isActive } | ||
isCustom="true" | ||
customViewId={ dataviewId } | ||
/> | ||
); | ||
} | ||
|
||
export function useCustomDataViews( type ) { | ||
const customDataViews = useSelect( ( select ) => { | ||
const { getEntityRecords } = select( coreStore ); | ||
const dataViewTypeRecords = getEntityRecords( | ||
'taxonomy', | ||
'wp_dataviews_type', | ||
{ slug: type } | ||
); | ||
if ( ! dataViewTypeRecords || dataViewTypeRecords.length === 0 ) { | ||
return EMPTY_ARRAY; | ||
} | ||
const dataViews = getEntityRecords( 'postType', 'wp_dataviews', { | ||
wp_dataviews_type: dataViewTypeRecords[ 0 ].id, | ||
orderby: 'date', | ||
order: 'asc', | ||
} ); | ||
if ( ! dataViews ) { | ||
return EMPTY_ARRAY; | ||
} | ||
return dataViews; | ||
} ); | ||
return customDataViews; | ||
} | ||
|
||
export default function CustomDataViewsList( { type, activeView, isCustom } ) { | ||
const customDataViews = useCustomDataViews( type ); | ||
return ( | ||
<ItemGroup> | ||
{ customDataViews.map( ( customViewRecord ) => { | ||
return ( | ||
<CustomDataViewItem | ||
key={ customViewRecord.id } | ||
dataviewId={ customViewRecord.id } | ||
isActive={ | ||
isCustom === 'true' && | ||
Number( activeView ) === customViewRecord.id | ||
} | ||
/> | ||
); | ||
} ) } | ||
<AddNewItem type={ type } /> | ||
</ItemGroup> | ||
); | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/edit-site/src/components/sidebar-dataviews/dataview-item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { page, columns } from '@wordpress/icons'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useLink } from '../routes/link'; | ||
import SidebarNavigationItem from '../sidebar-navigation-item'; | ||
import { unlock } from '../../lock-unlock'; | ||
const { useLocation } = unlock( routerPrivateApis ); | ||
|
||
function getDataViewIcon( type ) { | ||
const icons = { list: page, grid: columns }; | ||
return icons[ type ]; | ||
} | ||
|
||
export default function DataViewItem( { | ||
title, | ||
slug, | ||
customViewId, | ||
type, | ||
icon, | ||
isActive, | ||
isCustom, | ||
} ) { | ||
const { | ||
params: { path }, | ||
} = useLocation(); | ||
|
||
const iconToUse = icon || getDataViewIcon( type ); | ||
|
||
const linkInfo = useLink( { | ||
path, | ||
activeView: isCustom === 'true' ? customViewId : slug, | ||
isCustom, | ||
} ); | ||
return ( | ||
<SidebarNavigationItem | ||
icon={ iconToUse } | ||
{ ...linkInfo } | ||
aria-current={ isActive ? 'true' : undefined } | ||
> | ||
{ title } | ||
</SidebarNavigationItem> | ||
); | ||
} |
Oops, something went wrong.