Merge pull request 'feat: show product photos and let staff upload them' (#33) from feat/product-images into main
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Reviewed-on: #33
This commit is contained in:
commit
4e81ee7774
|
|
@ -27,7 +27,7 @@ docker run --rm -p 8080:80 manicandles-frontend:uat
|
|||
|
||||
After the one-time [server setup](docs/STATIC-DEPLOYMENT.md), main-branch Drone pushes upload the checked static build over SSH and activate it through the existing Nginx HTTPS site. Readiness failure restores the previous release. Drone requires the `manicandles_deploy_key` secret. Feature branches run validation without uploading or activating a release.
|
||||
|
||||
Storefront list returns only id/name/slug; details return prices/attributes/groups. The API has no product-media fields, so catalog cards use a generic branded placeholder rather than assigning local photos to unknown records. Existing marketing imagery remains intact.
|
||||
Storefront list returns id/name/slug and the main photo; details return prices/attributes/groups and all photos. Staff upload up to 8 JPEG/PNG/WebP photos per product (2 MiB each) under Admin catalog; products without photos show a branded placeholder. Existing marketing imagery remains intact.
|
||||
|
||||
Addresses, cart/checkout, orders, catalog, access, stock, coupons and pricing tools are available after sign-in. Customers can create their own account at `/workspace` (requires the backend `/auth/register` endpoint). There is no contact-message endpoint; simulated success was replaced with an availability notice. Newsletter signup awaits consent/messaging flows.
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ location /api/ {
|
|||
proxy_hide_header Access-Control-Allow-Origin;
|
||||
proxy_hide_header Access-Control-Allow-Credentials;
|
||||
|
||||
location ~ "^/api/v1/(?:supplier-documents|b2b/artworks)/[0-9a-fA-F-]{36}/versions$" {
|
||||
location ~ "^/api/v1/(?:(?:supplier-documents|b2b/artworks)/[0-9a-fA-F-]{36}/versions|products/[0-9a-fA-F-]{36}/images)$" {
|
||||
client_max_body_size 3m;
|
||||
proxy_pass https://manicandles-backend.midastix.com;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ server {
|
|||
proxy_hide_header Access-Control-Allow-Origin;
|
||||
proxy_hide_header Access-Control-Allow-Credentials;
|
||||
|
||||
location ~ "^/api/v1/(?:supplier-documents|b2b/artworks)/[0-9a-fA-F-]{36}/versions$" {
|
||||
location ~ "^/api/v1/(?:(?:supplier-documents|b2b/artworks)/[0-9a-fA-F-]{36}/versions|products/[0-9a-fA-F-]{36}/images)$" {
|
||||
client_max_body_size 3m;
|
||||
proxy_pass https://manicandles-backend.midastix.com;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import Alert from "@mui/material/Alert";
|
|||
import Button from "@mui/material/Button";
|
||||
import Skeleton from "@mui/material/Skeleton";
|
||||
import { Flame } from "lucide-react";
|
||||
import { listProducts } from "@/lib/api";
|
||||
import { listProducts, productImageUrl } from "@/lib/api";
|
||||
import { usePlatform } from "@/lib/platform";
|
||||
import { ApiNotice } from "./ApiNotice";
|
||||
import "@/pages/products.css";
|
||||
|
|
@ -75,11 +75,21 @@ export function Catalog({
|
|||
<article className="prod-card" key={product.id}>
|
||||
<Link
|
||||
href={`/products/${product.id}`}
|
||||
className="prod-img-wrap catalog-placeholder"
|
||||
className={`prod-img-wrap${product.images[0] ? "" : " catalog-placeholder"}`}
|
||||
aria-label={`View ${product.name}`}
|
||||
>
|
||||
<Flame size={48} aria-hidden="true" />
|
||||
<span>Mani Candles</span>
|
||||
{product.images[0] ? (
|
||||
<img
|
||||
src={productImageUrl(organization, product.id, product.images[0].id)}
|
||||
alt=""
|
||||
loading="lazy"
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
<Flame size={48} aria-hidden="true" />
|
||||
<span>Mani Candles</span>
|
||||
</>
|
||||
)}
|
||||
</Link>
|
||||
<div className="prod-info">
|
||||
<h3 className="prod-name">
|
||||
|
|
|
|||
|
|
@ -5,7 +5,15 @@ const productSummarySchema = z.object({
|
|||
id: z.string().uuid(),
|
||||
name: z.string(),
|
||||
slug: z.string(),
|
||||
images: z.array(z.object({ id: z.string().uuid() })).default([]),
|
||||
});
|
||||
export function productImageUrl(
|
||||
organization: string,
|
||||
productId: string,
|
||||
imageId: string,
|
||||
) {
|
||||
return `/api/v1/storefront/${organization}/products/${productId}/images/${imageId}`;
|
||||
}
|
||||
export const productSchema = productSummarySchema.extend({
|
||||
description: z.string(),
|
||||
variants: z.array(
|
||||
|
|
|
|||
|
|
@ -154,8 +154,26 @@ export const adminProductSummary = productInput
|
|||
.pick({ name: true, slug: true })
|
||||
.extend({ id: uuid, status: z.enum(["DRAFT", "PUBLISHED", "ARCHIVED"]) })
|
||||
.strip();
|
||||
export const productImage = z
|
||||
.object({
|
||||
id: uuid,
|
||||
mimeType: z.enum(["image/jpeg", "image/png", "image/webp"]),
|
||||
size: z.number().int().positive(),
|
||||
})
|
||||
.strip();
|
||||
export const productImageAccept = "image/jpeg,image/png,image/webp";
|
||||
export function prepareProductImage(file: FormDataEntryValue | null) {
|
||||
if (!(file instanceof File) || !file.size) throw new Error("Choose a JPEG, PNG or WebP photo.");
|
||||
if (file.size > 2 * 1024 * 1024) throw new Error("Choose a photo up to 2 MiB.");
|
||||
if (!productImageAccept.split(",").includes(file.type))
|
||||
throw new Error("Use a JPEG, PNG or WebP photo.");
|
||||
const body = new FormData();
|
||||
body.append("file", file);
|
||||
return body;
|
||||
}
|
||||
export const adminProduct = adminProductSummary.extend({
|
||||
description: z.string(),
|
||||
images: z.array(productImage).default([]),
|
||||
variants: z.array(variant),
|
||||
groups: z.array(z.object({ group })),
|
||||
});
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ import {
|
|||
group,
|
||||
groupInput,
|
||||
money,
|
||||
prepareProductImage,
|
||||
productImage,
|
||||
productImageAccept,
|
||||
type AdminProduct,
|
||||
uuid,
|
||||
} from "@/lib/commerce";
|
||||
|
|
@ -31,6 +34,8 @@ import {
|
|||
} from "@/components/WorkspaceTools";
|
||||
import { RecordSelection } from "@/components/RecordSelection";
|
||||
import { ConfirmAction } from "@/components/ConfirmAction";
|
||||
import { productImageUrl } from "@/lib/api";
|
||||
import "./products.css";
|
||||
function ProductForm({
|
||||
item,
|
||||
onSave,
|
||||
|
|
@ -323,8 +328,10 @@ function Products() {
|
|||
);
|
||||
}
|
||||
function ProductEditor({ id }: { id: string }) {
|
||||
const { request, can } = usePlatform();
|
||||
const { request, can, organization } = usePlatform();
|
||||
const query = usePrivateQuery(`/products/${id}`, adminProduct);
|
||||
const editable = (item: AdminProduct) =>
|
||||
can("catalog.manage") && item.status !== "ARCHIVED";
|
||||
const [editingVariant, setEditingVariant] = useState<
|
||||
z.infer<typeof variant> | undefined
|
||||
>();
|
||||
|
|
@ -373,6 +380,53 @@ function ProductEditor({ id }: { id: string }) {
|
|||
</>
|
||||
)}
|
||||
</Panel>
|
||||
<Panel title="Photos">
|
||||
{!item.images.length && <p>No photos yet. The first photo is shown on the catalog card.</p>}
|
||||
{item.status !== "PUBLISHED" && item.images.length > 0 && (
|
||||
<Alert severity="info">Previews appear after the product is published.</Alert>
|
||||
)}
|
||||
<div className="product-gallery">
|
||||
{item.images.map((image, index) => (
|
||||
<figure key={image.id}>
|
||||
{item.status === "PUBLISHED" ? (
|
||||
<img src={productImageUrl(organization, id, image.id)} alt={`${item.name}, photo ${index + 1}`} />
|
||||
) : (
|
||||
<p>Photo {index + 1}</p>
|
||||
)}
|
||||
<figcaption>
|
||||
{index === 0 ? "Main photo · " : ""}
|
||||
{image.mimeType.replace("image/", "").toUpperCase()}, {Math.ceil(image.size / 1024)} KB
|
||||
{editable(item) && (
|
||||
<ConfirmAction
|
||||
label="Remove"
|
||||
message={`Remove photo ${index + 1}? This cannot be undone.`}
|
||||
onConfirm={async () => {
|
||||
await request(`/products/${id}/images/${image.id}`, z.unknown(), { method: "DELETE" });
|
||||
await query.refetch();
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</figcaption>
|
||||
</figure>
|
||||
))}
|
||||
</div>
|
||||
{editable(item) && item.images.length < 8 && (
|
||||
<ActionForm
|
||||
label="Upload photo"
|
||||
read={(data) => prepareProductImage(data.get("file"))}
|
||||
submit={async (body) => {
|
||||
try {
|
||||
await request(`/products/${id}/images`, productImage, { method: "POST", body });
|
||||
} finally {
|
||||
await query.refetch();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<label htmlFor={`photo-${id}`}>Photo (JPEG, PNG or WebP, up to 2 MiB)</label>
|
||||
<input id={`photo-${id}`} name="file" type="file" accept={productImageAccept} required />
|
||||
</ActionForm>
|
||||
)}
|
||||
</Panel>
|
||||
<Panel title="Variants">
|
||||
<Table
|
||||
head={["Variant", "SKU", "Price", "Active", "ID", "Action"]}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import Alert from "@mui/material/Alert";
|
|||
import Button from "@mui/material/Button";
|
||||
import Skeleton from "@mui/material/Skeleton";
|
||||
import Chip from "@mui/material/Chip";
|
||||
import { getProduct } from "@/lib/api";
|
||||
import { getProduct, productImageUrl } from "@/lib/api";
|
||||
import { usePlatform } from "@/lib/platform";
|
||||
import { ApiNotice } from "@/components/ApiNotice";
|
||||
import "./products.css";
|
||||
|
|
@ -43,6 +43,18 @@ export default function ProductPage({ id }: { id: string }) {
|
|||
</>
|
||||
) : (
|
||||
<div className="product-detail">
|
||||
{query.data.images.length > 0 && (
|
||||
<div className="product-gallery">
|
||||
{query.data.images.map((image, index) => (
|
||||
<img
|
||||
key={image.id}
|
||||
src={productImageUrl(organization, id, image.id)}
|
||||
alt={`${query.data.name}, photo ${index + 1}`}
|
||||
loading={index ? "lazy" : "eager"}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<h1>{query.data.name}</h1>
|
||||
<p className="product-description">
|
||||
{query.data.description ||
|
||||
|
|
|
|||
|
|
@ -189,3 +189,7 @@
|
|||
.variant-list dl>div { display:flex; flex-wrap:wrap; gap:.5rem; font-size:.9rem; }
|
||||
.variant-list dt { font-weight:600; }
|
||||
@media(max-width:480px) { .variant-list li { flex-direction:column; } .products-body { padding:3rem .5rem; } .catalog-search { flex-direction:column; } }
|
||||
|
||||
.product-gallery { display:grid; grid-template-columns:repeat(auto-fill, minmax(200px, 1fr)); gap:1rem; margin:1.5rem 0; }
|
||||
.product-gallery img { width:100%; aspect-ratio:4 / 5; object-fit:cover; border:1px solid var(--brand-border); }
|
||||
.product-gallery img:first-child { grid-column:1 / -1; max-height:520px; }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { productImageUrl, productSchema } from "../src/lib/api.ts";
|
||||
import { adminProduct, prepareProductImage } from "../src/lib/commerce.ts";
|
||||
|
||||
const id = "d66c798a-f764-4b88-8d13-ace328ff1010";
|
||||
const product = { id, name: "Rose", slug: "rose", description: "", variants: [], groups: [] };
|
||||
|
||||
test("product images default to none and keep their storefront order", () => {
|
||||
assert.deepEqual(productSchema.parse(product).images, []);
|
||||
assert.deepEqual(productSchema.parse({ ...product, images: [{ id }] }).images, [{ id }]);
|
||||
assert.throws(() => productSchema.parse({ ...product, images: [{ id: "../x" }] }));
|
||||
assert.equal(productImageUrl(id, id, id), `/api/v1/storefront/${id}/products/${id}/images/${id}`);
|
||||
const admin = adminProduct.parse({ ...product, status: "DRAFT", images: [{ id, mimeType: "image/webp", size: 10 }] });
|
||||
assert.equal(admin.images[0].mimeType, "image/webp");
|
||||
});
|
||||
|
||||
test("photo uploads accept only JPEG, PNG or WebP up to 2 MiB", () => {
|
||||
const body = prepareProductImage(new File(["x"], "a.jpg", { type: "image/jpeg" }));
|
||||
assert.ok(body.get("file") instanceof File);
|
||||
assert.throws(() => prepareProductImage(new File(["<svg/>"], "a.svg", { type: "image/svg+xml" })), /JPEG, PNG or WebP/);
|
||||
assert.throws(() => prepareProductImage(new File([new Uint8Array(2 * 1024 * 1024 + 1)], "a.png", { type: "image/png" })), /2 MiB/);
|
||||
assert.throws(() => prepareProductImage(null), /Choose/);
|
||||
});
|
||||
Loading…
Reference in New Issue