| 1 |
import changeHeroSection from '@agent/workflows/theme/tools/change-hero-section'; |
| 2 |
import updateSiteVibes from '@agent/workflows/theme/tools/update-site-vibes'; |
| 3 |
import { deepMerge } from '@shared/lib/utils'; |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
|
| 6 |
const { globalStylesPostID } = window.extSharedData; |
| 7 |
|
| 8 |
// Apply the selected variation as authoritative, preserving only block style variations (vibes) |
| 9 |
const updateVariation = async ({ variation }) => { |
| 10 |
const currentVariations = await apiFetch({ |
| 11 |
path: '/extendify/v1/agent/block-style-variations', |
| 12 |
}); |
| 13 |
|
| 14 |
const preservedBlockVibes = Object.keys(currentVariations).reduce( |
| 15 |
(blocks, blockName) => { |
| 16 |
blocks[blockName] = { variations: currentVariations[blockName] }; |
| 17 |
return blocks; |
| 18 |
}, |
| 19 |
{}, |
| 20 |
); |
| 21 |
|
| 22 |
const final = deepMerge(variation, { |
| 23 |
styles: { blocks: preservedBlockVibes }, |
| 24 |
}); |
| 25 |
|
| 26 |
return apiFetch({ |
| 27 |
method: 'POST', |
| 28 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 29 |
data: { id: globalStylesPostID, ...final }, |
| 30 |
}); |
| 31 |
}; |
| 32 |
|
| 33 |
export default async ({ |
| 34 |
updatedPageBlocks, |
| 35 |
postId, |
| 36 |
vibeSlug, |
| 37 |
colorAndFontsVariation, |
| 38 |
}) => { |
| 39 |
if (!updatedPageBlocks || !vibeSlug || !colorAndFontsVariation) return; |
| 40 |
|
| 41 |
await Promise.all([ |
| 42 |
changeHeroSection({ updatedPageBlocks, postId }), |
| 43 |
(async () => { |
| 44 |
await updateSiteVibes({ selectedVibe: vibeSlug }); |
| 45 |
await updateVariation({ variation: colorAndFontsVariation }); |
| 46 |
})(), |
| 47 |
]); |
| 48 |
}; |
| 49 |
|