Issue in Resource picker "App Bridge + Polaris + React + Rails"

Solved
s-sana
Shopify Partner
10 1 0

I've created app using rails shopify_app gem + webpacker + polaris

I'm new to Shopify App Bridge. 

I'm getting all the values and no error in console regarding set up but when i click on button value gets true but ResourcePicker doesn't open.

I've reviewed the shopify help center content , unable to figure out whats the issue.

Have a look on below code, Please help me out what i am doing wrong.

 

index.html.erb

<%= javascript_pack_tag 'vpicker' %>
<%= content_tag :div,
    id: "picker",
    data: {
      products: @products,
      shop_session: @shop_session
      api_key: ENV['SHOPIFY_API_KEY']
    }.to_json do %>
  <% end %>

 

vpicker.jsx

import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import {AppProvider, Button} from '@shopify/polaris';
import {Provider as AppBridgeProvider, ResourcePicker} from '@shopify/app-bridge-react';

class Vpicker extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      resourcePickerOpen: false,
    }
  }

  togglePicker = () => {
    console.log("resourcePickerOpen", this.state.resourcePickerOpen);
    this.setState(({ resourcePickerOpen }) => {
      return { resourcePickerOpen: !resourcePickerOpen };
    });
  };

  handleSelection = (resources) => {
    this.setState({ resourcePickerOpen: false })
    console.log(resources)
  };

  render() {
    console.log("Props Data", this.props.shop_session.domain);
    const config = { apiKey: this.props.api_key, shopOrigin: this.props.shop_session.domain };
    console.log("Picker value", this.state.resourcePickerOpen);

    return (
        <AppProvider>
          <AppBridgeProvider config={config}>
            <ResourcePicker
              resourceType="Product"
              open={this.state.resourcePickerOpen}
              onSelection={(resources) => this.handleSelection(resources)}
              onCancel={this.togglePicker}
            />
            <Button primary onClick={() => this.setState({ resourcePickerOpen: true })}>Select Product</Button>
          </AppBridgeProvider>
        </AppProvider>
    );
  }
}

document.addEventListener('DOMContentLoaded', () => {
  const node = document.getElementById('picker')
  const data = JSON.parse(node.getAttribute('data'))
  ReactDOM.render(<Vpicker {...data} />, node);
})

Any help is appreciated.

Thank you

 

0 Likes
hannachen
Shopify Staff
Shopify Staff
49 8 14

👋Hey Ssana, thanks for using Shopify App Bridge in your project! Unfortunately at this time, `shopify_app` gem is not compatible with Shopify App Bridge out of box, and may require some additional configuration to get it working, which is what I suspect might be happening here. Could you kindly help me confirm this and check if your page's view contains the old EASDK app.js like below?

 

<script src="https://cdn.shopify.com/s/assets/external/app.js?<%= Time.now.strftime('%Y%m%d%H') %>"></script>
s-sana
Shopify Partner
10 1 0

Hi @hannachen, Thanks for replying, I wasn't aware about its compatibility but yes, the page view contains the JS in embedded_app.html.erb file

 

<script src="https://cdn.shopify.com/s/assets/external/app.js?<%= Time.now.strftime('%Y%m%d%H') %>"></script>

Please help me out i want to manage the product variant picker in my app and i studied shopify app bridge is the only way to make it work as it is deprecated from the polaris. 

 

Please guide me.

 

Thanks.

0 Likes
hannachen
Shopify Staff
Shopify Staff
49 8 14

Thanks for checking, this looks to be promising. If this is a new app that doesn't contain any EASDK code, simply remove that line from `embedded_app.html.erb`. If the AppBridgeProvider is set up correctly, your app should behave as described in the Shopify App Bridge tutorials. Could you please give that a try?

s-sana
Shopify Partner
10 1 0

This is an accepted solution.

Thanks for guiding, its working fine now.

0 Likes
hannachen
Shopify Staff
Shopify Staff
49 8 14

Fantastic! It's my pleasure, thanks for confirming.

arhumali07
Tourist
7 0 0
Click to expand...
i am facing same issue. can you please tell how did you solve it


0 Likes