Skip to content

Commit

Permalink
[form-builder] Fix slug source not supporting asynchronous resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Feb 19, 2020
1 parent 6a8fa93 commit eb049e1
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions packages/@sanity/form-builder/src/inputs/Slug/SlugInput.tsx
Expand Up @@ -105,27 +105,36 @@ export default withValuePath(
console.error(`Source is missing. Check source on type "${type.name}" in schema`)
return
}
const newFromSource = this.getNewFromSource()

this.setState({loading: true})
this.slugify(newFromSource || '')
this.getNewFromSource()
.then(newFromSource => this.slugify(newFromSource || ''))
.then(newSlug => this.updateCurrent(newSlug))
.catch(err => {
// eslint-disable-next-line no-console
console.error(
`An error occured while slugifying "${newFromSource}":\n${err.message}\n${err.stack}`
)
console.error(`An error occured while slugifying:\n${err.message}\n${err.stack}`)
})
.then(() => this._isMounted && this.setState({loading: false}))
}
getNewFromSource = () => {

hasSource = (): boolean => {
const {type, document} = this.props
const source = get(type, ['options', 'source'], [])
return typeof source === 'function' ? true : Boolean(get(document, source))
}

getNewFromSource = (): Promise<string> => {
const {getValuePath, type, document} = this.props
const parentPath = getValuePath().slice(0, -1)
const parent = get(document, parentPath)
const source = get(type, ['options', 'source'], [])
return typeof source === 'function'
? source(document, {parentPath, parent})
: get(document, source)
return Promise.resolve(
typeof source === 'function'
? source(document, {parentPath, parent})
: get(document, source)
)
}

render() {
const {value, type, level, markers, readOnly} = this.props
const {loading, inputText} = this.state
Expand All @@ -138,7 +147,6 @@ export default withValuePath(
}
const validation = markers.filter(marker => marker.type === 'validation')
const errors = validation.filter(marker => marker.level === 'error')
const hasSource = Boolean(this.getNewFromSource())
return (
<FormField {...formFieldProps}>
<div className={styles.wrapper}>
Expand All @@ -158,7 +166,7 @@ export default withValuePath(
<Button
className={styles.button}
inverted
disabled={readOnly || loading || !hasSource}
disabled={readOnly || loading || !this.hasSource()}
loading={loading}
onClick={this.handleGenerateSlug}
>
Expand Down

1 comment on commit eb049e1

@vercel
Copy link

@vercel vercel bot commented on eb049e1 Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.