Skip to content

Commit

Permalink
Merge pull request #663 from dadi/patch/reference-field-validation
Browse files Browse the repository at this point in the history
Fix issues with Reference field validation
  • Loading branch information
eduardoboucas authored Feb 11, 2019
2 parents 378ba52 + c15e4e7 commit d93b677
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ export default class ReferenceSelectHeader extends Component {
*/
instructionText: proptypes.string,

/**
* A callback to be used to obtain the base URL for the given page, as
* determined by the view.
*/
onBuildBaseUrl: proptypes.func,

/**
* The name of a reference field currently being edited.
*/
Expand All @@ -44,6 +38,11 @@ export default class ReferenceSelectHeader extends Component {
* The CTA text to display on the "return to document" button.
*/
returnCtaText: proptypes.string,

/**
* The URL of the "return to document" CTA button.
*/
returnCtaUrl: proptypes.string,
}

static defaultProps = {
Expand All @@ -56,10 +55,10 @@ export default class ReferenceSelectHeader extends Component {
children,
collectionParent,
instructionText,
onBuildBaseUrl,
parentDocumentId,
referencedField,
returnCtaText
returnCtaText,
returnCtaUrl
} = this.props
// Render nothing if we don't have the collection schema available.
if (!collectionParent) return null
Expand All @@ -70,15 +69,6 @@ export default class ReferenceSelectHeader extends Component {
if (!fieldSchema) return null

const displayName = fieldSchema.label || referencedField
const section = fieldSchema.publish &&
fieldSchema.publish.section &&
Format.slugify(fieldSchema.publish.section)
const returnUrl = onBuildBaseUrl({
createNew: false,//!Boolean(state.router.parameters.documentId),
referencedField: null,
search: null,
section
})

return (
<div class={styles.container}>
Expand All @@ -89,7 +79,7 @@ export default class ReferenceSelectHeader extends Component {

<Button
accent="destruct"
href={returnUrl}
href={returnCtaUrl}
size="small"
>{returnCtaText}</Button>

Expand Down
16 changes: 16 additions & 0 deletions frontend/containers/DocumentField/DocumentField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ class DocumentField extends Component {
this.validator = new Validator()
}

componentDidMount() {
const {state} = this.props
const {app, document} = state
const hasError = document.validationErrors
&& document.validationErrors[this.name]

// If we have just mounted the component and there's already an error
// registered for it in the store, we should validate the field again
// to ensure it isn't an error that carried on from a previous life of
// the component (e.g. when the component is mounted/unmounted) as part
// of the flow for selecting a reference document.
if (hasError) {
this.validate(this.value)
}
}

componentDidUpdate(oldProps, oldState) {
const {state} = this.props
const {document} = state
Expand Down
30 changes: 20 additions & 10 deletions frontend/views/ReferenceSelectView/ReferenceSelectView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ import Page from 'components/Page/Page'
import ReferenceSelectHeader from 'components/ReferenceSelectHeader/ReferenceSelectHeader'

class ReferenceSelectView extends Component {
getRedirectUrl() {
const {
onBuildBaseUrl,
referencedField,
state
} = this.props
const {currentCollection: collection} = state.api
const referenceFieldSchema = collection.fields[referencedField]

return onBuildBaseUrl.call(this, {
createNew: !Boolean(state.router.parameters.documentId),
search: null,
section: referenceFieldSchema &&
referenceFieldSchema.publish &&
Format.slugify(referenceFieldSchema.publish.section)
})
}

handleDocumentDelete(ids) {
const {actions, state} = this.props
const {
Expand Down Expand Up @@ -75,16 +93,7 @@ class ReferenceSelectView extends Component {
update
})

let referenceFieldSchema = collection.fields[referencedField]
let redirectUrl = onBuildBaseUrl.call(this, {
createNew: !Boolean(state.router.parameters.documentId),
search: null,
section: referenceFieldSchema &&
referenceFieldSchema.publish &&
Format.slugify(referenceFieldSchema.publish.section)
})

route(redirectUrl)
route(this.getRedirectUrl())
}

handleEmptyDocumentList() {
Expand Down Expand Up @@ -234,6 +243,7 @@ class ReferenceSelectView extends Component {
instructionText={instructionText}
referencedField={referencedField}
returnCtaText={returnCtaText}
returnCtaUrl={this.getRedirectUrl()}
/>

<DocumentListController
Expand Down

0 comments on commit d93b677

Please sign in to comment.