/** * WordPress dependencies */ import apiFetch from '@safe-wordpress/api-fetch'; import { Spinner } from '@safe-wordpress/components'; import { useSelect } from '@safe-wordpress/data'; import { useEffect } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { store as NC_DATA } from '@nelio-content/data'; /** * Internal dependencies */ import './style.scss'; import { DEFAULT_ATTRS } from './config'; import { useAttributes, useValue } from '../hooks'; import type { FieldSettingProps } from '../types'; type Response = { readonly notificationEmails?: ReadonlyArray< string >; }; export const CloudNotificationEmailsSetting = ( { name, }: FieldSettingProps ): JSX.Element => { const [ attributes, setAttributes ] = useAttributes( name, DEFAULT_ATTRS ); const [ value = [], setValue ] = useValue< ReadonlyArray< string > >( name ); const siteId = useSelect( ( select ) => select( NC_DATA ).getSiteId(), [] ); const apiRoot = useSelect( ( select ) => select( NC_DATA ).getApiRoot(), [] ); const token = useSelect( ( select ) => select( NC_DATA ).getAuthenticationToken(), [] ); const { status } = attributes; useEffect( () => { void ( async () => { if ( ! siteId || ! token ) { return; } try { const response = await apiFetch< Response >( { url: `${ apiRoot }/site/${ siteId }`, method: 'GET', credentials: 'omit', mode: 'cors', headers: { Authorization: `Bearer ${ token }`, }, } ); const { notificationEmails = [] } = response; setValue( notificationEmails ); setAttributes( { status: 'ready' } ); } catch ( _ ) { setValue( [] ); setAttributes( { status: 'error' } ); } } )(); }, [ siteId, token, apiRoot, setAttributes, setValue ] ); return (
{ 'loading' === status && } { 'error' === status && (

{ _x( 'Unable to retrieve cloud notification emails. Please try again later.', 'user', 'nelio-content' ) }

) } { 'ready' === status && ( <> x.replace( /\s/g, '' ) ) .filter( Boolean ) .join( ',' ) } />

{ _x( 'Please write one email address per line:', 'user', 'nelio-content' ) }