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 support for single-document collections #762

Merged
merged 6 commits into from
Aug 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
8 changes: 7 additions & 1 deletion app/components/FieldReference/FieldReferenceEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default class FieldReferenceEdit extends React.Component {

render() {
const {
collection,
config,
displayName,
documentId,
Expand Down Expand Up @@ -155,8 +156,13 @@ export default class FieldReferenceEdit extends React.Component {
})
const firstStringField = this.findFirstStringField(displayableFields)
const displayField = value && firstStringField ? firstStringField.key : null

const isSingleDocument =
collection.settings &&
collection.settings.publish &&
collection.settings.publish.isSingleDocument
const editLink = onBuildBaseUrl({
createNew: !documentId,
createNew: !documentId && !isSingleDocument,
referenceFieldSelect: name
})
const values = value && !(value instanceof Array) ? [value] : value
Expand Down
179 changes: 76 additions & 103 deletions app/containers/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,57 @@ class Route extends React.Component {
}
}

const collectionRoutes = [
{
path: `/:collection${REGEX_SLUG}/new/select/:referenceField/:page${REGEX_NUMBER}?`,
component: ReferenceSelectView
},
{
path: `/:collection${REGEX_SLUG}/select/:referenceField/:page${REGEX_NUMBER}?`,
component: ReferenceSelectView
},
{
path: `/:collection${REGEX_SLUG}/new/:section?`,
component: DocumentEditView
},
{
path: `/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/select/:referenceField/:page${REGEX_NUMBER}?`,
component: ReferenceSelectView
},
{
path: `/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/new/select/:referenceField/:page${REGEX_NUMBER}?`,
component: ReferenceSelectView
},
{
path: `/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/select/:referenceField/:page${REGEX_NUMBER}?`,
component: ReferenceSelectView
},
{
path: `/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/new/:section?`,
component: DocumentEditView
},
{
path: `/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/select/:referenceField/:page${REGEX_NUMBER}?`,
component: ReferenceSelectView
},
{
path: `/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/:section?`,
component: DocumentEditView
},
{
path: `/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/:section?`,
component: DocumentEditView
},
{
path: `/:collection${REGEX_SLUG}/:page${REGEX_NUMBER}?`,
component: DocumentListView
},
{
path: `/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/:page${REGEX_NUMBER}?`,
component: DocumentListView
}
]

class App extends React.Component {
componentWillMount() {
const {actions} = this.props
Expand Down Expand Up @@ -233,147 +284,69 @@ class App extends React.Component {

<Switch>
<Route
isSignedIn={isSignedIn}
exact
mustBeSignedIn
path="/"
component={HomeView}
config={state.app.config}
/>

<Route
isSignedIn={isSignedIn}
exact
isSignedIn={isSignedIn}
mustBeSignedIn
path="/profile/:section?"
component={ProfileEditView}
config={state.app.config}
path="/"
/>

<Route
isSignedIn={isSignedIn}
exact
mustBeSignedIn
path="/profile/select/:referenceField/:page[^\d+$]?"
component={ProfileEditView}
config={state.app.config}
/>

<Route
isSignedIn={isSignedIn}
exact
path="/sign-in/:token?"
component={SignInView}
config={state.app.config}
/>

<Route
isSignedIn={isSignedIn}
exact
mustBeSignedIn
path="/sign-out"
render={() => {
actions.signOut()

return <Redirect to="/" />
}}
config={state.app.config}
path="/profile/:section?"
/>

<Route
isSignedIn={isSignedIn}
exact
component={DocumentEditView}
component={ProfileEditView}
config={state.app.config}
mustBeSignedIn
path={`/:collection${REGEX_SLUG}/new/:section?`}
/>

<Route
isSignedIn={isSignedIn}
exact
component={DocumentEditView}
config={state.app.config}
mustBeSignedIn
path={`/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/new/:section?`}
/>

<Route
isSignedIn={isSignedIn}
exact
component={DocumentEditView}
config={state.app.config}
mustBeSignedIn
path={`/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/:section?`}
path="/profile/select/:referenceField/:page[^\d+$]?"
/>

<Route
isSignedIn={isSignedIn}
exact
component={DocumentEditView}
component={SignInView}
config={state.app.config}
mustBeSignedIn
path={`/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/:section?`}
/>

<Route
isSignedIn={isSignedIn}
exact
component={DocumentListView}
config={state.app.config}
mustBeSignedIn
path={`/:collection${REGEX_SLUG}/:page${REGEX_NUMBER}?`}
/>

<Route
isSignedIn={isSignedIn}
exact
component={ReferenceSelectView}
config={state.app.config}
mustBeSignedIn
path={`/:collection${REGEX_SLUG}/new/select/:referenceField/:page${REGEX_NUMBER}?`}
path="/sign-in/:token?"
/>

<Route
isSignedIn={isSignedIn}
exact
component={ReferenceSelectView}
config={state.app.config}
mustBeSignedIn
path={`/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/select/:referenceField/:page${REGEX_NUMBER}?`}
/>

<Route
isSignedIn={isSignedIn}
exact
component={DocumentListView}
config={state.app.config}
mustBeSignedIn
path={`/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/:page${REGEX_NUMBER}?`}
/>

<Route
isSignedIn={isSignedIn}
exact
component={ReferenceSelectView}
config={state.app.config}
mustBeSignedIn
path={`/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/new/select/:referenceField/:page${REGEX_NUMBER}?`}
/>
path="/sign-out"
render={() => {
actions.signOut()

<Route
isSignedIn={isSignedIn}
exact
component={ReferenceSelectView}
config={state.app.config}
mustBeSignedIn
path={`/:group${REGEX_SLUG}/:collection${REGEX_SLUG}/:documentId${REGEX_DOCUMENT_ID}/select/:referenceField/:page${REGEX_NUMBER}?`}
return <Redirect to="/" />
}}
/>

{collectionRoutes.map(({path, component}) => (
<Route
component={component}
config={state.app.config}
exact
isSignedIn={isSignedIn}
key={path}
mustBeSignedIn
path={path}
/>
))}

<Route
isSignedIn={isSignedIn}
component={ErrorView}
config={state.app.config}
isSignedIn={isSignedIn}
/>
</Switch>
</Router>
Expand Down
47 changes: 34 additions & 13 deletions app/containers/DocumentEditToolbar/DocumentEditToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as appActions from 'actions/appActions'
import * as Constants from 'lib/constants'
import * as documentActions from 'actions/documentActions'
import * as userActions from 'actions/userActions'
import Button from 'components/Button/Button'
import ButtonWithOptions from 'components/ButtonWithOptions/ButtonWithOptions'
import ButtonWithPrompt from 'components/ButtonWithPrompt/ButtonWithPrompt'
import {connectRedux} from 'lib/redux'
Expand Down Expand Up @@ -145,7 +146,14 @@ class DocumentEditToolbar extends React.Component {
}

render() {
const {contentKey, documentId, multiLanguage, router, state} = this.props
const {
contentKey,
documentId,
isSingleDocument,
multiLanguage,
router,
state
} = this.props
const {api} = state.app.config
const {document: documentStore} = state
const document = documentStore[contentKey] || {}
Expand Down Expand Up @@ -219,7 +227,7 @@ class DocumentEditToolbar extends React.Component {
</div>

<div className={styles.buttons}>
{remote && (
{remote && !isSingleDocument && (
<div className={styles.button}>
<ButtonWithPrompt
accent="destruct"
Expand All @@ -236,17 +244,30 @@ class DocumentEditToolbar extends React.Component {
)}

<div className={styles.button}>
<ButtonWithOptions
accent="save"
disabled={Boolean(
hasConnectionIssues || hasValidationErrors || isSaving
)}
isLoading={isSaving}
onClick={saveOptions.primary.action}
options={saveOptions.secondary}
>
{saveOptions.primary.label}
</ButtonWithOptions>
{isSingleDocument ? (
<Button
accent="save"
disabled={Boolean(
hasConnectionIssues || hasValidationErrors || isSaving
)}
isLoading={isSaving}
onClick={saveOptions.primary.action}
>
Save document
</Button>
) : (
<ButtonWithOptions
accent="save"
disabled={Boolean(
hasConnectionIssues || hasValidationErrors || isSaving
)}
isLoading={isSaving}
onClick={saveOptions.primary.action}
options={saveOptions.secondary}
>
{saveOptions.primary.label}
</ButtonWithOptions>
)}
</div>
</div>
</Toolbar>
Expand Down
10 changes: 8 additions & 2 deletions app/lib/redux.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ export function enableBatching(reducer) {
}
}

export function connectRedux(...actions) {
export function connectRedux(mapState, ...actions) {
const receivedMapper = typeof mapState === 'function'

if (!receivedMapper) {
actions.unshift(mapState)
}

const mergedActions = actions.reduce((actions, action) => {
return {...actions, ...action}
}, {})

return connect(
state => ({state}),
receivedMapper ? mapState : state => ({state}),
dispatch => ({
actions: bindActionCreators(mergedActions, dispatch)
})
Expand Down
Loading