import React from "react";
import { getGoogleApiKey } from "../services/ggsh.service";
import Loading from "./loading.component";

const withGoogleApiKey = ComposedComponent =>
  class extends React.Component {
    constructor(props) {
      super(props);
      this.state = { googleApiKey: null }
    }

    async componentDidMount() {
      const googleApiKey = await getGoogleApiKey();
      this.setState({ googleApiKey });
    }

    render() {
      const { googleApiKey } = this.state;
      if (!googleApiKey) {
        return <Loading/>
      }

      return <ComposedComponent {...this.props} googleApiKey={googleApiKey} />
    }
  }

export default withGoogleApiKey;
