Skip to content

Commit

Permalink
fix: support creating version with initial value (#7878)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored Nov 26, 2024
1 parent 41a63c8 commit 027aabf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('useVersionOperations', () => {
expect(useReleaseOperationsMockReturn.createVersion).toHaveBeenCalledWith(
'releaseId',
'documentId',
undefined,
)
expect(usePerspectiveMockReturn.setPerspectiveFromReleaseId).toHaveBeenCalledWith('releaseId')
})
Expand Down
14 changes: 11 additions & 3 deletions packages/sanity/src/core/releases/hooks/useVersionOperations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {getCreateVersionOrigin} from '../util/util'
import {usePerspective} from './usePerspective'

export interface VersionOperationsValue {
createVersion: (releaseId: string, documentId: string) => Promise<void>
createVersion: (
releaseId: string,
documentId: string,
initialValue?: Record<string, unknown>,
) => Promise<void>
discardVersion: (releaseId: string, documentId: string) => Promise<void>
}

Expand All @@ -21,10 +25,14 @@ export function useVersionOperations(): VersionOperationsValue {
const toast = useToast()
const {t} = useTranslation()

const handleCreateVersion = async (releaseId: string, documentId: string) => {
const handleCreateVersion = async (
releaseId: string,
documentId: string,
initialValue?: Record<string, unknown>,
) => {
const origin = getCreateVersionOrigin(documentId)
try {
await createVersion(releaseId, documentId)
await createVersion(releaseId, documentId, initialValue)
setPerspectiveFromReleaseId(releaseId)
telemetry.log(AddedVersion, {
documentOrigin: origin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export interface ReleaseOperationsStore {
unarchive: (releaseId: string) => Promise<void>
updateRelease: (release: EditableReleaseDocument) => Promise<void>
createRelease: (release: EditableReleaseDocument) => Promise<void>
createVersion: (releaseId: string, documentId: string) => Promise<void>
createVersion: (
releaseId: string,
documentId: string,
initialvalue?: Record<string, unknown>,
) => Promise<void>
discardVersion: (releaseId: string, documentId: string) => Promise<void>
}

Expand Down Expand Up @@ -96,27 +100,31 @@ export function createReleaseOperationsStore(options: {
},
])

const handleCreateVersion = async (releaseId: string, documentId: string) => {
const handleCreateVersion = async (
releaseId: string,
documentId: string,
initialValue?: Record<string, unknown>,
) => {
// the documentId will show you where the document is coming from and which
// document should it copy from

// fetch original document
const document = await client.getDocument(documentId)

if (!document) {
throw new Error(`Document with id ${documentId} not found`)
if (!document && !initialValue) {
throw new Error(`Document with id ${documentId} not found and no initial value provided`)
}

const versionDocument = {
...document,
...(document || initialValue || {}),
_id: getVersionId(documentId, releaseId),
} as IdentifiedSanityDocumentStub

await (IS_CREATE_VERSION_ACTION_SUPPORTED
? requestAction(client, [
{
actionType: 'sanity.action.document.createVersion',
releaseId: getBundleIdFromReleaseDocumentId(releaseId),
releaseId,
attributes: versionDocument,
},
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const DocumentPanel = function DocumentPanel(props: DocumentPanelProps) {
<AddToReleaseBanner
documentId={value._id}
currentRelease={currentGlobalBundle as ReleaseDocument}
value={displayed || undefined}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import {Banner} from './Banner'
export function AddToReleaseBanner({
documentId,
currentRelease,
value,
}: {
documentId: string
currentRelease: ReleaseDocument
value?: Record<string, unknown>
}): JSX.Element {
const tone = getReleaseTone(currentRelease ?? LATEST)
const {t} = useTranslation(structureLocaleNamespace)
Expand All @@ -37,9 +39,9 @@ export function AddToReleaseBanner({

const handleAddToRelease = useCallback(async () => {
if (currentRelease._id) {
await createVersion(getBundleIdFromReleaseDocumentId(currentRelease._id), documentId)
await createVersion(getBundleIdFromReleaseDocumentId(currentRelease._id), documentId, value)
}
}, [createVersion, currentRelease._id, documentId])
}, [createVersion, currentRelease._id, documentId, value])

return (
<Banner
Expand Down

0 comments on commit 027aabf

Please sign in to comment.