Skip to content

Commit

Permalink
fix(core): duplicate should work with version documents
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Jan 18, 2025
1 parent b47e306 commit e13884f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface EditStateFor {
/**
* When editing a version, the name of the release the document belongs to.
*/
release?: string
release: string | undefined
}
const LOCKED: TransactionSyncLockState = {enabled: true}
const NOT_LOCKED: TransactionSyncLockState = {enabled: false}
Expand Down Expand Up @@ -98,7 +98,7 @@ export const editState = memoize(
liveEditSchemaType,
ready: !fromCache,
transactionSyncLock: fromCache ? null : transactionSyncLock,
bundleId: idPair.versionId ? getVersionFromId(idPair.versionId) : undefined,
release: idPair.versionId ? getVersionFromId(idPair.versionId) : undefined,
}),
),
startWith({
Expand All @@ -111,7 +111,7 @@ export const editState = memoize(
liveEditSchemaType,
ready: false,
transactionSyncLock: null,
bundleId: idPair.versionId ? getVersionFromId(idPair.versionId) : undefined,
release: idPair.versionId ? getVersionFromId(idPair.versionId) : undefined,
}),
publishReplay(1),
refCount(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import {omit} from 'lodash'

import {getDraftId} from '../../../../../util'
import {getDraftId, getVersionFromId, getVersionId} from '../../../../../util'
import {isLiveEditEnabled} from '../utils/isLiveEditEnabled'
import {type OperationImpl} from './types'

const omitProps = ['_createdAt', '_updatedAt']

export const duplicate: OperationImpl<[baseDocumentId: string], 'NOTHING_TO_DUPLICATE'> = {
disabled: ({snapshots}) => {
return snapshots.published || snapshots.draft ? false : 'NOTHING_TO_DUPLICATE'
if (snapshots.version) return false
return snapshots.published || snapshots.draft || snapshots.version
? false
: 'NOTHING_TO_DUPLICATE'
},
execute: ({schema, client, snapshots, typeName}, dupeId) => {
const source = snapshots.version || snapshots.draft || snapshots.published
Expand All @@ -17,10 +20,19 @@ export const duplicate: OperationImpl<[baseDocumentId: string], 'NOTHING_TO_DUPL
throw new Error('cannot execute on empty document')
}

// When duplicating a version document we need to create it with a version id
const versionId = snapshots.version?._id ? getVersionFromId(snapshots.version._id) : null
// eslint-disable-next-line no-nested-ternary
const _id = versionId
? getVersionId(dupeId, versionId)
: isLiveEditEnabled(schema, typeName)
? dupeId
: getDraftId(dupeId)

return client.observable.create(
{
...omit(source, omitProps),
_id: isLiveEditEnabled(schema, typeName) ? dupeId : getDraftId(dupeId),
_id,
_type: source._type,
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const DISABLED_REASON_KEY = {
}

/** @internal */
export const DuplicateAction: DocumentActionComponent = ({id, type, onComplete, release}) => {
export const DuplicateAction: DocumentActionComponent = (props) => {
const {id, type, onComplete, release} = props
const documentStore = useDocumentStore()
const {duplicate} = useDocumentOperation(id, type, release)
const {navigateIntent} = useRouter()
Expand Down

0 comments on commit e13884f

Please sign in to comment.