/** * The Import page's "From another plugin on this site": the newsletter * and CRM plugins Mailpuff can import from (MailPoet, The Newsletter Plugin, * FluentCRM, Groundhogg, or one * an add-on adds; `GET contacts/import/sources`), each with how many people * it holds. Import shows the dry run first (what each person becomes), then * starts the import in the background (`POST contacts/import/sources/`) * and hands the job to the page's progress. Nothing shows when the site has * none of them. */ import { useState } from '@wordpress/element'; import { __, _n, sprintf } from '@wordpress/i18n'; import { useQuery } from '@tanstack/react-query'; import { useUi } from '../runtime'; import { apiGet, apiPost } from '../api/client'; import { errorText, number } from '../lib/format'; /** * What an import from a source does, in words, for its dry run. * * @param {Object} source A source with `statuses`, `tags`, `fields`, `skipped`. * @return {string} The words. */ export function dryRunWords( source ) { const s = source.statuses || {}; const parts = []; const add = ( count, words ) => count > 0 && parts.push( words( count ) ); add( s.subscribed, ( n ) => /* translators: %s: a number of people. */ sprintf( __( '%s subscribed', 'mailpuff' ), number( n ) ) ); add( s.pending, ( n ) => /* translators: %s: a number of people. */ sprintf( __( '%s pending (never confirmed)', 'mailpuff' ), number( n ) ) ); add( s.not_subscribed, ( n ) => /* translators: %s: a number of people. */ sprintf( __( '%s not subscribed', 'mailpuff' ), number( n ) ) ); const left = ( s.unsubscribed || 0 ) + ( s.bounced || 0 ) + ( s.complained || 0 ); add( left, ( n ) => sprintf( /* translators: %s: a number of people. */ _n( '%s who left or bounced, never emailed', '%s who left or bounced, never emailed', n, 'mailpuff' ), number( n ) ) ); const lines = [ /* translators: %s: a list such as "2 subscribed, 1 pending". */ sprintf( __( 'They come in as: %s.', 'mailpuff' ), parts.join( ', ' ) ), ]; if ( source.tags?.length ) { lines.push( /* translators: %s: tag names. */ sprintf( __( 'Tags: %s.', 'mailpuff' ), source.tags.join( ', ' ) ) ); } if ( source.fields?.length ) { lines.push( sprintf( /* translators: %s: field names. */ __( 'Fields: %s.', 'mailpuff' ), source.fields.join( ', ' ) ) ); } lines.push( __( 'People already in Mailpuff keep what they have and only gain tags. Nothing is sent, and nothing changes in the other plugin.', 'mailpuff' ) ); return lines.join( ' ' ); } /** * @param {Object} props * @param {Function} props.onStarted Called with `{ job_id, total }` once an import starts. * @return {?Element} The section, or nothing. */ export default function ImportSources( { onStarted } ) { const { ui, kit } = useUi(); const { Button, Card, CardContent, Row, RowList, toast, useConfirm } = ui; const confirm = useConfirm(); const [ busy, setBusy ] = useState( '' ); const { data } = useQuery( { queryKey: [ 'import-sources' ], queryFn: () => apiGet( 'contacts/import/sources' ), } ); const sources = Array.isArray( data ) ? data : []; if ( ! sources.length ) { return null; } const start = async ( source ) => { const ok = await confirm( { title: sprintf( /* translators: %s: a plugin's name. */ __( 'Import everyone from %s?', 'mailpuff' ), source.label ), description: dryRunWords( source ), confirmLabel: sprintf( /* translators: %s: a number of people. */ _n( 'Import %s person', 'Import %s people', source.total, 'mailpuff' ), number( source.total ) ), cancelLabel: __( 'Cancel', 'mailpuff' ), } ); if ( ! ok ) { return; } setBusy( source.id ); try { const started = await apiPost( `contacts/import/sources/${ encodeURIComponent( source.id ) }`, {} ); onStarted( started ); } catch ( err ) { toast.error( errorText( err, __( 'Could not start the import.', 'mailpuff' ) ) ); } finally { setBusy( '' ); } }; return (
{ sources.map( ( source ) => ( start( source ) } > { __( 'Import', 'mailpuff' ) } } /> ) ) }
); }