// @flow import React, { Component } from 'react'; import { components, i18n, apiFetch } from '../../wp'; import Loader from '../Loader/Loader'; import './Inserter.css'; const { __ } = i18n; const { Modal, Button, SelectControl } = components; type InserterProps = { pickTest: (id: string) => void; removeSelf: () => void; insertNew: () => void; }; type InserterState = { value: string; isLoading: boolean; isPicking: boolean; options: { ID: string, post_title: string, }[]; }; class Inserter extends Component { state = { value: '', isLoading: true, isPicking: false, options: [], }; componentDidMount() { apiFetch({ path: '/ab-testing-for-wp/v1/get-posts-by-type?type=abt4wp-test' }) .then((options) => { if (options.length === 0) { this.insertNew(); return; } this.setState({ isLoading: false, value: options[0].ID, options, }); }); } insertNew = () => { const { insertNew } = this.props; insertNew(); }; cancelInsert = () => { const { removeSelf } = this.props; removeSelf(); }; insertExisting = () => { const { pickTest } = this.props; const { value } = this.state; pickTest(value); }; render() { const { value, isLoading, isPicking, options, } = this.state; if (isLoading) return
; return ( {isPicking ? (
({ label: option.post_title, value: option.ID, }))} onChange={newValue => this.setState({ value: newValue })} />
) : (
)}
); } } export default Inserter;