| 1 |
import { deepMerge } from '@shared/lib/utils'; |
| 2 |
import apiFetch from '@wordpress/api-fetch'; |
| 3 |
|
| 4 |
const { globalStylesPostID } = window.extSharedData; |
| 5 |
|
| 6 |
// Will merge into the current variation and preserve block style variations (vibes) |
| 7 |
export default async ({ variation }) => { |
| 8 |
const currentTheme = await apiFetch({ |
| 9 |
path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, |
| 10 |
}); |
| 11 |
const currentVariations = await apiFetch({ |
| 12 |
path: '/extendify/v1/agent/block-style-variations', |
| 13 |
}); |
| 14 |
|
| 15 |
const preservedBlockVibes = Object.keys(currentVariations).reduce( |
| 16 |
(blocks, blockName) => { |
| 17 |
blocks[blockName] = { variations: currentVariations[blockName] }; |
| 18 |
return blocks; |
| 19 |
}, |
| 20 |
{}, |
| 21 |
); |
| 22 |
|
| 23 |
const final = deepMerge(currentTheme, variation, { |
| 24 |
styles: { blocks: preservedBlockVibes }, |
| 25 |
}); |
| 26 |
|
| 27 |
return apiFetch({ |
| 28 |
method: 'POST', |
| 29 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 30 |
data: { id: globalStylesPostID, ...final }, |
| 31 |
}); |
| 32 |
}; |
| 33 |
|