import React, { useState } from "react"; import { json } from "@remix-run/node"; import { useLoaderData, useActionData, useSubmit } from "@remix-run/react"; import { Page, Layout, Card, BlockStack, Text, Badge, InlineStack, Image, Divider, Button, Modal, TextField, Box, Banner, } from "@shopify/polaris"; import { TitleBar } from "@shopify/app-bridge-react"; import data4autosLogo from "../assets/data4autos_logo.png"; // ensure this exists import turn14DistributorLogo from "../assets/turn14-logo.png"; import { authenticate } from "../shopify.server"; import { Form } from "@remix-run/react"; /* =========================== LOADER: check subscription =========================== */ export const loader = async ({ request }) => { const { admin } = await authenticate.admin(request); const resp = await admin.graphql(` query { currentAppInstallation { activeSubscriptions { id status trialDays createdAt currentPeriodEnd } } } `); const result = await resp.json(); const subscription = result?.data?.currentAppInstallation?.activeSubscriptions?.[0] || null; const { session } = await authenticate.admin(request); const shop = session.shop; if (!subscription) { return json({ redirectToBilling: true, subscription: null, shop }); } if (subscription.status !== "ACTIVE" && subscription.status !== "TRIAL") { return json({ redirectToBilling: true, subscription, shop }); } return json({ redirectToBilling: false, subscription, shop }); }; /* =========================== ACTION: create subscription =========================== */ export const action = async ({ request }) => { const { admin } = await authenticate.admin(request); const createRes = await admin.graphql(` mutation { appSubscriptionCreate( name: "Pro Plan" returnUrl: "https://your-app.com/after-billing" lineItems: [ { plan: { appRecurringPricingDetails: { price: { amount: 79, currencyCode: USD } interval: EVERY_30_DAYS } } } ] trialDays: 7 test: true ) { confirmationUrl appSubscription { id status trialDays } userErrors { field message } } } `); const data = await createRes.json(); const url = data?.data?.appSubscriptionCreate?.confirmationUrl; const userErrors = data?.data?.appSubscriptionCreate?.userErrors || []; const topLevelErrors = data?.errors || []; if (!url || userErrors.length || topLevelErrors.length) { return json( { errors: [ "Failed to create subscription.", ...userErrors.map((e) => e.message), ...topLevelErrors.map((e) => e.message || String(e)), ], }, { status: 400 } ); } return json({ confirmationUrl: url }); }; /* =========================== PAGE =========================== */ export default function Index() { const actionData = useActionData(); const loaderData = useLoaderData(); const submit = useSubmit(); const [activeModal, setActiveModal] = useState(false); const subscription = loaderData?.subscription; const shop = loaderData?.shop; const shopDomain = (shop || "").split(".")[0]; const items = [ { icon: "⚙️", text: "Manage API settings", link: `https://admin.shopify.com/store/${shopDomain}/apps/d4a-turn14/app/settings`, }, { icon: "🏷️", text: "Browse and import available brands", link: `https://admin.shopify.com/store/${shopDomain}/apps/d4a-turn14/app/brands`, }, { icon: "📦", text: "Sync brand collections to Shopify", link: `https://admin.shopify.com/store/${shopDomain}/apps/d4a-turn14/app/managebrand`, }, { icon: "🔐", text: "Handle secure Turn14 login credentials", link: `https://admin.shopify.com/store/${shopDomain}/apps/d4a-turn14/app/help`, }, ]; const openModal = () => setActiveModal(true); const closeModal = () => setActiveModal(false); const hasConfirmationUrl = Boolean(actionData?.confirmationUrl); const errors = actionData?.errors || []; return ( Welcome to your Turn14 Dashboard Data4Autos Logo Turn14 Distributors Logo 🚀 Data4Autos Turn14 Integration gives you the power to sync product brands, manage collections, and automate catalog setup directly from Turn14 to your Shopify store. Use the left sidebar to: {items.map((item, index) => ( {item.icon} {/* Real anchor to avoid double-URL issues */} {item.text} ))} {/* Status: Connected Shopify × Turn14 */} Need help? Contact us at{" "} support@data4autos.com {/* =========================== MODAL =========================== */} { const form = document.getElementById("billing-form"); submit(form); }, } } secondaryActions={[{ content: "Close", onAction: closeModal }]} >
{errors.length > 0 && (
    {errors.map((e, i) => (
  • {e}
  • ))}
)} {!hasConfirmationUrl && ( <> )} {hasConfirmationUrl && ( Click the button below to open Shopify’s billing confirmation in a new tab. If it doesn’t open, copy the link and open it manually. {/* Polaris Button-as-link opens absolute URL in a new tab */} {/* Plain anchor fallback (optional) */} {actionData.confirmationUrl} )}
); }