// External Dependencies import React, {Component} from 'react'; import $ from 'jquery'; // Internal Dependencies import './style.css'; class WpmfPdfEmbedDivi extends Component { static slug = 'wpmf_pdf_embed'; constructor(props) { super(props); this.state = { html: '', loading: true, embed: true }; this.wrap = React.createRef(); } componentWillMount() { if (typeof this.props.url !== "undefined") { this.loadHtml(this.props); } } componentWillReceiveProps(nextProps) { if (this.props.url !== nextProps.url || this.props.embed !== nextProps.embed || this.props.target !== nextProps.target) { this.loadHtml(nextProps); } } loadHtml(props) { if (!this.state.loading) { this.setState({ loading: true }); } let width = ''; let height = ''; if (typeof props.width !== "undefined") { width = props.width; } else { if (typeof props.max_width !== "undefined") { width = props.max_width; } } if (typeof props.height !== "undefined") { height = props.height; } else { if (typeof props.max_height !== "undefined") { height = props.max_height; } } fetch(window.et_fb_options.ajaxurl + `?action=wpmf_divi_load_pdf_embed_html&url=${props.url}&embed=${props.embed}&target=${props.target}&width=${width}&height=${height}&et_admin_load_nonce=${window.et_fb_options.et_admin_load_nonce}`) .then(res => res.json()) .then( (result) => { this.setState({ html: result.html, loading: false, embed: false }); }, // errors (error) => { this.setState({ html: '', loading: true }); } ); } componentDidUpdate(prevProps, prevState) { if (this.props.embed === 'on' && !this.state.embed) { if($(this.wrap.current).find(".wpmf-pdfemb-viewer").length) { $(this.wrap.current).find(".wpmf-pdfemb-viewer").pdfEmbedder(); } this.setState({ embed: true }); } } render() { const loadingIcon = ( ); if (typeof this.props.url === "undefined") { return (
{'Please select a PDF file to activate the preview'}
); } if (this.state.loading) { return (
{loadingIcon}
); } if (!this.state.loading) { return (
); } } } export default WpmfPdfEmbedDivi;