| 1 |
import { applyDesignBuildOrder } from '@auto-launch/fetchers/get-design-build'; |
| 2 |
import { getPagesShape, pageTemplateShape } from '@auto-launch/fetchers/shape'; |
| 3 |
import { |
| 4 |
fetchWithTimeout, |
| 5 |
retryTwice, |
| 6 |
setStatus, |
| 7 |
} from '@auto-launch/functions/helpers'; |
| 8 |
import { PATTERNS_HOST } from '@constants'; |
| 9 |
import { reqDataBasics } from '@shared/lib/data'; |
| 10 |
import { __ } from '@wordpress/i18n'; |
| 11 |
import { z } from 'zod'; |
| 12 |
|
| 13 |
const url = `${PATTERNS_HOST}/api/page-templates`; |
| 14 |
const method = 'POST'; |
| 15 |
const headers = { 'Content-Type': 'application/json' }; |
| 16 |
|
| 17 |
const shapeLocal = z.looseObject({ |
| 18 |
recommended: z.array(pageTemplateShape), |
| 19 |
}); |
| 20 |
|
| 21 |
export const handlePages = async ({ |
| 22 |
siteProfile, |
| 23 |
sitePlugins, |
| 24 |
siteStyle, |
| 25 |
siteImages, |
| 26 |
designBuild, |
| 27 |
}) => { |
| 28 |
if (siteProfile.structure !== 'multi-page') { |
| 29 |
return { pages: [] }; |
| 30 |
} |
| 31 |
|
| 32 |
// translators: this is for a action log UI. Keep it short |
| 33 |
setStatus(__('Preparing your pages', 'extendify-local')); |
| 34 |
|
| 35 |
const body = JSON.stringify({ |
| 36 |
...reqDataBasics, |
| 37 |
siteProfile, |
| 38 |
siteStyle, |
| 39 |
siteImages: { siteImages }, |
| 40 |
sitePlugins, |
| 41 |
includeOptional: false, |
| 42 |
// If pages are passed in they may be used |
| 43 |
pages: designBuild?.pages ?? [], |
| 44 |
buildId: designBuild?.buildId, |
| 45 |
}); |
| 46 |
|
| 47 |
const response = await retryTwice(() => |
| 48 |
fetchWithTimeout(url, { method, headers, body }), |
| 49 |
); |
| 50 |
const template = shapeLocal.parse(await response.json()); |
| 51 |
const pages = applyDesignBuildOrder(template.recommended, designBuild); |
| 52 |
|
| 53 |
return getPagesShape.parse({ pages }); |
| 54 |
}; |
| 55 |
|