88 * @license Pimcore Open Core License (POCL)
99 */
1010
11+ import { type OverrideResultType } from '@reduxjs/toolkit/query'
1112import { invalidatingTags , providingTags , tagNames } from '@Pimcore/app/api/pimcore/tags'
12- import { api as baseApi } from './class-definition-slice.gen'
13+ import { type ElementIcon } from '@Pimcore/components/icon/icon'
14+ import { denormalizeLayoutTreeIcons , normalizeLayoutTreeIcons } from '@Pimcore/utils/normalize-icon'
15+ import { api as baseApi , type ConfigLayoutDefinition , type CustomLayouts , type Layout } from './class-definition-slice.gen'
16+
17+ // Backend returns the root layout's icon as `string` for field-collection and
18+ // object-brick layouts. The codegen for ConfigLayoutDefinition reflects that
19+ // (`icon: string | null`), so we both convert it at runtime and override the
20+ // result type. For the other layout endpoints (classDefinitionGetLayoutById,
21+ // classCustomLayoutGet, classObjectBrickCustomLayoutGet) the codegen claims
22+ // `ElementIcon | null` already, but the same string-runtime mismatch can
23+ // occur, so we normalize defensively without any type override.
24+ export type NormalizedConfigLayoutDefinition =
25+ Omit < ConfigLayoutDefinition , 'icon' > & { icon : ElementIcon | null }
26+
27+ const normalizeLayout = ( raw : Layout ) : Layout => normalizeLayoutTreeIcons ( raw )
28+
29+ const normalizeCustomLayouts = ( raw : CustomLayouts ) : CustomLayouts => ( {
30+ ...raw ,
31+ layoutDefinition : raw . layoutDefinition !== null ? normalizeLayout ( raw . layoutDefinition ) : null
32+ } )
1333
1434/* eslint-disable max-lines */
15- const api = baseApi . enhanceEndpoints ( {
35+ const apiWithTags = baseApi . enhanceEndpoints ( {
1636 addTagTypes : [ tagNames . DATA_OBJECT , tagNames . DATA_OBJECT_DETAIL , tagNames . CLASS_DEFINITION , tagNames . CLASS_DEFINITION_DETAIL , tagNames . CLASS_DEFINITION_COLLECTION , tagNames . CUSTOM_LAYOUT , tagNames . CUSTOM_LAYOUT_DETAIL , tagNames . CUSTOM_LAYOUT_COLLECTION , tagNames . FIELD_COLLECTION , tagNames . FIELD_COLLECTION_DETAIL , tagNames . FIELD_COLLECTION_COLLECTION , tagNames . OBJECT_BRICK , tagNames . OBJECT_BRICK_DETAIL , tagNames . OBJECT_BRICK_COLLECTION , tagNames . OBJECT_BRICK_CUSTOM_LAYOUT , tagNames . OBJECT_BRICK_CUSTOM_LAYOUT_DETAIL , tagNames . OBJECT_BRICK_CUSTOM_LAYOUT_COLLECTION , tagNames . SELECT_OPTION_DETAIL , tagNames . SELECT_OPTION_COLLECTION ] ,
1737 endpoints : {
1838 classDefinitionCollection : {
@@ -22,7 +42,8 @@ const api = baseApi.enhanceEndpoints({
2242 providesTags : ( result , error , args ) => providingTags . CLASS_DEFINITION_DETAIL ( args . id )
2343 } ,
2444 classDefinitionGetLayoutById : {
25- providesTags : ( result , error , args ) => providingTags . CLASS_DEFINITION_DETAIL ( args . id )
45+ providesTags : ( result , error , args ) => providingTags . CLASS_DEFINITION_DETAIL ( args . id ) ,
46+ transformResponse : normalizeLayout
2647 } ,
2748 classCustomLayoutCollection : {
2849 providesTags : ( ) => providingTags . CUSTOM_LAYOUT_COLLECTION ( )
@@ -33,7 +54,7 @@ const api = baseApi.enhanceEndpoints({
3354 try {
3455 const { data } = await queryFulfilled
3556 dispatch (
36- api . util . updateQueryData ( 'classDefinitionGetById' , { id : args . id } , ( draft ) => {
57+ apiWithTags . util . updateQueryData ( 'classDefinitionGetById' , { id : args . id } , ( draft ) => {
3758 Object . assign ( draft , data )
3859 } )
3960 )
@@ -61,15 +82,17 @@ const api = baseApi.enhanceEndpoints({
6182 ]
6283 } ,
6384 classCustomLayoutGet : {
64- providesTags : ( result , error , args ) => providingTags . CUSTOM_LAYOUT_DETAIL ( args . customLayoutId )
85+ providesTags : ( result , error , args ) => providingTags . CUSTOM_LAYOUT_DETAIL ( args . customLayoutId ) ,
86+ transformResponse : normalizeCustomLayouts
6587 } ,
6688 classCustomLayoutUpdate : {
6789 invalidatesTags : ( ) => [ ] ,
90+ transformResponse : normalizeCustomLayouts ,
6891 async onQueryStarted ( args , { dispatch, queryFulfilled } ) {
6992 try {
7093 const { data } = await queryFulfilled
7194 dispatch (
72- api . util . updateQueryData ( 'classCustomLayoutGet' , { customLayoutId : args . customLayoutId } , ( draft ) => {
95+ apiWithTags . util . updateQueryData ( 'classCustomLayoutGet' , { customLayoutId : args . customLayoutId } , ( draft ) => {
7396 Object . assign ( draft , data )
7497 } )
7598 )
@@ -120,7 +143,7 @@ const api = baseApi.enhanceEndpoints({
120143 try {
121144 const { data } = await queryFulfilled
122145 dispatch (
123- api . util . updateQueryData ( 'classFieldCollectionGetByKey' , { key : args . key } , ( draft ) => {
146+ apiWithTags . util . updateQueryData ( 'classFieldCollectionGetByKey' , { key : args . key } , ( draft ) => {
124147 Object . assign ( draft , data )
125148 } )
126149 )
@@ -171,7 +194,7 @@ const api = baseApi.enhanceEndpoints({
171194 try {
172195 const { data } = await queryFulfilled
173196 dispatch (
174- api . util . updateQueryData ( 'classObjectBrickGetByKey' , { key : args . key } , ( draft ) => {
197+ apiWithTags . util . updateQueryData ( 'classObjectBrickGetByKey' , { key : args . key } , ( draft ) => {
175198 Object . assign ( draft , data )
176199 } )
177200 )
@@ -196,15 +219,17 @@ const api = baseApi.enhanceEndpoints({
196219 ]
197220 } ,
198221 classObjectBrickCustomLayoutGet : {
199- providesTags : ( result , error , args ) => providingTags . OBJECT_BRICK_CUSTOM_LAYOUT_DETAIL ( args . key , args . customLayoutId )
222+ providesTags : ( result , error , args ) => providingTags . OBJECT_BRICK_CUSTOM_LAYOUT_DETAIL ( args . key , args . customLayoutId ) ,
223+ transformResponse : normalizeCustomLayouts
200224 } ,
201225 classObjectBrickCustomLayoutUpdate : {
202226 invalidatesTags : ( ) => [ ] ,
227+ transformResponse : normalizeCustomLayouts ,
203228 async onQueryStarted ( args , { dispatch, queryFulfilled } ) {
204229 try {
205230 const { data } = await queryFulfilled
206231 dispatch (
207- api . util . updateQueryData ( 'classObjectBrickCustomLayoutGet' , { key : args . key , customLayoutId : args . customLayoutId } , ( draft ) => {
232+ apiWithTags . util . updateQueryData ( 'classObjectBrickCustomLayoutGet' , { key : args . key , customLayoutId : args . customLayoutId } , ( draft ) => {
208233 Object . assign ( draft , data )
209234 } )
210235 )
@@ -237,7 +262,7 @@ const api = baseApi.enhanceEndpoints({
237262 try {
238263 const { data } = await queryFulfilled
239264 dispatch (
240- api . util . updateQueryData ( 'classSelectOptionGet' , { id : args . id } , ( draft ) => {
265+ apiWithTags . util . updateQueryData ( 'classSelectOptionGet' , { id : args . id } , ( draft ) => {
241266 Object . assign ( draft , data )
242267 } )
243268 )
@@ -278,6 +303,69 @@ const api = baseApi.enhanceEndpoints({
278303 }
279304} )
280305
306+ // Retype the two layout-by-key queries so callers see the normalized icon,
307+ // and wire transformResponse to do the runtime conversion. Both happen in the
308+ // same enhanceEndpoints call so transformResponse's return type matches the
309+ // overridden result type.
310+ const apiWithReadTransforms = apiWithTags . enhanceEndpoints < never , {
311+ classFieldCollectionGetLayoutByKey : OverrideResultType <
312+ typeof apiWithTags . endpoints . classFieldCollectionGetLayoutByKey . Types . QueryDefinition ,
313+ NormalizedConfigLayoutDefinition
314+ >
315+ classObjectBrickGetLayoutByKey : OverrideResultType <
316+ typeof apiWithTags . endpoints . classObjectBrickGetLayoutByKey . Types . QueryDefinition ,
317+ NormalizedConfigLayoutDefinition
318+ >
319+ } > ( {
320+ endpoints : {
321+ classFieldCollectionGetLayoutByKey : {
322+ transformResponse : ( raw : ConfigLayoutDefinition ) : NormalizedConfigLayoutDefinition =>
323+ normalizeLayoutTreeIcons ( raw ) as unknown as NormalizedConfigLayoutDefinition
324+ } ,
325+ classObjectBrickGetLayoutByKey : {
326+ transformResponse : ( raw : ConfigLayoutDefinition ) : NormalizedConfigLayoutDefinition =>
327+ normalizeLayoutTreeIcons ( raw ) as unknown as NormalizedConfigLayoutDefinition
328+ }
329+ }
330+ } )
331+
332+ const denormalizeConfigurationOnQuery = ( endpoint : { query ?: ( arg : any ) => any } ) : void => {
333+ const originalQuery = endpoint . query
334+ if ( originalQuery === undefined ) {
335+ return
336+ }
337+
338+ endpoint . query = ( queryArg : any ) => {
339+ const baseResult = originalQuery ( queryArg )
340+ if ( baseResult === null || typeof baseResult !== 'object' || ! ( 'body' in baseResult ) ) {
341+ return baseResult
342+ }
343+
344+ const body = baseResult . body as { configuration ?: unknown }
345+ if ( body . configuration === undefined ) {
346+ return baseResult
347+ }
348+
349+ return {
350+ ...baseResult ,
351+ body : {
352+ ...body ,
353+ configuration : denormalizeLayoutTreeIcons ( body . configuration )
354+ }
355+ }
356+ }
357+ }
358+
359+ const api = apiWithReadTransforms . enhanceEndpoints ( {
360+ endpoints : {
361+ classDefinitionUpdate : denormalizeConfigurationOnQuery ,
362+ classCustomLayoutUpdate : denormalizeConfigurationOnQuery ,
363+ classFieldCollectionUpdate : denormalizeConfigurationOnQuery ,
364+ classObjectBrickUpdate : denormalizeConfigurationOnQuery ,
365+ classObjectBrickCustomLayoutUpdate : denormalizeConfigurationOnQuery
366+ }
367+ } )
368+
281369export type * from './class-definition-slice.gen'
282370
283371export const {
0 commit comments