import { __ } from '@wordpress/i18n' import { useEffect } from 'react' import { FormComponentConstructor } from '../../lib/types' import { FormFieldProps } from '../../types' import { useField } from '../../lib/hooks/useField' import { usePushSubscription } from '../../../../hooks/usePushSubscription' import './PushSubscribeField.scss' export const createPushSubscribeField: FormComponentConstructor = ({ field, }) => { return ({ label }: FormFieldProps) => { const { setValue } = useField(field) const { isLoading, isSubscribed, status, enable, disable, sendTest } = usePushSubscription() useEffect(() => { setValue(isSubscribed ? 'true' : '') }, [isSubscribed, setValue]) const handleEnable = async () => { const enabled = await enable() if (enabled) { setValue('true') } } const handleDisable = async () => { const disabled = await disable() if (disabled) { setValue('') } } return (
{label && (
{label}
)}
{!isSubscribed && ( )} {isSubscribed && ( <> )}
{status && (

{status.message}

)}
) } }