-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dataviews: Add some client side data handling utils #57488
Changes from all commits
706b378
5d60ac0
9bd1be1
ab5feff
a4c8fac
ea4b2ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as DataViews } from './dataviews'; | ||
export { sortByTextFields, getPaginationResults } from './utils'; | ||
export { VIEW_LAYOUTS } from './constants'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Helper util to sort data by text fields, when sorting is done client side. | ||
* | ||
* @param {Object} params Function params. | ||
* @param {Object[]} params.data Data to sort. | ||
* @param {Object} params.view Current view object. | ||
* @param {Object[]} params.fields Array of available fields. | ||
* @param {string[]} params.textFields Array of the field ids to sort. | ||
* | ||
* @return {Object[]} Sorted data. | ||
*/ | ||
export const sortByTextFields = ( { data, view, fields, textFields } ) => { | ||
const sortedData = [ ...data ]; | ||
const fieldId = view.sort.field; | ||
if ( textFields.includes( fieldId ) ) { | ||
const fieldToSort = fields.find( ( field ) => { | ||
return field.id === fieldId; | ||
} ); | ||
sortedData.sort( ( a, b ) => { | ||
const valueA = fieldToSort.getValue( { item: a } ) ?? ''; | ||
const valueB = fieldToSort.getValue( { item: b } ) ?? ''; | ||
return view.sort.direction === 'asc' | ||
? valueA.localeCompare( valueB ) | ||
: valueB.localeCompare( valueA ); | ||
} ); | ||
} | ||
return sortedData; | ||
}; | ||
|
||
/** | ||
* Helper util to get the paginated data and the paginateInfo needed, | ||
* when pagination is done client side. | ||
* | ||
* @param {Object} params Function params. | ||
* @param {Object[]} params.data Available data. | ||
* @param {Object} params.view Current view object. | ||
* | ||
* @return {Object} Paginated data and paginationInfo. | ||
*/ | ||
export function getPaginationResults( { data, view } ) { | ||
const start = ( view.page - 1 ) * view.perPage; | ||
const totalItems = data?.length || 0; | ||
data = data?.slice( start, start + view.perPage ); | ||
return { | ||
data, | ||
paginationInfo: { | ||
totalItems, | ||
totalPages: Math.ceil( totalItems / view.perPage ), | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,11 @@ import { | |
BlockPreview, | ||
privateApis as blockEditorPrivateApis, | ||
} from '@wordpress/block-editor'; | ||
import { DataViews } from '@wordpress/dataviews'; | ||
import { | ||
DataViews, | ||
sortByTextFields, | ||
getPaginationResults, | ||
} from '@wordpress/dataviews'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
|
@@ -252,10 +256,10 @@ export default function DataviewsTemplates() { | |
[ authors, view.type ] | ||
); | ||
|
||
const { shownTemplates, paginationInfo } = useMemo( () => { | ||
const { data, paginationInfo } = useMemo( () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this hook become a reusable hook that filters and sorts and everything based on "view", "allData" and "fields"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can explore this separately better because I think we need to advance the filters API more and see how we can abstract it best. Right now there is a lot of specific code regarding the filtering(see author in templates). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem specific at all for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course, eventually we shouldn't have much helpers.. |
||
if ( ! allTemplates ) { | ||
return { | ||
shownTemplates: EMPTY_ARRAY, | ||
data: EMPTY_ARRAY, | ||
paginationInfo: { totalItems: 0, totalPages: 0 }, | ||
}; | ||
} | ||
|
@@ -301,36 +305,18 @@ export default function DataviewsTemplates() { | |
|
||
// Handle sorting. | ||
if ( view.sort ) { | ||
const stringSortingFields = [ 'title', 'author' ]; | ||
const fieldId = view.sort.field; | ||
if ( stringSortingFields.includes( fieldId ) ) { | ||
const fieldToSort = fields.find( ( field ) => { | ||
return field.id === fieldId; | ||
} ); | ||
filteredTemplates.sort( ( a, b ) => { | ||
const valueA = fieldToSort.getValue( { item: a } ) ?? ''; | ||
const valueB = fieldToSort.getValue( { item: b } ) ?? ''; | ||
return view.sort.direction === 'asc' | ||
? valueA.localeCompare( valueB ) | ||
: valueB.localeCompare( valueA ); | ||
} ); | ||
} | ||
filteredTemplates = sortByTextFields( { | ||
data: filteredTemplates, | ||
view, | ||
fields, | ||
textFields: [ 'title' ], | ||
} ); | ||
} | ||
|
||
// Handle pagination. | ||
const start = ( view.page - 1 ) * view.perPage; | ||
const totalItems = filteredTemplates?.length || 0; | ||
filteredTemplates = filteredTemplates?.slice( | ||
start, | ||
start + view.perPage | ||
); | ||
return { | ||
shownTemplates: filteredTemplates, | ||
paginationInfo: { | ||
totalItems, | ||
totalPages: Math.ceil( totalItems / view.perPage ), | ||
}, | ||
}; | ||
return getPaginationResults( { | ||
data: filteredTemplates, | ||
view, | ||
} ); | ||
}, [ allTemplates, view, fields ] ); | ||
|
||
const resetTemplateAction = useResetTemplateAction(); | ||
|
@@ -381,7 +367,7 @@ export default function DataviewsTemplates() { | |
paginationInfo={ paginationInfo } | ||
fields={ fields } | ||
actions={ actions } | ||
data={ shownTemplates } | ||
data={ data } | ||
isLoading={ isLoadingData } | ||
view={ view } | ||
onChangeView={ onChangeView } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unrelated fix for a bug that I observed here.