import React from 'react'; import { useDispatch, useSelect } from '@wordpress/data'; import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { Post, trackEvent } from '../../../utils/admin'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; type Props = { listId: string, item?: Post|null, onCancel(): void, onSuccess( post: Post ): void, }; /** * Create a Broadcast modal component. * * @param {Object} props Props. * * @return {React.element} Modal component. */ export default function Upsert( props: Props ) { const { listId, item, onCancel, onSuccess, } = props; const { createPost, updatePost } = useDispatch( 'accelerate' ); const isSaving: boolean = useSelect( select => select( 'accelerate' ).getIsUpdating() ); const [ title, setTitle ] = useState( item ? item.title : '' ); const onSave = async function ( e: React.FormEvent ) { e.preventDefault(); let post: Post; if ( item ) { post = await updatePost( { id: item.id, title, type: 'broadcast', } ); trackEvent( 'list_action', { list: listId, action: 'rename', type: 'broadcast', } ); } else { post = await createPost( { title, type: 'broadcast', status: 'publish', } ); trackEvent( 'list_action', { list: listId, action: 'create', type: 'broadcast', } ); } onSuccess( post ); }; return (
setTitle( e.target.value ) } />
); }