Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Shopify App: Polaris React to Polaris WC. Migrate the Custom App UI f…
…or creating, editing & deleting QR codes.
  • Loading branch information
byrichardpowell committed Sep 30, 2025
commit bead872ca8c2d95a685b0682e74bdffec3955fc5
176 changes: 87 additions & 89 deletions app/routes/app._index.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import { useLoaderData, Link, useNavigate } from "react-router";
import { authenticate } from "../shopify.server";
import { useLoaderData, Link } from "react-router";
import { boundary } from "@shopify/shopify-app-react-router/server";
import {
Card,
EmptyState,
Layout,
Page,
IndexTable,
Thumbnail,
Text,
Icon,
InlineStack,
} from "@shopify/polaris";

import { authenticate } from "../shopify.server";
import { getQRCodes } from "../models/QRCode.server";
import { AlertDiamondIcon, ImageIcon } from "@shopify/polaris-icons";

// [START loader]
export async function loader({ request }) {
Expand All @@ -28,17 +16,35 @@ export async function loader({ request }) {
// [END loader]

// [START empty]
const EmptyQRCodeState = ({ onAction }) => (
<EmptyState
heading="Create unique QR codes for your product"
action={{
content: "Create QR code",
onAction,
}}
image="https://cdn.shopify.com/s/files/1/0262/4071/2726/files/emptystate-files.png"
>
<p>Allow customers to scan codes and buy products using their phones.</p>
</EmptyState>
const EmptyQRCodeState = () => (
<s-section accessibilityLabel="Empty state section">
<s-grid gap="base" justifyItems="center" paddingBlock="large-400">
<s-box maxInlineSize="200px" maxBlockSize="200px">
<s-image
aspectRatio="1/0.5"
src="https://cdn.shopify.com/s/files/1/0262/4071/2726/files/emptystate-files.png"
alt="A stylized graphic of a document"
/>
</s-box>
<s-grid justifyItems="center" maxBlockSize="450px" maxInlineSize="450px">
<s-heading>Create unique QR codes for your products</s-heading>
<s-paragraph>
Allow customers to scan codes and buy products using their phones.
</s-paragraph>
<s-stack
gap="small-200"
justifyContent="center"
padding="base"
paddingBlockEnd="none"
direction="inline"
>
<s-button href="/app/qrcodes/new" variant="primary">
Create QR code
</s-button>
</s-stack>
</s-grid>
</s-grid>
</s-section>
);
// [END empty]

Expand All @@ -50,89 +56,81 @@ function truncate(str, { length = 25 } = {}) {

// [START table]
const QRTable = ({ qrCodes }) => (
<IndexTable
resourceName={{
singular: "QR code",
plural: "QR codes",
}}
itemCount={qrCodes.length}
headings={[
{ title: "Thumbnail", hidden: true },
{ title: "Title" },
{ title: "Product" },
{ title: "Date created" },
{ title: "Scans" },
]}
selectable={false}
>
{qrCodes.map((qrCode) => (
<QRTableRow key={qrCode.id} qrCode={qrCode} />
))}
</IndexTable>
<s-section padding="none" accessibilityLabel="QRCode table">
<s-table>
<s-table-header-row>
<s-table-header listSlot="primary">Title</s-table-header>
<s-table-header>Product</s-table-header>
<s-table-header>Date created</s-table-header>
<s-table-header>Scans</s-table-header>
</s-table-header-row>
<s-table-body>
{qrCodes.map((qrCode) => (
<QRTableRow key={qrCode.id} qrCode={qrCode} />
))}
</s-table-body>
</s-table>
</s-section>
);
// [END table]

// [START row]
const QRTableRow = ({ qrCode }) => (
<IndexTable.Row id={qrCode.id} position={qrCode.id}>
<IndexTable.Cell>
<Thumbnail
source={qrCode.productImage || ImageIcon}
alt={qrCode.productTitle}
size="small"
/>
</IndexTable.Cell>
<IndexTable.Cell>
<Link to={`qrcodes/${qrCode.id}`}>{truncate(qrCode.title)}</Link>
</IndexTable.Cell>
<IndexTable.Cell>
<s-table-row id={qrCode.id} position={qrCode.id}>
<s-table-cell>
<s-stack direction="inline" gap="small" alignItems="center">
<s-clickable
href={`/app/qrcodes/${qrCode.id}`}
accessibilityLabel={`Go to the product page for ${qrCode.productTitle}`}
border="base"
borderRadius="base"
overflow="hidden"
inlineSize="20px"
blockSize="20px"
>
{qrCode.productImage ? (
<s-image objectFit="cover" src={qrCode.productImage}></s-image>
) : (
<s-icon size="large" type="image" />
)}
</s-clickable>
<s-link href={`/app/qrcodes/${qrCode.id}`}>
{truncate(qrCode.title)}
</s-link>
</s-stack>
</s-table-cell>
<s-table-cell>
{/* [START deleted] */}
{qrCode.productDeleted ? (
<InlineStack align="start" gap="200">
<span style={{ width: "20px" }}>
<Icon source={AlertDiamondIcon} tone="critical" />
</span>
<Text tone="critical" as="span">
product has been deleted
</Text>
</InlineStack>
<s-badge icon="alert-diamond" tone="critical">
Product has been deleted
</s-badge>
) : (
truncate(qrCode.productTitle)
)}
{/* [END deleted] */}
</IndexTable.Cell>
<IndexTable.Cell>
{new Date(qrCode.createdAt).toDateString()}
</IndexTable.Cell>
<IndexTable.Cell>{qrCode.scans}</IndexTable.Cell>
</IndexTable.Row>
</s-table-cell>
<s-table-cell>{new Date(qrCode.createdAt).toDateString()}</s-table-cell>
<s-table-cell>{qrCode.scans}</s-table-cell>
</s-table-row>
);
// [END row]

export default function Index() {
const { qrCodes } = useLoaderData();
const navigate = useNavigate();

// [START page]
return (
<Page>
<ui-title-bar title="QR codes">
<button variant="primary" onClick={() => navigate("/app/qrcodes/new")}>
Create QR code
</button>
</ui-title-bar>
<Layout>
<Layout.Section>
<Card padding="0">
{qrCodes.length === 0 ? (
<EmptyQRCodeState onAction={() => navigate("qrcodes/new")} />
) : (
<QRTable qrCodes={qrCodes} />
)}
</Card>
</Layout.Section>
</Layout>
</Page>
<s-page heading="QR codes">
<s-link slot="secondary-actions" href="/app/qrcodes/new">
Create QR code
</s-link>
{qrCodes.length === 0 ? (
<EmptyQRCodeState />
) : (
<QRTable qrCodes={qrCodes} />
)}
</s-page>
);
// [END page]
}
Expand Down
Loading
⌂ 换网址