| 1 |
import { useEffect, useState, useCallback } from '@wordpress/element' |
| 2 |
import { __, sprintf, _n } from '@wordpress/i18n' |
| 3 |
import { installPlugin, updateTemplatePart } from '@onboarding/api/WPApi' |
| 4 |
import { updateOption } from '@onboarding/api/WPApi' |
| 5 |
import { useWarnOnLeave } from '@onboarding/hooks/useWarnOnLeave' |
| 6 |
import { PageLayout } from '@onboarding/layouts/PageLayout' |
| 7 |
import { |
| 8 |
createWordpressPages, |
| 9 |
trashWordpressPages, |
| 10 |
updateGlobalStyleVariant, |
| 11 |
} from '@onboarding/lib/wp' |
| 12 |
import { useGlobalStore } from '@onboarding/state/Global' |
| 13 |
import { useUserSelectionStore } from '@onboarding/state/UserSelections' |
| 14 |
import { Checkmark, SpinnerIcon } from '@onboarding/svg' |
| 15 |
|
| 16 |
export const CreatingSite = () => { |
| 17 |
const canLaunch = useUserSelectionStore((state) => state.canLaunch()) |
| 18 |
const { siteType, siteInformation, pages, style, plugins } = |
| 19 |
useUserSelectionStore() |
| 20 |
const [info, setInfo] = useState([]) |
| 21 |
const inform = (msg) => setInfo((info) => [msg, ...info]) |
| 22 |
useWarnOnLeave() |
| 23 |
|
| 24 |
const doEverything = useCallback(async () => { |
| 25 |
if (!canLaunch) { |
| 26 |
throw new Error(__('Site is not ready to launch.', 'extendify')) |
| 27 |
} |
| 28 |
inform(__('Preparing your website', 'extendify')) |
| 29 |
try { |
| 30 |
await updateOption('blogname', siteInformation.title) |
| 31 |
} catch (e) { |
| 32 |
/* do nothing */ |
| 33 |
} |
| 34 |
await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 35 |
inform(__('Installing your theme', 'extendify')) |
| 36 |
try { |
| 37 |
await updateGlobalStyleVariant(style?.variation ?? {}) |
| 38 |
} catch (e) { |
| 39 |
/* do nothing */ |
| 40 |
} |
| 41 |
if (plugins?.length) { |
| 42 |
inform( |
| 43 |
_n( |
| 44 |
`Getting ready to install ${plugins.length} plugin`, |
| 45 |
`Getting ready to install ${plugins.length} plugins`, |
| 46 |
plugins.length, |
| 47 |
'extendify', |
| 48 |
), |
| 49 |
) |
| 50 |
await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 51 |
for (const [index, plugin] of plugins.entries()) { |
| 52 |
// TODO: instead of updating here, we could have a progress component |
| 53 |
// that we can update a % of the width every index/n |
| 54 |
inform( |
| 55 |
__( |
| 56 |
`Installing (${index + 1}/${plugins.length}): ${ |
| 57 |
plugin.name |
| 58 |
}`, |
| 59 |
'extendify', |
| 60 |
), |
| 61 |
) |
| 62 |
try { |
| 63 |
await installPlugin(plugin) |
| 64 |
} catch (e) { |
| 65 |
/* do nothing */ |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 70 |
inform(__('Inserting header area', 'extendify')) |
| 71 |
try { |
| 72 |
await updateTemplatePart('extendable//header', style?.headerCode) |
| 73 |
} catch (e) { |
| 74 |
/* do nothing */ |
| 75 |
} |
| 76 |
await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 77 |
inform(__('Generating page content', 'extendify')) |
| 78 |
let pageIds |
| 79 |
try { |
| 80 |
pageIds = await createWordpressPages(pages, siteType, style) |
| 81 |
} catch (e) { |
| 82 |
/* do nothing */ |
| 83 |
} |
| 84 |
await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 85 |
inform(__('Inserting footer area', 'extendify')) |
| 86 |
try { |
| 87 |
await updateTemplatePart('extendable//footer', style?.footerCode) |
| 88 |
} catch (e) { |
| 89 |
/* do nothing */ |
| 90 |
} |
| 91 |
await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 92 |
inform(__('Finalizing your site', 'extendify')) |
| 93 |
try { |
| 94 |
await trashWordpressPages([ |
| 95 |
{ slug: 'hello-world', type: 'post' }, |
| 96 |
{ slug: 'sample-page', type: 'page' }, |
| 97 |
]) |
| 98 |
} catch (e) { |
| 99 |
/* do nothing */ |
| 100 |
} |
| 101 |
|
| 102 |
return pageIds |
| 103 |
}, [pages, plugins, siteType, style, canLaunch, siteInformation.title]) |
| 104 |
|
| 105 |
useEffect(() => { |
| 106 |
doEverything().then((pageIds) => { |
| 107 |
// This will load up the finished page |
| 108 |
useGlobalStore.setState({ generatedPages: pageIds }) |
| 109 |
}) |
| 110 |
}, [doEverything]) |
| 111 |
|
| 112 |
return ( |
| 113 |
<PageLayout includeNav={false}> |
| 114 |
<div> |
| 115 |
<h1 className="text-3xl text-partner-primary-text mb-4 mt-0"> |
| 116 |
{__('Building your site now!', 'extendify')} |
| 117 |
</h1> |
| 118 |
<p className="text-base mb-0"> |
| 119 |
{__("Please don't close the window.", 'extendify')} |
| 120 |
</p> |
| 121 |
</div> |
| 122 |
<div className="w-full"> |
| 123 |
<div className="flex flex-col items-start space-y-4"> |
| 124 |
{info.map((step, index) => { |
| 125 |
if (!index) { |
| 126 |
return ( |
| 127 |
<div |
| 128 |
className="text-4xl flex space-x-4 items-center" |
| 129 |
key={step}> |
| 130 |
<SpinnerIcon className="spin w-10 mr-2" /> |
| 131 |
{sprintf(step, '...')} |
| 132 |
</div> |
| 133 |
) |
| 134 |
} |
| 135 |
return ( |
| 136 |
<div |
| 137 |
className="ml-12 text-base text-gray-500 flex" |
| 138 |
key={step}> |
| 139 |
<Checkmark className="text-green-500 w-6 mr-1" /> |
| 140 |
{sprintf(step, '...')} |
| 141 |
</div> |
| 142 |
) |
| 143 |
})} |
| 144 |
</div> |
| 145 |
</div> |
| 146 |
</PageLayout> |
| 147 |
) |
| 148 |
} |
| 149 |
|