-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[base] Create part of referring documents hoc/util-component (#1188)
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. Additionally it is usable not only a higher order component, but also as 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> ) } ```
- Loading branch information
1 parent
f6dde53
commit 0e7d301
Showing
3 changed files
with
55 additions
and
45 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
25 changes: 25 additions & 0 deletions
25
packages/@sanity/base/src/components/WithReferringDocuments.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,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) | ||
) |
67 changes: 22 additions & 45 deletions
67
packages/@sanity/desk-tool/src/components/enhanceWithReferringDocuments.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 |
---|---|---|
@@ -1,51 +1,28 @@ | ||
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) { | ||
// eslint-disable-next-line react/no-multi-comp | ||
const renderChild = ({isLoading, referringDocuments}) => ( | ||
<Component | ||
{...props} | ||
referringDocuments={referringDocuments} | ||
isCheckingReferringDocuments={isLoading} | ||
/> | ||
) | ||
return props.published ? ( | ||
<WithReferringDocuments id={props.published._id}>{renderChild}</WithReferringDocuments> | ||
) : ( | ||
renderChild({referringDocuments: [], isLoading: false}) | ||
) | ||
} | ||
|
||
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 | ||
} |