| 1 |
import { |
| 2 |
applyDesignBuildHero, |
| 3 |
applyDesignBuildNav, |
| 4 |
} from '@auto-launch/fetchers/get-design-build'; |
| 5 |
import { getHomeShape, homeTemplateShape } from '@auto-launch/fetchers/shape'; |
| 6 |
import { |
| 7 |
fetchWithTimeout, |
| 8 |
retryTwice, |
| 9 |
setStatus, |
| 10 |
} from '@auto-launch/functions/helpers'; |
| 11 |
import { getHeadersAndFooters } from '@auto-launch/functions/wp'; |
| 12 |
import { PATTERNS_HOST } from '@constants'; |
| 13 |
import { digest } from '@shared/api/digest'; |
| 14 |
import { reqDataBasics } from '@shared/lib/data'; |
| 15 |
import { __ } from '@wordpress/i18n'; |
| 16 |
|
| 17 |
const url = `${PATTERNS_HOST}/api/home`; |
| 18 |
const { wpLanguage, showImprint } = window.extSharedData; |
| 19 |
const method = 'POST'; |
| 20 |
const headers = { 'Content-Type': 'application/json' }; |
| 21 |
|
| 22 |
export const handleHome = async ({ |
| 23 |
siteProfile, |
| 24 |
sitePlugins, |
| 25 |
siteImages, |
| 26 |
aiHeaders, |
| 27 |
designBuild, |
| 28 |
}) => { |
| 29 |
// translators: this is for a action log UI. Keep it short |
| 30 |
setStatus(__('Preparing your home page', 'extendify-local')); |
| 31 |
|
| 32 |
const body = JSON.stringify({ |
| 33 |
...reqDataBasics, |
| 34 |
siteProfile, |
| 35 |
siteImages, |
| 36 |
sitePlugins, |
| 37 |
aiHeaders, |
| 38 |
// If pages are passed in they may be used |
| 39 |
pages: designBuild?.pages ?? [], |
| 40 |
buildId: designBuild?.buildId, |
| 41 |
}); |
| 42 |
|
| 43 |
const response = await retryTwice(() => |
| 44 |
fetchWithTimeout(url, { method, headers, body }), |
| 45 |
).catch((error) => { |
| 46 |
return { ok: false, statusText: error.message, status: 0 }; |
| 47 |
}); |
| 48 |
|
| 49 |
if (!response?.ok) { |
| 50 |
digest({ |
| 51 |
error: { |
| 52 |
message: response.statusText, |
| 53 |
name: 'FetchError', |
| 54 |
status: response.status, |
| 55 |
}, |
| 56 |
details: { source: 'auto-launch', caller: 'handleHome' }, |
| 57 |
}); |
| 58 |
throw new Error(response.statusText); |
| 59 |
} |
| 60 |
|
| 61 |
const template = homeTemplateShape.parse(await response.json()); |
| 62 |
template.patterns = applyDesignBuildHero(template.patterns, designBuild); |
| 63 |
template.patterns = applyDesignBuildNav(template.patterns, designBuild); |
| 64 |
|
| 65 |
const hasFooterNav = Array.isArray(showImprint) |
| 66 |
? showImprint.includes(wpLanguage ?? '') && |
| 67 |
siteProfile.category === 'Business' |
| 68 |
: false; |
| 69 |
|
| 70 |
const { headers: head, footers: foot } = await getHeadersAndFooters({ |
| 71 |
useNavFooter: hasFooterNav, |
| 72 |
}); |
| 73 |
const randomHeader = head[Math.floor(Math.random() * head.length)]; |
| 74 |
const randomFooter = foot[Math.floor(Math.random() * foot.length)]; |
| 75 |
const headerCode = |
| 76 |
designBuild?.headerCode ?? randomHeader?.content?.raw?.trim() ?? ''; |
| 77 |
const footerCode = randomFooter?.content?.raw?.trim() ?? ''; |
| 78 |
return getHomeShape.parse({ home: { ...template, headerCode, footerCode } }); |
| 79 |
}; |
| 80 |
|