import React, { Component, Fragment } from 'react'; import styled from 'styled-components'; import { Button, Spinner } from '@wordpress/components'; import { compose } from '@wordpress/compose'; import { withSelect } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; import { __ } from '@wordpress/i18n'; import Modal from '../../components/modal'; import { store } from '../data'; import { Post } from '../types'; import Manager from './manager'; const StyledModal = styled( Modal )` .altis-audience-manager { &__header { position: fixed; left: 50px; top: 30px; z-index: 10; @media only screen and (max-width: 640px), screen and (max-height: 400px) { left: 20px; top: 0; } h1 { vertical-align: middle; } } } .altis-audience-manager table { width: 100%; } `; const StyledSelect = styled.div` .audience-select__label { display: block; margin-bottom: 4px; } .audience-select__controls { margin-left: -3px; display: flex; button { padding-left: 0; padding-right: 0; text-align: left; } button:not(:disabled):hover strong, button:not(:disabled):focus strong { text-decoration: underline; } .dashicon { margin-right: 3px; } } .audience-select__clear { margin-left: -2px; margin-right: 5px; min-width: 0; } `; interface SelectProps { audience?: number; button?: ( controls: { onOpen: () => void; onClose: () => void } ) => React.ReactNode; buttonLabel?: string; icon?: string; label?: string; hideLabel?: boolean; onSelect: ( id: number, post: Post ) => void; onClearSelection?: () => void; audiencePost?: ( Post & { error?: { message?: string } } ) | null; } interface SelectState { show: boolean; } const ManagerComponent = Manager as unknown as React.ComponentType<{ onSelect?: ( post: Post ) => void }>; class Select extends Component { state: SelectState = { show: false }; render() { const { audience, audiencePost, button = null, buttonLabel, icon = 'groups', label, onSelect, onClearSelection, hideLabel, } = this.props; const { show } = this.state; const buttonLabelDefault = audience ? __( 'Change Audience', 'altis' ) : __( 'Select Audience', 'altis' ); const status = audiencePost?.status || 'draft'; const error = audiencePost?.error?.message; const title = audiencePost?.title?.rendered; const LabelText = hideLabel ? ( { buttonLabel || buttonLabelDefault } ) : ( buttonLabel || buttonLabelDefault ); const ManagerModal = () => ( this.setState( { show: false } ) } > { this.setState( { show: false } ); onSelect( post.id, post ); } } /> ); if ( button ) { return ( <> { button( { onOpen: () => this.setState( { show: true } ), onClose: () => this.setState( { show: false } ), } ) } { show && } ); } return (
{ label && }
{ audience && onClearSelection ? ( ) : null }
{ show && }
); } } const applyWithSelect = withSelect( ( select: ( key: string ) => unknown, props: SelectProps ) => { let audiencePost: SelectProps['audiencePost'] = null; const core = select( 'core' ); const canCreate = core.canUser( 'create', 'audiences' ); const queryArgs = canCreate ? { context: 'edit' } : {}; if ( props.audience ) { audiencePost = select( store ).getPost( props.audience, queryArgs ); } return { audiencePost }; } ); export default compose( applyWithSelect )( Select );