How to access the action method data in the remix?
How to access the action method data in the remix?
I made a GraphQL call from the action method and got the URL in the response. In the action, I want to redirect that URL but I am not able to get the window object in the action method that's why I am trying to use the useActionData method. But the problem is I am not able to get the URL on the first click. I got the URL after second time when I had to click the button. Below is my code.
export async function action({request}){
const { admin, session } = await authenticate.admin(request);
const shop = await session.shop;
const graphql = await admin.graphql;
const data = Object.fromEntries(await request.formData())
const plan_50kg = await db.plans.findUnique({ where: { id: 4 } });
const shopName = await shop.split('.')[0];
const appName = process.env.APP_NAME;
const args_50kg = {
"name":"Super Duper Capped Pricing Plan",
"return_url": "https://admin.shopify.com/store/"+shopName+"/apps/"+appName+"/app",
"terms": "$"+plan_50kg?.usage_charges+" USD every 30 days",
"cappedAmount": {
"amount": ""+plan_50kg?.capped_amount+"",
"currencyCode": "USD"
},
"price": {
"amount": ""+plan_50kg?.price+"",
"currencyCode": "USD"
}
}
const confirmation_url_50kg = await createSubscription(args_50kg, graphql);
return json({
confirmation_url: confirmation_url_50kg,
});
}
const Pricing = () => {
const data = useActionData();
<Form method="post">
<input type="hidden" value="1" name="plan_id"></input>
<input type="hidden" name="plan_id"></input>
<Button submit primary onClick={()=>{
console.log("data", data);
}}>Unsubscribe</Button>
</Form>
}
To redirect from inside your action, use the redirect method.
import { redirect } from "@remix-run/node";
export async function action({request}){
// ...
const confirmation_url_50kg = await createSubscription(args_50kg, graphql);
return redirect(confirmation_url_50kg);
}
https://remix.run/docs/en/main/utils/redirect
December 30, 2023
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
December 30, 2023