Skip to content

Commit

Permalink
fix: return url in select view
Browse files Browse the repository at this point in the history
  • Loading branch information
ohmoses committed Aug 8, 2019
1 parent 43370cc commit b091b50
Showing 1 changed file with 50 additions and 54 deletions.
104 changes: 50 additions & 54 deletions app/views/ReferenceSelectView/ReferenceSelectView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,57 @@ class ReferenceSelectView extends React.Component {
super(props)

this.cancelSelection = this.cancelSelection.bind(this)
this.saveSelection = this.saveSelection.bind(this)
this.hasPropagatedInitialSelection = {}
}

cancelSelection() {
const {
actions,
documentId,
history,
onBuildBaseUrl,
selectionKey
} = this.props
const {actions, selectionKey} = this.props

actions.setDocumentSelection({
key: selectionKey,
selection: []
})

this.goBackToDocument()
}

goBackToDocument() {
const {
history,
isNewDocument,
onBuildBaseUrl,
referenceFieldSchema
} = this.props

// It's fair to assume that the user got here because they were editing
// the value of the reference field in the edit view. As such, we must
// take them back to whatever section the reference field belongs to, if
// any.
const fieldSection =
referenceFieldSchema.publish &&
referenceFieldSchema.publish.section &&
slugify(referenceFieldSchema.publish.section)

const redirectUrl = onBuildBaseUrl.call(this, {
createNew: !documentId,
referenceFieldSelect: null
createNew: isNewDocument,
referenceFieldSelect: null,
search: {
filter: null
},
section: fieldSection || null
})

history.push(redirectUrl)
}

handleDocumentSelect({referenceField: schema, selection}) {
saveSelection() {
const {
actions,
history,
isNewDocument,
onBuildBaseUrl,
parentContentKey,
route
route,
selection,
selectionKey
} = this.props
const {referenceField} = route.params

Expand All @@ -75,28 +93,11 @@ class ReferenceSelectView extends React.Component {
})

actions.setDocumentSelection({
key: this.getSelectionKey(),
key: selectionKey,
selection: []
})

// It's fair to assume that the user got here because they were editing
// the value of the reference field in the edit view. As such, we must
// take them back to whatever section the reference field belongs to, if
// any.
const fieldSection =
schema.publish &&
schema.publish.section &&
slugify(schema.publish.section)
const redirectUrl = onBuildBaseUrl.call(this, {
createNew: isNewDocument,
referenceFieldSelect: null,
search: {
filter: null
},
section: fieldSection || null
})

history.push(redirectUrl)
this.goBackToDocument()
}

handleEmptyDocumentList({selection}) {
Expand Down Expand Up @@ -204,8 +205,9 @@ class ReferenceSelectView extends React.Component {
isSingleDocument,
onBuildBaseUrl,
parentContentKey,
referenceFieldSchema,
route,
selectionKey,
selection,
state
} = this.props
const {documentId, page, referenceField: referenceFieldName} = route.params
Expand All @@ -232,15 +234,12 @@ class ReferenceSelectView extends React.Component {
return null
}

// Getting the schema of the reference field.
const referenceField = collection.fields[referenceFieldName]

if (!referenceField) {
if (!referenceFieldSchema) {
return null
}

// Getting the component for the given reference field.
const fieldType = getFieldType(referenceField)
const fieldType = getFieldType(referenceFieldSchema)
const fieldComponent = fieldComponents[`Field${fieldType}`]

// If the field component does not declare a `onReferenceSelect` method, it
Expand All @@ -255,8 +254,9 @@ class ReferenceSelectView extends React.Component {
// If the `referenceField` parameter doesn't match a field of type
// `Reference`, we render nothing.
if (
!referenceField ||
(referenceField.type !== 'Media' && referenceField.type !== 'Reference')
!referenceFieldSchema ||
(referenceFieldSchema.type !== 'Media' &&
referenceFieldSchema.type !== 'Reference')
) {
return null
}
Expand Down Expand Up @@ -285,11 +285,7 @@ class ReferenceSelectView extends React.Component {
}

// Getting documents from store.
const data = state.documents[contentKey] || {}
const {metadata} = data

// Getting the IDs of the selected documents.
const selection = state.selection[selectionKey] || []
const {metadata} = state.documents[contentKey] || {}

// Are we showing only selected documents?
const isFilteringSelection =
Expand Down Expand Up @@ -320,7 +316,7 @@ class ReferenceSelectView extends React.Component {
<Page>
<ReferenceSelectHeader
onCancel={this.cancelSelection}
referenceField={referenceField}
referenceField={referenceFieldSchema}
/>

<DocumentListController
Expand Down Expand Up @@ -371,13 +367,7 @@ class ReferenceSelectView extends React.Component {
selectedDocuments={selection}
showSelectedDocumentsUrl={showSelectedDocumentsUrl}
>
<Button
accent="save"
onClick={this.handleDocumentSelect.bind(this, {
referenceField,
selection
})}
>
<Button accent="save" onClick={this.saveSelection}>
Save selection
</Button>
</DocumentListToolbar>
Expand Down Expand Up @@ -479,6 +469,8 @@ function mapState(state, ownProps) {
return collection.slug === params.collection
})

const referenceFieldSchema = collection.fields[params.referenceField]

const isNewDocument = /\/new(\/|$)/.test(path) && !params.documentId
const isSingleDocument =
collection.settings &&
Expand All @@ -504,12 +496,16 @@ function mapState(state, ownProps) {
referenceField
})

const selection = state.selection[selectionKey] || []

return {
collection,
contentKey,
isNewDocument,
isSingleDocument,
parentContentKey,
referenceFieldSchema,
selection,
selectionKey,
state
}
Expand Down

0 comments on commit b091b50

Please sign in to comment.