polaris icon indicating copy to clipboard operation
polaris copied to clipboard

How to redirect from inside a Polaris component in an embedded app?

Open paulgrieselhuber opened this issue 7 years ago • 11 comments

I am attempting to redirect customers of my app to the confirmation_url to accept a billing charge in a Shopify Polaris embedded app. Per the billing API documentation, once we create the charge we need to redirect merchants to the charge's confirmation_url.

Neither a standard server-side nor client side redirect work due to CORS, so I pursued the App Provider documentation which describes creating redirects at the absolute top level of the app where we pass the API key to the AppProvider, and then creating a redirect via const redirect = Redirect.create(... at the same time.

Is the idea here that we would then pass redirect down as a prop down the component chain to every component that might need to make a redirect? The reason I ask is that it doesn't seem like the method implied for creating redirects would enable creating them at the component level.

paulgrieselhuber avatar Jan 31 '19 13:01 paulgrieselhuber

Hi @paulisloud! Sorry for our delay in getting to your question. Were you able to find the info you needed in the Redirect documentation linked from the app provider page you mentioned?

dpersing avatar Feb 07 '19 16:02 dpersing

Thanks for getting back to me @dpersing, but unfortunately that documentation also asks you to create the redirect instance using the shopOrigin and apiKey, meaning we would need to:

  • createApp while in a component, which is inside the app already, and pass shopOrigin and apiKey all the way down to the component, or
  • should we instead create redirect at the top level of the app and attach it to state and make it available to components around via Redux / Context?

Or, is there some other way to instantiate the redirect while already in a component, say, a Card on a Page and perhaps I am making this harder than it needs to be?

paulgrieselhuber avatar Feb 07 '19 19:02 paulgrieselhuber

@paulisloud not sure if this helps you but I just started playing around with this library and got a redirect working using something like this

import { Redirect } from "@shopify/app-bridge/actions";

class ProductForm extends React.Component {
  static contextTypes = {
    polaris: PropTypes.object
  };

  handleSubmit = () => {
     // after form is submitted redirect to admin product show page
     const redirect = Redirect.create(this.context.polaris.appBridge);

    return redirect.dispatch(
      Redirect.Action.ADMIN_PATH,
      `/products/${product.id}`
    );
  }

this is in a component that is a child of app provider

<AppProvider apiKey={apiKey}>
    <ProductForm />
</AppProvider>

WesKetch avatar Mar 17 '19 15:03 WesKetch

Hi @WesKetch! I followed the guidance on https://polaris.shopify.com/components/structure/app-provider. Redirect should work after button click, no errors appears but no redirect also. I have attached my source code briefly. Could you tell me why it doesn't works for me?

import * as PropTypes from 'prop-types';
import { Redirect } from "@shopify/app-bridge/actions";

class Help extends Component {
	static contextTypes = {
		polaris: PropTypes.object
	};

	gotoInstallPage = () => {
		const redirect = Redirect.create(this.context.polaris.appBridge);
		return redirect.dispatch(Redirect.Action.APP,'/install');
	}

	render() {
		return(
			<Link monochrome onClick={() => this.gotoInstallPage()}>troubleshooting</Link>
		)
	}
}

u00t avatar Apr 09 '19 09:04 u00t

Edit: Turned out to be my own fault.

marguskoiduste avatar Apr 17 '19 13:04 marguskoiduste

Are you sure your "App URL" matches the URL where the app is actually loaded from? I had exactly the same behavior, but it turned out there was a mismatch between the "App URL" and the origin of the postMessage the AppBridge was sending. Please see this issue: #1332

marguskoiduste avatar Apr 17 '19 17:04 marguskoiduste

Hi @marguskoiduste! Yes, I have checked App URL and the origin of the postMessage the AppBridge. Everything seems okay but redirect is not working. Currently I am using react-redux in a polaris react. I think both of libraries doesn't compatible each other.

u00t avatar Apr 20 '19 07:04 u00t

For me the problem was that I had "https://" preceding the app URL. If your including it, try taking it out (ex: "store.myshopify.com" instead of "https://store.myshopify.com") and see if that helps?

cgenevier avatar Apr 20 '19 18:04 cgenevier

This issue has been inactive for 180 days and labeled with Icebox. It will be closed in 7 days if there is no further activity.

ghost avatar Oct 17 '19 19:10 ghost

How would you do this within a FC? Where are the docs in how to access the App Bridge context?

https://shopify.dev/tools/app-bridge/react-components/provider#accessing-the-app-bridge-client-directly

Seems like you can do this for example:

import { useAppBridge } from '@shopify/app-bridge-react'
import { MediaCard } from '@shopify/polaris'
import { Redirect } from '@shopify/app-bridge/actions'

export const Component: React.FC<IComponentProps> = () => {
  const app = useAppBridge()
  const redirect = Redirect.create(app)

  const params = new URLSearchParams({
    resource_name: 'Product',
    edit: editFields.join(',')
  })

  redirect.dispatch(Redirect.Action.ADMIN_PATH, {
    path: `/bulk?${params.toString()}`,
    newContext: true
  })
}

coler-j avatar Jan 09 '21 13:01 coler-j

I'm using an older version of app-bridge-react that doesn't have useAppBridge, so I used useContext for now.

Ref: https://shopify.dev/tools/app-bridge/react-components/provider

import React, { useContext } from 'react';
import { Context } from '@shopify/app-bridge-react';
import { Redirect } from '@shopify/app-bridge/actions';

const YourPage: React.FC = () => {
  const app = useContext(Context);
  const redirect = Redirect.create(app);
  ...
  redirect.dispatch(Redirect.Action.APP, '/otherpage?somequeryparam=123');

ggtaylor avatar Jun 11 '21 23:06 ggtaylor