import { useEffect, useRef } from '@wordpress/element' import { __ } from '@wordpress/i18n' import classNames from 'classnames' import { getGoals } from '@onboarding/api/DataApi' import { CheckboxInputCard } from '@onboarding/components/CheckboxInputCard' import { SquareNextButton } from '@onboarding/components/SquareNextButton' import { useFetch } from '@onboarding/hooks/useFetch' import { PageLayout } from '@onboarding/layouts/PageLayout' import { usePagesStore } from '@onboarding/state/Pages' import { useUserSelectionStore } from '@onboarding/state/UserSelections' import { pageState } from '@onboarding/state/factory' import { BarChart, Design, Donate, OpenEnvelope, Pencil, Planner, PriceTag, Shop, Speech, Ticket, } from '@onboarding/svg' export const fetcher = () => getGoals() export const fetchData = () => ({ key: 'goals' }) export const state = pageState('Goals', () => ({ title: __('Goals', 'extendify'), default: undefined, showInSidebar: true, ready: false, isDefault: () => { // If no goals are selected and no text is entered const { feedbackMissingGoal, goals } = useUserSelectionStore.getState() return !feedbackMissingGoal?.length && goals?.length === 0 }, })) export const Goals = () => { const { data: goals, loading } = useFetch(fetchData, fetcher) const { toggle, has } = useUserSelectionStore() const { feedbackMissingGoal: feedback, setFeedbackMissingGoal, goals: selectedGoals, } = useUserSelectionStore() const nextPage = usePagesStore((state) => state.nextPage) const initialFocus = useRef() const showMissingInput = () => window.extOnbData?.activeTests?.['remove-dont-see-inputs'] === 'A' const userSelectedGoals = selectedGoals.map((goal) => goal.slug) const iconComponents = { BarChart, Design, Donate, OpenEnvelope, Pencil, Planner, PriceTag, Shop, Speech, Ticket, } useEffect(() => { if (loading) return state.setState({ ready: true }) }, [loading]) useEffect(() => { if (!initialFocus.current) return const raf = requestAnimationFrame(() => initialFocus.current?.querySelector('input')?.focus(), ) return () => cancelAnimationFrame(raf) }, [initialFocus]) return (

{__( 'What do you want to accomplish with this new site?', 'extendify', )}

{__('You can change these later.', 'extendify')}

{__('Select the goals relevant to your site:', 'extendify')}

{loading ? (

{__('Loading...', 'extendify')}

) : (
{ e.preventDefault() nextPage() }} className="w-full grid lg:grid-cols-2 gap-4 goal-select"> {/* Added so forms can be submitted by pressing Enter */} {goals?.map((goal, index) => { const selected = userSelectedGoals.includes( goal.slug, ) const Icon = iconComponents[goal.icon] return (
{ toggle('goals', goal) }} Icon={Icon} />
) })}
)} {!loading && showMissingInput() && (

{__( "Don't see what you're looking for?", 'extendify', )}

setFeedbackMissingGoal(e.target.value) } placeholder={__( 'Add your goals...', 'extendify', )} />
)}
) }