Fix admin design edit page to properly parse JSON product fields

The DesignCanvas component expects product.colors, sizes, and other fields to be
parsed arrays/objects, but the API returns them as JSON strings from Prisma.
Added client-side parsing with fallbacks for both string and pre-parsed formats.

Also removed unused getCurrentAdmin import from the API route.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Andymick
2026-07-18 17:58:00 +01:00
co-authored by Claude Haiku 4.5
parent ac27cc554b
commit dda63e559b
2 changed files with 19 additions and 8 deletions
+18 -2
View File
@@ -48,8 +48,24 @@ export default function EditDesignPage({ params }: { params: { id: string } }) {
useEffect(() => {
fetch(`/api/designs/${params.id}`)
.then((r) => r.json())
.then(setDesign)
.finally(() => setLoading(false));
.then((data) => {
if (data.product) {
const parseIfString = (val: any) => typeof val === 'string' ? JSON.parse(val) : val;
data.product.colors = Array.isArray(data.product.colors) ? data.product.colors : parseIfString(data.product.colors || '[]');
data.product.sizes = Array.isArray(data.product.sizes) ? data.product.sizes : (data.product.sizes ? parseIfString(data.product.sizes) : []);
data.product.colorPhotos = typeof data.product.colorPhotos === 'object' ? data.product.colorPhotos : (data.product.colorPhotos ? parseIfString(data.product.colorPhotos) : {});
data.product.colorPhotosBack = typeof data.product.colorPhotosBack === 'object' ? data.product.colorPhotosBack : (data.product.colorPhotosBack ? parseIfString(data.product.colorPhotosBack) : {});
data.product.printArea = typeof data.product.printArea === 'object' ? data.product.printArea : parseIfString(data.product.printArea || '{"xPct":0,"yPct":0,"wPct":1,"hPct":1}');
if (data.product.printAreaBack) {
data.product.printAreaBack = typeof data.product.printAreaBack === 'object' ? data.product.printAreaBack : parseIfString(data.product.printAreaBack);
}
}
setDesign(data);
})
.catch((err) => {
console.error('Error fetching design:', err);
setLoading(false);
});
}, [params.id]);
const handleSendToCustomer = async () => {