| 1 |
import { useState, useEffect } from '@wordpress/element' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { useIsMounted } from '@library/hooks/helpers' |
| 4 |
import { getLayoutTypes } from '@onboarding/api/DataApi' |
| 5 |
import { PagePreview } from '@onboarding/components/PagePreview' |
| 6 |
import { useFetch } from '@onboarding/hooks/useFetch' |
| 7 |
import { PageLayout } from '@onboarding/layouts/PageLayout' |
| 8 |
import { useUserSelectionStore } from '@onboarding/state/UserSelections' |
| 9 |
import { pageState } from '@onboarding/state/factory' |
| 10 |
|
| 11 |
export const fetcher = async () => { |
| 12 |
const layoutTypes = await getLayoutTypes() |
| 13 |
const pageRecords = layoutTypes?.data ?? [] |
| 14 |
if (!pageRecords?.length) throw new Error('Error fetching pages') |
| 15 |
|
| 16 |
// Home first and sort the other pages |
| 17 |
const homePage = pageRecords[0] |
| 18 |
const otherPages = pageRecords |
| 19 |
.slice(1) |
| 20 |
?.sort((a, b) => (a.title > b.title ? 1 : -1)) |
| 21 |
return { data: [homePage, ...(otherPages ?? [])] } |
| 22 |
} |
| 23 |
export const fetchData = () => { |
| 24 |
return { key: 'layout-types' } |
| 25 |
} |
| 26 |
export const state = pageState('Pages', (set, get) => ({ |
| 27 |
title: __('Pages', 'extendify'), |
| 28 |
// default will end up just a list of all the pages |
| 29 |
default: undefined, |
| 30 |
showInSidebar: true, |
| 31 |
ready: false, |
| 32 |
isDefault: () => |
| 33 |
useUserSelectionStore.getState().pages?.length === |
| 34 |
get().default?.length, |
| 35 |
})) |
| 36 |
export const SitePages = () => { |
| 37 |
const { data: availablePages } = useFetch(fetchData, fetcher) |
| 38 |
const [pagesToShow, setPagesToShow] = useState([]) |
| 39 |
const { add, goals, reset } = useUserSelectionStore() |
| 40 |
const isMounted = useIsMounted() |
| 41 |
|
| 42 |
useEffect(() => { |
| 43 |
if (pagesToShow?.length === availablePages?.length) { |
| 44 |
state.setState({ ready: true }) |
| 45 |
} |
| 46 |
}, [availablePages?.length, pagesToShow?.length]) |
| 47 |
|
| 48 |
useEffect(() => { |
| 49 |
if (!availablePages?.length) return |
| 50 |
const pagesbyGoal = availablePages.filter((page) => { |
| 51 |
// Show all if the user hasn't selected any goals |
| 52 |
if (!goals?.length) return true |
| 53 |
// If this page has no associated goals, show it |
| 54 |
if (!page?.goals?.length) return true |
| 55 |
// If this page has goals that the user has selected, show it |
| 56 |
return ( |
| 57 |
// Check whether the goals intersect |
| 58 |
page?.goals?.some((goal) => goals.some((g) => goal == g.id)) ?? |
| 59 |
true |
| 60 |
) |
| 61 |
}) |
| 62 |
;(async () => { |
| 63 |
for (const page of pagesbyGoal) { |
| 64 |
if (!isMounted.current) return |
| 65 |
setPagesToShow((pages) => [...pages, page]) |
| 66 |
await new Promise((resolve) => setTimeout(resolve, 100)) |
| 67 |
} |
| 68 |
state.setState({ ready: true }) |
| 69 |
})() |
| 70 |
}, [availablePages, goals, isMounted]) |
| 71 |
|
| 72 |
// Select all pages by default |
| 73 |
useEffect(() => { |
| 74 |
reset('pages') |
| 75 |
pagesToShow?.map((page) => add('pages', page)) |
| 76 |
state.setState({ default: pagesToShow }) |
| 77 |
}, [pagesToShow, add, reset]) |
| 78 |
|
| 79 |
return ( |
| 80 |
<PageLayout> |
| 81 |
<div> |
| 82 |
<h1 className="text-3xl text-partner-primary-text mb-4 mt-0"> |
| 83 |
{__('What pages do you want on this site?', 'extendify')} |
| 84 |
</h1> |
| 85 |
<p className="text-base opacity-70 mb-0"> |
| 86 |
{__('You may add more later', 'extendify')} |
| 87 |
</p> |
| 88 |
</div> |
| 89 |
<div className="w-full"> |
| 90 |
<h2 className="text-lg m-0 mb-4 text-gray-900"> |
| 91 |
{__( |
| 92 |
"Pick the pages you'd like to add to your site", |
| 93 |
'extendify', |
| 94 |
)} |
| 95 |
</h2> |
| 96 |
<div className="flex gap-6 flex-wrap justify-center"> |
| 97 |
{pagesToShow?.map((page) => { |
| 98 |
if (page.slug !== 'home') |
| 99 |
return ( |
| 100 |
<div |
| 101 |
className="relative" |
| 102 |
style={{ height: 541, width: 352 }} |
| 103 |
key={page.id}> |
| 104 |
<PagePreview |
| 105 |
required={page?.slug === 'home'} |
| 106 |
page={page} |
| 107 |
title={page?.title} |
| 108 |
blockHeight={541} |
| 109 |
/> |
| 110 |
</div> |
| 111 |
) |
| 112 |
})} |
| 113 |
</div> |
| 114 |
</div> |
| 115 |
</PageLayout> |
| 116 |
) |
| 117 |
} |
| 118 |
|