Skip to content

Commit 6ee48dc

Browse files
committed
Fix empty component-group location when adding to an unpublished draft
useCwaComponent's publishedIri returned undefined for a never-published draft (findPublishedComponentIri returns undefined by design — Publish.vue and resource-stack-manager rely on that), so CwaComponentGroup received an empty :location and warned 'The location provided `` is not a current resource' when adding a component inside a static page nested in a data page. Default publishedIri to the component's own iri when there is no published version — a draft is still a valid group location. The getter is left untouched (its undefined return is still needed by other callers).
1 parent dae085f commit 6ee48dc

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/runtime/composables/cwa-component.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,20 @@ describe('useCwaComponent', () => {
100100
expect('files' in result).toBe(false)
101101
})
102102
})
103+
104+
describe('publishedIri', () => {
105+
test('returns the mapped published iri when a draft has a published version', () => {
106+
;(mockCwa.resources as any).findPublishedComponentIri = vi.fn(() => ({ value: '/component/published' }))
107+
const result = useCwaComponent({ iri: '/component/draft' })
108+
expect((result as any).publishedIri.value).toBe('/component/published')
109+
})
110+
111+
test('falls back to the component own iri when there is no published version (unpublished draft)', () => {
112+
// findPublishedComponentIri returns undefined for a never-published draft by design; a draft is
113+
// still a valid component-group location, so publishedIri must default to the component own iri.
114+
;(mockCwa.resources as any).findPublishedComponentIri = vi.fn(() => ({ value: undefined }))
115+
const result = useCwaComponent({ iri: '/component/unpublished-draft' })
116+
expect((result as any).publishedIri.value).toBe('/component/unpublished-draft')
117+
})
118+
})
103119
})

src/runtime/composables/cwa-component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ export const useCwaComponent = <P extends CwaResourcePlugin<any>[]>(
2828
const iri = toRef(props, 'iri')
2929
const { getResource, $cwa, exposeMeta, getCurrentStyleName, uiClassNames } = useCwaResource(iri, ops)
3030
const resource = getResource()
31-
// Published IRI of the current component (resolves the live/published equivalent when this is a
32-
// draft). Intended as the reference to use when adding a component group to this component.
33-
const publishedIri = computed(() => $cwa.resources.findPublishedComponentIri(iri.value).value)
31+
// The canonical component IRI to use as a component-group location: the live/published equivalent
32+
// when this is a draft, falling back to this component's own IRI when it has never been published
33+
// (an unpublished draft is still a valid group location). `findPublishedComponentIri` returns
34+
// undefined in that case by design (other callers rely on it), so we default to `iri` here.
35+
const publishedIri = computed(() => $cwa.resources.findPublishedComponentIri(iri.value).value ?? iri.value)
3436
const ctx: CwaResourcePluginContext = { iri, resource, $cwa }
3537
const pluginResults = (plugins ?? []).map(plugin => plugin(ctx))
3638

src/runtime/storage/stores/resources/getters-additional.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ describe('getters -> findPublishedComponentIri / findDraftComponentIri', () => {
204204
expect(getterFns.findPublishedComponentIri.value('/non-existent')).toBeUndefined()
205205
})
206206

207+
test('findPublishedComponentIri returns undefined for a draft that has never been published (callers rely on this)', () => {
208+
state.current.byId['/component/unpublished-draft'] = {
209+
apiState: { status: CwaResourceApiStatuses.SUCCESS },
210+
data: { '@id': '/component/unpublished-draft', '@type': 'Component', '_metadata': { publishable: { published: false } } },
211+
}
212+
expect(getterFns.findPublishedComponentIri.value('/component/unpublished-draft')).toBeUndefined()
213+
})
214+
207215
test('findDraftComponentIri returns iri when resource is draft', () => {
208216
state.current.byId['/component/draft'] = {
209217
apiState: { status: CwaResourceApiStatuses.SUCCESS },

src/runtime/storage/stores/resources/getters.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ export default function (resourcesState: CwaResourcesStateInterface): CwaResourc
250250
if (isPublished) {
251251
return iri
252252
}
253+
// a draft: its mapped published IRI, or undefined when it has never been published. Callers
254+
// that need "no published version exists" (e.g. the Publish toggle, resource-stack-manager)
255+
// rely on this undefined — do NOT fall back to the draft iri here.
253256
return draftToPublishedIris.value[iri]
254257
}
255258
}),

0 commit comments

Comments
 (0)