Dev Helpers
← Back

Discount only multi-unit order lines

Discount only multi-unit order lines. Render the transformed entries in a reusable React component.

Open this example in the interactive library

Live preview

Source code

// CollectionView.tsx
export default function CollectionView() {
  const rows = [
    { item: "tea", units: 2, cents: 350 },
    { item: "mug", units: 1, cents: 900 },
    { item: "tea", units: 3, cents: 325 },
    { item: "tin", units: 2, cents: 500 },
  ];
  const result = rows.map((r) => ({
    ...r,
    cents: r.units >= 2 ? Math.round(r.cents * 0.9) : r.cents,
  }));
  const entries = Object.entries(
    typeof result === "object" && result !== null ? result : { value: result },
  );
  return (
    <section>
      <h2>{"Discount only multi-unit order lines"}</h2>
      {entries.length ? (
        <dl>
          {entries.map(([key, value]) => (
            <div key={key}>
              <dt>{key}</dt>
              <dd>
                {typeof value === "string" ? value : JSON.stringify(value)}
              </dd>
            </div>
          ))}
        </dl>
      ) : (
        <p>No matching entries.</p>
      )}
    </section>
  );
}