import { useCallback, useEffect, useMemo, useState } from 'react' import { __ } from '@wordpress/i18n' import { useDispatch, useSelect } from '@wordpress/data' import { store, store_name } from '../../../store/backend' import { FormProvider } from '../Form/lib/FormProvider' import { createFormFromModel } from '../Form/lib/createForm' import { getFormState } from '../Form/lib/utils' import { createFormMenuSectionsFromModel } from '../Form/utils/utils' import { buildModelFromSettingsFields } from '../Settings/utils/utils' import { Loading } from '../Loading/Loading' import type { ISettingsSection } from '../Settings/types' import './SettingsSectionStepContent.scss' import { capitalize } from '../../utils/capitalize' interface SettingsSectionStepContentProps { sectionId: string checklistStepId: string guideUrl: string emptyMessage: string onSkip: () => void } export const SettingsSectionStepContent = ({ sectionId, checklistStepId, guideUrl, emptyMessage, onSkip, }: SettingsSectionStepContentProps) => { const [activeTab, setActiveTab] = useState('general') const [isSaving, setIsSaving] = useState(false) const [isSkipping, setIsSkipping] = useState(false) const settingsSection = useSelect((select: any) => { const options = select(store).getOptions() as Record< string, ISettingsSection > return options?.[sectionId] || null }, [sectionId]) const isOptionsLoading = useSelect( (select: any) => select(store).getLoadingState('options'), [] ) const { setOptions, completeSetupChecklistStep } = useDispatch( store_name ) as { setOptions: ( section: string, formData: Record ) => Promise completeSetupChecklistStep: (stepId: string) => Promise } const { model, defaultValues } = useMemo(() => { if (!settingsSection?.fields?.length) { return { model: { properties: {} }, defaultValues: {} } } return buildModelFromSettingsFields({ fields: settingsSection.fields }) }, [settingsSection]) const form = useMemo(() => createFormFromModel(model), [model]) const tabSections = useMemo( () => createFormMenuSectionsFromModel({ model, form, modelName: 'settings', }), [model, form] ) const tabLabels = settingsSection?.tabs || {} useEffect(() => { if (Object.keys(defaultValues).length) { form.patchValue(defaultValues) } }, [defaultValues, form]) useEffect(() => { const availableTabs = Object.keys(tabSections).filter( (tabId) => (tabSections[tabId] || []).length > 0 ) if (availableTabs.length && !availableTabs.includes(activeTab)) { setActiveTab(availableTabs[0]) } }, [activeTab, tabSections]) const handleConfirm = useCallback(async () => { const { values, isValid } = getFormState(form) if (!isValid) { return } setIsSaving(true) try { await setOptions(sectionId, values) await completeSetupChecklistStep(checklistStepId) } finally { setIsSaving(false) } }, [checklistStepId, completeSetupChecklistStep, form, sectionId, setOptions]) const handleSkip = useCallback(async () => { setIsSkipping(true) try { await onSkip() } finally { setIsSkipping(false) } }, [onSkip]) if (isOptionsLoading && !settingsSection) { return } if (!settingsSection?.fields?.length) { return (

{emptyMessage}

) } const activeFields = tabSections[activeTab] || [] const availableTabs = Object.keys(tabSections).filter( (tabId) => (tabSections[tabId] || []).length > 0 ) return (
{availableTabs.length > 1 && (
{availableTabs.map((tabId) => ( ))}
)}
{activeFields.map((field) => (
{field.element}
))}
{guideUrl && ( {__('View Guide', 'webba-booking-lite')} )}
) }