/**
* WordPress dependencies
*/
import { useSelect } from '@safe-wordpress/data';
import { useState } from '@safe-wordpress/element';
import {
Button,
SelectControl,
Spinner,
TextControl,
} from '@safe-wordpress/components';
import { _nx, _x } from '@safe-wordpress/i18n';
/**
* External dependencies
*/
import { values } from 'lodash';
import { usePostTypes } from '@nelio-content/data';
import { ItemSelectControl } from '@nelio-content/components';
import type {
RegularAutomationGroupId,
TaxonomySlug,
TermId,
} from '@nelio-content/types';
/**
* Internal dependencies
*/
import { PublicationControl } from './publication-control';
import { ProfileSelector } from './profile-selector';
import './style.scss';
import {
useEntityRecordLoader,
useRegularGroupProperty,
useSupportsAuthor,
useTaxonomies,
} from '~/nelio-content-pages/settings/automations/hooks';
import { store as NC_AUTOMATION_SETTINGS } from '~/nelio-content-pages/settings/automations/store';
export type RegularSettingsProps = {
readonly groupId: RegularAutomationGroupId;
};
export const RegularSettings = ( {
groupId,
}: RegularSettingsProps ): JSX.Element => {
const postTypes = usePostTypes( 'social' ).map( ( t ) => ( {
value: t.name,
label: t.labels.plural,
} ) );
const [ name, setName ] = useRegularGroupProperty( groupId, 'name' );
const [ postType, setPostType ] = useRegularGroupProperty(
groupId,
'postType'
);
const isPostTypeValid = useSelect(
( select ) =>
select( NC_AUTOMATION_SETTINGS ).isPostTypeValid( groupId ),
[ groupId ]
);
return (
{ _x(
'Which content should be shared?',
'user',
'nelio-content'
) }
{ _x(
'The selected post type is not valid.',
'text',
'nelio-content'
) }
)
}
options={ postTypes }
value={ postType }
onChange={ setPostType }
/>
);
};
// ============
// HELPER VIEWS
// ============
const Details = ( { groupId }: RegularSettingsProps ): JSX.Element => {
const [ postType ] = useRegularGroupProperty( groupId, 'postType' );
const areAuthorsSupported = useSupportsAuthor( postType );
const [ groupTaxonomies = {}, setTaxonomies ] = useRegularGroupProperty(
groupId,
'taxonomies'
);
const [ authors = [], setAuthors ] = useRegularGroupProperty(
groupId,
'authors'
);
const [ publication = { type: 'always' }, setPublication ] =
useRegularGroupProperty( groupId, 'publication' );
const [ areDetailsVisible, showDetails ] = useState(
// Some taxonomy term or author or publication filter selected.
values( groupTaxonomies ).some( ( terms ) => terms.length ) ||
authors.length ||
( publication && publication.type !== 'always' )
);
const taxonomies = useTaxonomies( postType );
const isReady = useEntityRecordLoader( postType );
const { areAuthorsValid, areTermsValid } = useSelect(
( select ) => {
const s = select( NC_AUTOMATION_SETTINGS );
return {
areAuthorsValid: s.areAuthorsValid( groupId ),
areTermsValid: s.getGroupTermsValidation( groupId ),
};
},
[ groupId ]
);
if ( ! areDetailsVisible ) {
return (
);
}
if ( ! isReady ) {
return (
);
}
return (
{ taxonomies.map( ( t ) => (
) =>
setTaxonomies( {
...groupTaxonomies,
[ t.slug ]: terms,
} )
}
/>
) ) }
{ areAuthorsSupported && (
) }
);
};