| 1 |
import { useEffect, useRef } from '@wordpress/element' |
| 2 |
import { __ } from '@wordpress/i18n' |
| 3 |
import { getGoals } from '@onboarding/api/DataApi' |
| 4 |
import { CheckboxInput } from '@onboarding/components/CheckboxInput' |
| 5 |
import { useFetch } from '@onboarding/hooks/useFetch' |
| 6 |
import { PageLayout } from '@onboarding/layouts/PageLayout' |
| 7 |
import { usePagesStore } from '@onboarding/state/Pages' |
| 8 |
import { useUserSelectionStore } from '@onboarding/state/UserSelections' |
| 9 |
import { pageState } from '@onboarding/state/factory' |
| 10 |
|
| 11 |
export const fetcher = () => getGoals() |
| 12 |
export const fetchData = () => ({ key: 'goals' }) |
| 13 |
export const state = pageState('Goals', () => ({ |
| 14 |
title: __('Goals', 'extendify'), |
| 15 |
default: undefined, |
| 16 |
showInSidebar: true, |
| 17 |
ready: false, |
| 18 |
isDefault: () => { |
| 19 |
// If no goals are selected and no text is entered |
| 20 |
const { feedbackMissingGoal, goals } = useUserSelectionStore.getState() |
| 21 |
return !feedbackMissingGoal?.length && goals?.length === 0 |
| 22 |
}, |
| 23 |
})) |
| 24 |
export const Goals = () => { |
| 25 |
const { data: goals, loading } = useFetch(fetchData, fetcher) |
| 26 |
const { toggle, has } = useUserSelectionStore() |
| 27 |
const { feedbackMissingGoal: feedback, setFeedbackMissingGoal } = |
| 28 |
useUserSelectionStore() |
| 29 |
const nextPage = usePagesStore((state) => state.nextPage) |
| 30 |
const initialFocus = useRef() |
| 31 |
|
| 32 |
useEffect(() => { |
| 33 |
state.setState({ ready: !loading }) |
| 34 |
}, [loading]) |
| 35 |
|
| 36 |
useEffect(() => { |
| 37 |
if (!initialFocus.current) return |
| 38 |
const raf = requestAnimationFrame(() => |
| 39 |
initialFocus.current.querySelector('input').focus(), |
| 40 |
) |
| 41 |
return () => cancelAnimationFrame(raf) |
| 42 |
}, [initialFocus]) |
| 43 |
|
| 44 |
return ( |
| 45 |
<PageLayout> |
| 46 |
<div> |
| 47 |
<h1 className="text-3xl text-partner-primary-text mb-4 mt-0"> |
| 48 |
{__( |
| 49 |
'What do you want to accomplish with this new site?', |
| 50 |
'extendify', |
| 51 |
)} |
| 52 |
</h1> |
| 53 |
<p className="text-base opacity-70 mb-0"> |
| 54 |
{__('You can change these later.', 'extendify')} |
| 55 |
</p> |
| 56 |
</div> |
| 57 |
<div className="w-full"> |
| 58 |
<h2 className="text-lg m-0 mb-4 text-gray-900"> |
| 59 |
{__('Select the goals relevant to your site:', 'extendify')} |
| 60 |
</h2> |
| 61 |
{loading ? ( |
| 62 |
<p>{__('Loading...', 'extendify')}</p> |
| 63 |
) : ( |
| 64 |
<form |
| 65 |
onSubmit={(e) => { |
| 66 |
e.preventDefault() |
| 67 |
nextPage() |
| 68 |
}} |
| 69 |
className="w-full grid lg:grid-cols-2 gap-3 goal-select"> |
| 70 |
{/* Added so forms can be submitted by pressing Enter */} |
| 71 |
<input type="submit" className="hidden" /> |
| 72 |
{/* Seems excessive but this keeps failing and crashing randomly */} |
| 73 |
{goals && goals?.length > 0 |
| 74 |
? goals?.map((goal, index) => ( |
| 75 |
<div |
| 76 |
key={goal.id} |
| 77 |
className="border border-gray-800 rounded-lg p-4" |
| 78 |
ref={ |
| 79 |
index === 0 ? initialFocus : undefined |
| 80 |
}> |
| 81 |
<CheckboxInput |
| 82 |
label={goal.title} |
| 83 |
checked={has('goals', goal)} |
| 84 |
onChange={() => { |
| 85 |
toggle('goals', goal) |
| 86 |
}} |
| 87 |
/> |
| 88 |
</div> |
| 89 |
)) |
| 90 |
: null} |
| 91 |
</form> |
| 92 |
)} |
| 93 |
{!loading && ( |
| 94 |
<div className="max-w-onboarding-sm mx-auto"> |
| 95 |
<h2 className="text-lg mt-12 mb-4 text-gray-900"> |
| 96 |
{__( |
| 97 |
"Don't see what you're looking for?", |
| 98 |
'extendify', |
| 99 |
)} |
| 100 |
</h2> |
| 101 |
<div className="search-panel flex items-center justify-center relative"> |
| 102 |
<input |
| 103 |
type="text" |
| 104 |
className="w-full bg-gray-100 h-12 pl-4 input-focus rounded-none ring-offset-0 focus:bg-white" |
| 105 |
value={feedback} |
| 106 |
onChange={(e) => |
| 107 |
setFeedbackMissingGoal(e.target.value) |
| 108 |
} |
| 109 |
placeholder={__( |
| 110 |
'Add your goals...', |
| 111 |
'extendify', |
| 112 |
)} |
| 113 |
/> |
| 114 |
</div> |
| 115 |
</div> |
| 116 |
)} |
| 117 |
</div> |
| 118 |
</PageLayout> |
| 119 |
) |
| 120 |
} |
| 121 |
|