| 1 |
import { useMemo } from '@wordpress/element' |
| 2 |
import { __, sprintf } from '@wordpress/i18n' |
| 3 |
import classNames from 'classnames' |
| 4 |
import { getTemplate } from '@onboarding/api/DataApi' |
| 5 |
import { StylePreview } from '@onboarding/components/StyledPreview' |
| 6 |
import { useFetch } from '@onboarding/hooks/useFetch' |
| 7 |
import { findTheCode } from '@onboarding/lib/util' |
| 8 |
import { useUserSelectionStore } from '@onboarding/state/UserSelections' |
| 9 |
import { Checkmark } from '@onboarding/svg' |
| 10 |
|
| 11 |
export const fetcher = (data) => getTemplate(data) |
| 12 |
export const PagePreview = ({ |
| 13 |
page, |
| 14 |
blockHeight, |
| 15 |
required = false, |
| 16 |
displayOnly = false, |
| 17 |
title = '', |
| 18 |
}) => { |
| 19 |
const { siteType, style, toggle, has } = useUserSelectionStore() |
| 20 |
const isHome = page?.slug === 'home' |
| 21 |
const { data: pageData } = useFetch( |
| 22 |
{ |
| 23 |
siteType: siteType.slug, |
| 24 |
layoutType: page.slug, |
| 25 |
baseLayout: isHome |
| 26 |
? siteType.slug.startsWith('blog') |
| 27 |
? style?.blogBaseLayout |
| 28 |
: style?.homeBaseLayout |
| 29 |
: null, |
| 30 |
kit: page.slug !== 'home' ? style?.kit : null, |
| 31 |
}, |
| 32 |
fetcher, |
| 33 |
) |
| 34 |
if (displayOnly) { |
| 35 |
return ( |
| 36 |
<div |
| 37 |
className="text-base p-2 bg-transparent overflow-hidden rounded-lg border border-gray-100" |
| 38 |
style={{ height: blockHeight }}> |
| 39 |
{title && ( |
| 40 |
<div className="p-3 pb-0 bg-white text-left">{title}</div> |
| 41 |
)} |
| 42 |
<StylePreviewWrapper |
| 43 |
key={style?.recordId} |
| 44 |
page={page} |
| 45 |
measure={false} |
| 46 |
blockHeight={blockHeight} |
| 47 |
style={{ |
| 48 |
...style, |
| 49 |
code: findTheCode({ template: pageData }), |
| 50 |
}} |
| 51 |
/> |
| 52 |
</div> |
| 53 |
) |
| 54 |
} |
| 55 |
|
| 56 |
return ( |
| 57 |
<div |
| 58 |
role="button" |
| 59 |
tabIndex={0} |
| 60 |
aria-label={__('Press to select', 'extendify')} |
| 61 |
disabled={required} |
| 62 |
className="text-base p-0 bg-transparent overflow-hidden rounded-lg border border-gray-100 button-focus" |
| 63 |
onClick={() => required || toggle('pages', page)} |
| 64 |
title={ |
| 65 |
required && title |
| 66 |
? sprintf(__('%s page is required', 'extendify'), title) |
| 67 |
: sprintf(__('Toggle %s page', 'extendify'), title) |
| 68 |
} |
| 69 |
onKeyDown={(e) => { |
| 70 |
if (['Enter', 'Space', ' '].includes(e.key)) { |
| 71 |
if (!required) toggle('pages', page) |
| 72 |
} |
| 73 |
}}> |
| 74 |
<div className="border-gray-100 border-b-0 min-w-sm z-30 relative bg-white pt-3 px-3 pb-1.5 flex justify-between items-center"> |
| 75 |
{title && ( |
| 76 |
<div |
| 77 |
className={classNames('flex items-center', { |
| 78 |
'text-gray-700': !has('pages', page), |
| 79 |
})}> |
| 80 |
<span className="text-left">{title}</span> |
| 81 |
{required && ( |
| 82 |
<span className="w-4 h-4 text-base leading-none pl-2 mr-6 dashicons dashicons-lock"></span> |
| 83 |
)} |
| 84 |
</div> |
| 85 |
)} |
| 86 |
{has('pages', page) ? ( |
| 87 |
<div |
| 88 |
className={classNames('w-5 h-5 rounded-sm', { |
| 89 |
'bg-gray-700': required, |
| 90 |
'bg-partner-primary-bg': !required, |
| 91 |
})}> |
| 92 |
<Checkmark className="text-white w-5" /> |
| 93 |
</div> |
| 94 |
) : ( |
| 95 |
<div |
| 96 |
className={classNames('border w-5 h-5 rounded-sm', { |
| 97 |
'border-gray-700': required, |
| 98 |
'border-partner-primary-bg': !required, |
| 99 |
})}></div> |
| 100 |
)} |
| 101 |
</div> |
| 102 |
<div className="p-2 relative" style={{ height: blockHeight - 44 }}> |
| 103 |
<StylePreviewWrapper |
| 104 |
key={style?.recordId} |
| 105 |
page={page} |
| 106 |
blockHeight={blockHeight} |
| 107 |
style={{ |
| 108 |
...style, |
| 109 |
code: findTheCode({ template: pageData }), |
| 110 |
}} |
| 111 |
/> |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
) |
| 115 |
} |
| 116 |
|
| 117 |
const StylePreviewWrapper = ({ |
| 118 |
page, |
| 119 |
style, |
| 120 |
measure = true, |
| 121 |
blockHeight = false, |
| 122 |
}) => { |
| 123 |
const context = useMemo( |
| 124 |
() => ({ |
| 125 |
type: 'page', |
| 126 |
detail: page.slug, |
| 127 |
measure, |
| 128 |
}), |
| 129 |
[page, measure], |
| 130 |
) |
| 131 |
return ( |
| 132 |
<StylePreview |
| 133 |
style={style} |
| 134 |
context={context} |
| 135 |
blockHeight={blockHeight} |
| 136 |
/> |
| 137 |
) |
| 138 |
} |
| 139 |
|