Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single document with tabs sample #820

Merged
merged 4 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions app/containers/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
registerProgressCallback
} from 'lib/api-bridge-client'
import AuthenticatedRoute from './AuthenticatedRoute'
import buildGroupedRoutes from './buildGroupedRoutes'
import buildRoutes from './buildRoutes'
import {connectRedux} from 'lib/redux'
import {debounce} from 'lib/util'
import DocumentEditView from 'views/DocumentEditView/DocumentEditView'
Expand All @@ -23,32 +23,6 @@ import ProfileEditView from 'views/ProfileEditView/ProfileEditView'
import React from 'react'
import SignInView from 'views/SignInView/SignInView'

const baseRoutes = [
{
path: '/:collection/new/:section?',
render: props => <DocumentEditView isNewDocument {...props} />
},
{
path: '/:collection/:documentId/:section?',
component: DocumentEditView
},
{
path: '/:collection',
component: DocumentListView
}
]

function buildRoutes({menu, isMultiProperty}) {
const routes = [...buildGroupedRoutes(menu, baseRoutes), ...baseRoutes]

return isMultiProperty
? routes.map(route => ({
...route,
path: '/:property' + route.path
}))
: routes
}

class App extends React.Component {
constructor(props) {
super(props)
Expand Down
47 changes: 0 additions & 47 deletions app/containers/App/buildGroupedRoutes.js

This file was deleted.

104 changes: 104 additions & 0 deletions app/containers/App/buildRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import DocumentEditView from 'views/DocumentEditView/DocumentEditView'
import DocumentListView from 'views/DocumentListView/DocumentListView'
import React from 'react'
import {slugify} from '../../../shared/lib/string'

const baseRoutes = [
{
path: '/:collection/new/:section?',
render: props => <DocumentEditView {...props} isNewDocument />
},
{
path: '/:collection/:documentId/:section?',
component: DocumentEditView
},
{
path: '/:collection',
component: DocumentListView
}
]

export default function buildRoutes({
collections = [],
menu: menuGroups = [],
isMultiProperty
}) {
const singleDocCollections = []
const multiDocCollections = []

for (const collection of collections) {
if (
collection.settings &&
collection.settings.publish &&
collection.settings.publish.isSingleDocument
) {
singleDocCollections.push(collection)
} else {
multiDocCollections.push(collection)
}
}

const validMenuGroups = menuGroups
.filter(
group =>
typeof group.title === 'string' &&
group.title.length &&
Array.isArray(group.collections) &&
group.collections.length
)
.map(group => ({...group, slug: slugify(group.title)}))

const singleDocRoutes = singleDocCollections.map(collection => {
const group = validMenuGroups.find(group =>
group.collections.includes(collection.slug)
)

return {
path: (group ? `/${group.slug}` : '') + `/${collection.slug}/:section?`,
render: props => {
const {params} = props.route

params.group = group ? group.slug : undefined
params.collection = collection.slug

return <DocumentListView {...props} isSingleDocument />
}
}
})

const multiDocRoutes = validMenuGroups
.map(group =>
group.collections.map(collectionSlug =>
baseRoutes.map(route => ({
path: `/${group.slug}${route.path.replace(
':collection',
collectionSlug
)}`,
render(props) {
const {params} = props.route

params.group = group.slug
params.collection = collectionSlug

if (route.render) {
return route.render(props)
}

const Component = route.component

return <Component {...props} />
}
}))
)
)
.flat(2)

const routes = [...singleDocRoutes, ...multiDocRoutes, ...baseRoutes]

return isMultiProperty
? routes.map(route => ({
...route,
path: '/:property' + route.path
}))
: routes
}
7 changes: 5 additions & 2 deletions app/lib/util/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ export function buildUrl({
group = this.props.route.params.group,
property = this.props.route.params.property,
search = this.props.route.search,
section = this.props.route.params.section
section
} = {}) {
if (!createNew && !documentId) section = null
if (createNew || documentId) {
section = section || this.props.route.params.section
}

if (createNew) documentId = 'new'

const urlNodes = [property, group, collection, documentId, section]
Expand Down
4 changes: 2 additions & 2 deletions app/views/DocumentEditView/DocumentEditView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class DocumentEditView extends React.Component {
// - `name`: name of the section
// - `slug`: slug of the section
groupFieldsIntoSections(fields) {
const {documentId, onBuildBaseUrl, route} = this.props
const {isNewDocument, onBuildBaseUrl, route} = this.props
const {section: activeSectionSlug} = route.params

const sections = {}
Expand Down Expand Up @@ -236,7 +236,7 @@ class DocumentEditView extends React.Component {
return {
fields: fieldsInPlacements,
href: onBuildBaseUrl.call(this, {
createNew: !documentId,
createNew: isNewDocument,
section: slug
}),
isActive,
Expand Down
8 changes: 1 addition & 7 deletions app/views/DocumentListView/DocumentListView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ class DocumentListView extends React.Component {

function mapState(state, ownProps) {
const {
isSingleDocument,
route: {params, search, searchString}
} = ownProps

Expand All @@ -556,12 +557,6 @@ function mapState(state, ownProps) {
return collection.slug === params.collection
})

const isSingleDocument =
collection &&
collection.settings &&
collection.settings.publish &&
collection.settings.publish.isSingleDocument

const {group} = params
const contentKey = isSingleDocument
? JSON.stringify({collection: collection && collection.slug})
Expand All @@ -579,7 +574,6 @@ function mapState(state, ownProps) {
return {
collection,
contentKey,
isSingleDocument,
selectionKey,
state
}
Expand Down
4 changes: 4 additions & 0 deletions dev-config/config.ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"field-test"
],
"title": "Field Testing"
},
{
"collections": ["test-single-document", "test-single-document-tabs"],
"title": "Single Document"
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions dev-config/config.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"no-filterable-fields"
],
"title": "Field Testing"
},
{
"collections": ["test-single-document", "test-single-document-tabs"],
"title": "Single Document"
}
]
}
Expand Down
Loading