0

First of all, apologies for my grammatical errors. My English level is not good.

I've created a project where you can create your own maps with layers, ok?

And I export my map inside an iframe, so I can load one or multiples maps in a index.html file.

I've created an API inside my project and I want to access this API from my index.html.

How can access it??

I have read that I can access with a :

Which is better option??

How can declare it in my index.html??

Here is my code:

class APIService extends Component {

    constructor(props) {
        super(props);
        this.logged = (this.props.userLogged && this.props.userLogged.logged) ? true : false;
        this.interval = undefined;
    }

    componentWillUnmount() {
        clearInterval(this.interval);
        this.interval = undefined;
    };

    getUserLayers = () => {
        if (!this.logged) return null; 
        const userlayers = this.props.layers.filter(l => l.name).map(l => {
            return { id: l.id, name: l.name, order: l.order };
        });
        return userlayers;
    };

    getVisibilityLayerById = (id) => {
        if (!this.logged) return null; 
        let index = this.props.layers.findIndex(l => l.id === id);
        if (index === -1) return null;
        return (this.props.layers[index] && this.props.layers[index].layout && this.props.layers[index].layout.visibility) ? this.props.layers[index].layout.visibility : null;
    };

    getVisibilityLayerByOrder = (order) => {
        if (!this.logged) return null; 
        let index = this.props.layers.findIndex(l => l.order === order);
        if (index === -1) return null;
        return (this.props.layers[index] && this.props.layers[index].layout && this.props.layers[index].layout.visibility) ? this.props.layers[index].layout.visibility : null;
    };

    changeVisibilityLayerById = (id) => {
        if (!this.logged) return null; 
        let index = this.props.layers.findIndex(l => l.id === id);
        if (index === -1) return null;
        let layer = this.props.layers[index];
        this.props.changeLayerVisibility(layer);
    };

    changeVisibilityLayerByOrder = (order) => {
        if (!this.logged) return null; 
        let index = this.props.layers.findIndex(l => l.order === order);
        if (index === -1) return null;
        let layer = this.props.layers[index];
        this.props.changeLayerVisibility(layer);
    };

    changeLayerVisibilityWithIntervalById = (id, interval) => {
        if (!this.logged) return null;
        setInterval( () => {
            this.changeVisibilityLayerById(id);
        }, interval);
    };

    changeLayerVisibilityWithIntervalByOrder = (order, interval) => {
        if (!this.logged) return null;
        setInterval( () => {
            this.changeVisibilityLayerByOrder(order);
        }, interval);
    };

    getCenter = () => {
        if (!this.logged) return null;
        let properties = (this.props.properties !== null) ? Object.assign({}, this.props.properties) : null;
        let hasCenter = (properties !== null && Object.keys(properties).filter(p => p === "center")) ? true : false;
        let hasLatAndLong = (hasCenter) ? Object.keys(properties.center).filter(c => c === 'latitude' || c === 'longitude') : null;
        let center = (hasLatAndLong !== null && hasLatAndLong.length === 2) ? true : false;
        return (center) ? properties.center : null;
    };

    setCenter = (latitude, longitude) => {
        if (!this.logged) return null;
        if ( isNaN(latitude) || isNaN(longitude) ) return null;
        this.props.setCenter({ latitude: latitude, longitude: longitude });
    };

    getZoom = () => {
        if (!this.logged) return null;
        let properties = (this.props.properties !== null) ? Object.assign({}, this.props.properties) : null;
        let hasZoom = (properties !== null && Object.keys(properties).filter(p => p === "zoom")) ? true : false;
        return (hasZoom !== null) ? properties.zoom : null;
    };

    setZoom = (zoom) => {
        if (!this.logged) return null;
        let checkedZoom;
        if (Array.isArray(zoom)) checkedZoom = zoom;
        else checkedZoom = [zoom];
        this.props.setZoom(checkedZoom);
    };

    render() { return null };
}

function selectStateApp(state) {
    return {
        userLogged: state.auth.userLogged,
        layers: state.maplayers.mapGLlayers,
        properties: state.map.properties,
    };
}

export default compose(withTranslation('maps'), connect(
    selectStateApp,
    dispatch => ({
        changeLayerVisibility: (layer) => LayerActions.changeLayerVisibility(layer)(dispatch),
        setCenter: (center) => MapActions.setCenter(center)(dispatch),
        setZoom: (zoom) => MapActions.setZoom(zoom)(dispatch),
        setProperties: (properties) => MapActions.setProperties(properties)(dispatch),
    })
))(APIService);
Javier
  • 1,975
  • 3
  • 24
  • 46

1 Answers1

1

If I understand correctly, you basically want to render your React component into the DOM, right? If so, here's the official React documentation just for that: Rendering Elements.

Alternatively, if you want to use your React Component just like any other HTML Element that can be used anywhere, you can consider wrapping your React Component inside a Web Component. The official React documentation also has that: Using React in your Web Components. It would look something like this for your component:

class APIServiceElement extends HTMLElement {
  connectedCallback() {
    const mountPoint = document.createElement('div');
    this.attachShadow({ mode: 'open' }).appendChild(mountPoint);

    ReactDOM.render(<APIService />, mountPoint);
  }
}
customElements.define('api-service', APIServiceElement);

And after that you can use <api-service></api-service> in your index.html file.

Huy
  • 261
  • 2
  • 4
  • Thank you so much. But I have two questions. 1 - where I add that class?? 2 - how call functions from index.html?? – Javier Jul 26 '19 at 10:07
  • 1
    @Javier From what I'm seeing, it's likely to be hard for you if I'm simply explaining here without some demo. If you don't mind waiting, I'll put up a simple demo and notify you later when I'm done. – Huy Jul 26 '19 at 10:22
  • here is the code that you say. https://github.com/jadelmag/APIService/tree/master – Javier Jul 26 '19 at 11:21
  • thank you so much!! But I explaned wrog.. because APIService is a component that consumes libraries. And I know how explain it now... I need to create a bundle using webpack. – Javier Jul 27 '19 at 16:31