Skip to content

Commit

Permalink
add filter option to orderableDocumentListDeskItem
Browse files Browse the repository at this point in the history
  • Loading branch information
plsrd committed May 26, 2022
1 parent a4b7de5 commit df3fb19
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default () =>
// Optional configuration
orderableDocumentListDeskItem({
type: 'project',
filter: `'e-fca35c8b2edc19f66675e200d2b376b8' in teamMembers[]._ref`
title: 'Projects',
icon: Paint
}),
Expand Down Expand Up @@ -97,7 +98,6 @@ To get this first version out the door there are few configuration settings and
- The `name` of the `orderRank` field is constant
- The ability to only sort across _all_ Documents of a `type`
- The absence of a `filter` configuration on the Document List
Feedback and PRs welcome :)
Expand Down
5 changes: 3 additions & 2 deletions src/DocumentListQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const client = sanityClient.withConfig({
apiVersion: '2021-09-01',
})

export default function DocumentListQuery({type}) {
export default function DocumentListQuery({type, filter}) {
const [isLoading, setIsLoading] = useState(true)
const [listIsUpdating, setListIsUpdating] = useState(false)
const [data, setData] = useState([])

useEffect(() => {
const query = `*[_type == $type]|order(@[$order] asc){
const query = `*[_type == $type ${filter ? '&&' + filter : ''}]|order(@[$order] asc){
_id, _type, ${ORDER_FIELD_NAME}
}`
const queryParams = {type, order: ORDER_FIELD_NAME}
Expand Down Expand Up @@ -99,4 +99,5 @@ export default function DocumentListQuery({type}) {

DocumentListQuery.propTypes = {
type: PropTypes.string.isRequired,
type: PropTypes.string
}
5 changes: 3 additions & 2 deletions src/DocumentListWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Feedback from './Feedback'

// 1. Validate first that the schema has been configured for ordering
// 2. Setup context for showIncrements
export default function DocumentListWrapper({type, showIncrements, resetOrderTransaction}) {
export default function DocumentListWrapper({type, filter, showIncrements, resetOrderTransaction}) {
const toast = useToast()

useEffect(() => {
Expand Down Expand Up @@ -76,14 +76,15 @@ export default function DocumentListWrapper({type, showIncrements, resetOrderTra

return (
<OrderableContext.Provider value={{showIncrements}}>
<DocumentListQuery type={type} />
<DocumentListQuery type={type} filter={filter}/>
</OrderableContext.Provider>
)
}

DocumentListWrapper.propTypes = {
showIncrements: PropTypes.bool.isRequired,
type: PropTypes.string.isRequired,
filter: PropTypes.string,
resetOrderTransaction: PropTypes.shape({
title: PropTypes.string,
status: PropTypes.string,
Expand Down
2 changes: 2 additions & 0 deletions src/OrderableDocumentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class OrderableDocumentList extends Component {
static propTypes = {
options: PropTypes.shape({
type: PropTypes.string,
filter: PropTypes.string
}).isRequired,
}

Expand Down Expand Up @@ -56,6 +57,7 @@ export default class OrderableDocumentList extends Component {
return (
<DocumentListWrapper
type={this?.props?.options?.type}
filter={this?.props?.options?.filter}
showIncrements={this.state.showIncrements}
resetOrderTransaction={this.state.resetOrderTransaction}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/desk-structure/orderableDocumentListDeskItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function orderableDocumentListDeskItem(config = {}) {
`)
}

const {type, title, icon} = config
const {type, filter, title, icon} = config

const listTitle = title ?? `Orderable ${type}`
const listId = `orderable-${type}`
Expand All @@ -33,7 +33,7 @@ export function orderableDocumentListDeskItem(config = {}) {

type: 'component',
component: OrderableDocumentList,
options: {type},
options: {type, filter},
menuItems: [
S.menuItem()
.title(`Create new ${typeTitle}`)
Expand Down

0 comments on commit df3fb19

Please sign in to comment.