Skip to content

Commit

Permalink
[base] Create part of referring documents hoc/util-component (#1188)
Browse files Browse the repository at this point in the history
This is a tiny refactor of the `enhanceWithReferringDocuments` utility used in desk tool for fetching a list of document that refers to the one you are attempting to unpublish/delete. Since it is useful for other packages as well, i moved it into base and made a sanity-part of it.

It has been rewritten to be a component that takes a render func as child, so it can be used like this:

```jsx
import {WithReferringDocuments} from 'part:@sanity/base/with-referring-documents'
function MyComponent(props) {
  return (
    <WithReferringDocuments id={props.documentId}>
      {({isLoading, referringDocuments}) =>
        isLoading
          ? 'Loading referring documents…'
          : `Found ${referringDocuments.length} documents referring to ${props.documentId}`
      }
    </WithReferringDocuments>
  )
}
```

The old higher order component behavior is kept in desk tool for now, but should probably be replaced by using `WithReferringDocuments` directly
  • Loading branch information
bjoerge authored and Kristoffer Sivertsen committed Jan 31, 2019
1 parent 2c20fad commit c2fe33d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
8 changes: 8 additions & 0 deletions packages/@sanity/base/sanity.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@
"name": "part:@sanity/base/query-container",
"description": "Wraps a query and passes down results as props to its child component"
},
{
"name": "part:@sanity/base/with-referring-documents",
"description": "A utility component that takes a document id and calls its child render prop with a list of documents referring to it"
},
{
"name": "part:@sanity/base/theme/layout/resets-style",
"description": "Provides resets for resetting browser styling"
Expand Down Expand Up @@ -410,6 +414,10 @@
"implements": "part:@sanity/base/query-container",
"path": "components/QueryContainer"
},
{
"implements": "part:@sanity/base/with-referring-documents",
"path": "components/WithReferringDocuments"
},
{
"implements": "part:@sanity/base/locale/formatters",
"path": "components/IntlWrapper.js"
Expand Down
25 changes: 25 additions & 0 deletions packages/@sanity/base/src/components/WithReferringDocuments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import documentStore from 'part:@sanity/base/datastore/document'
import {withPropsStream} from 'react-props-stream'
import {concat, of} from 'rxjs'
import {distinctUntilChanged, map, switchMap} from 'rxjs/operators'

const loadProps = receivedProps$ =>
receivedProps$.pipe(
distinctUntilChanged((prev, next) => prev.id === next.id),
switchMap(receivedProps =>
concat(
of({...receivedProps, referringDocuments: [], isLoading: true}),
documentStore.query('*[references($docId)] [0...101]', {docId: receivedProps.id}).pipe(
map(event => ({
...receivedProps,
referringDocuments: event.documents,
isLoading: false
}))
)
)
)
)

export const WithReferringDocuments = withPropsStream(loadProps, ({children, ...props}) =>
children(props)
)
Original file line number Diff line number Diff line change
@@ -1,51 +1,26 @@
import React from 'react'
import PropTypes from 'prop-types'
import documentStore from 'part:@sanity/base/datastore/document'
import {WithReferringDocuments} from 'part:@sanity/base/with-referring-documents'

export default function withReferringDocuments(Component) {
return class extends React.PureComponent {
static displayName = `enhanceWithAvailHeight(${Component.displayName || Component.name})`

static propTypes = {
published: PropTypes.object
}

state = {
isLoading: false,
referringDocuments: []
}

componentDidMount() {
const {published} = this.props
if (!published) {
return
}
this.setState({isLoading: true})
this.refSubscription = documentStore
.query('*[references($docId)] [0...101]', {docId: published._id})
.subscribe(event => {
this.setState({
referringDocuments: event.documents || [],
isLoading: false
})
})
}

componentWillUnmount() {
if (this.refSubscription) {
this.refSubscription.unsubscribe()
}
}
export default function enhanceWithReferringDocuments(Component) {
function EnhancedWithReferringDocuments(props) {
return (
<WithReferringDocuments id={props.id}>
{({isLoading, referringDocuments}) => (
<Component
{...props}
referringDocuments={referringDocuments}
isCheckingReferringDocuments={isLoading}
/>
)}
</WithReferringDocuments>
)
}

render() {
const {isLoading, referringDocuments} = this.state
return (
<Component
{...this.props}
referringDocuments={referringDocuments}
isCheckingReferringDocuments={isLoading}
/>
)
}
EnhancedWithReferringDocuments.displayName = `enhanceWithReferringDocuments(${Component.displayName ||
Component.name})`
EnhancedWithReferringDocuments.propTypes = {
published: PropTypes.object
}
return EnhancedWithReferringDocuments
}

0 comments on commit c2fe33d

Please sign in to comment.