// Native
import { Flex, Icon, ToggleControl } from '@wordpress/components';
import { useState, useEffect, useRef } from '@wordpress/element';
import { select, subscribe } from '@wordpress/data';
import { info as infoIcon } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
// Custom
import {
NotificationNewTestAdded,
NotificationUnlockMoreTests,
NotificationUpgradeRequired,
NotificationConnectionFailed,
} from 'editor/components/metabox-notifications';
import Settings from 'editor/components/settings';
import apiFetch from '@wordpress/api-fetch';
const Metabox = () => {
const upgradeUrl = window.vrts_editor_vars.upgrade_url;
const pluginUrl = window.vrts_editor_vars.plugin_url;
const testStatus = window.vrts_editor_vars.test_status;
const screenshot = window.vrts_editor_vars.screenshot;
const postId = select( 'core/editor' ).getCurrentPostId();
const [ postStatus, setPostStatus ] = useState(
select( 'core/editor' ).getEditedPostAttribute( 'status' )
);
const [ loading, setLoading ] = useState( true );
const [ disabled, setDisabled ] = useState( true );
const [ test, setTest ] = useState( {} );
const [ credits, setCredits ] = useState( {} );
const [ newTest, setNewTest ] = useState( false );
async function createTest() {
setLoading( true );
try {
const response = await apiFetch( {
path: `/vrts/v1/tests/post/${ postId }`,
method: 'POST',
} );
setTest( response );
if ( test.service_test_id ) {
setCredits( {
...credits,
remaining_tests: credits.remaining_tests - 1,
} );
}
} catch ( error ) {
console.log( error ); // eslint-disable-line no-console
}
setLoading( false );
setNewTest( true );
}
async function deleteTest() {
setLoading( true );
try {
const previousServiceTestId = test.service_test_id;
const response = await apiFetch( {
path: `/vrts/v1/tests/post/${ postId }`,
method: 'DELETE',
} );
setTest( response || {} );
setNewTest( false );
if ( previousServiceTestId ) {
setCredits( {
...credits,
remaining_tests: credits.remaining_tests + 1,
} );
}
} catch ( error ) {
console.log( error ); // eslint-disable-line no-console
}
setLoading( false );
}
useEffect( () => {
if ( 'auto-draft' === postStatus ) {
setDisabled( true );
} else {
setDisabled( false );
}
}, [ postStatus ] );
useEffect( () => {
setLoading( true );
async function fetchAndSetTest() {
try {
const response = await apiFetch( {
path: `/vrts/v1/tests/post/${ postId }`,
} );
setTest( response );
} catch ( error ) {
console.log( error ); // eslint-disable-line no-console
}
setLoading( false );
}
fetchAndSetTest();
}, [ postStatus, postId ] );
useEffect( () => {
async function fetchAndSetCredits() {
try {
const response = await apiFetch( {
path: `/vrts/v1/tests`,
} );
setCredits( response );
} catch ( error ) {
console.log( error ); // eslint-disable-line no-console
}
}
setLoading( true );
fetchAndSetCredits();
}, [ postStatus ] );
const wasSavingPost = useRef( select( 'core/editor' ).isSavingPost() );
useEffect( () => {
subscribe( () => {
const newPostStatus =
select( 'core/editor' ).getEditedPostAttribute( 'status' );
const isSavingPost = select( 'core/editor' ).isSavingPost();
if (
wasSavingPost.current &&
! isSavingPost &&
newPostStatus !== postStatus
) {
setPostStatus( newPostStatus );
}
wasSavingPost.current = isSavingPost;
} );
}, [ postStatus ] );
let metaboxNotification = null;
if ( true === newTest ) {
metaboxNotification =
{ __( 'Test Status', 'visual-regression-tests' ) } { testStatus.text }
{ __( 'Snapshot', 'visual-regression-tests' ) }