Refactor: Move start design server action to separate file
Extracted the server action logic from the component into a dedicated actions.ts file for better code organization.
This commit is contained in:
@@ -83,7 +83,8 @@
|
|||||||
"Bash(git commit -m 'Fix: Import revalidatePath from correct module *)",
|
"Bash(git commit -m 'Fix: Import revalidatePath from correct module *)",
|
||||||
"Bash(git commit -m 'Fix: Route photo requests to design canvas instead of approval upload *)",
|
"Bash(git commit -m 'Fix: Route photo requests to design canvas instead of approval upload *)",
|
||||||
"Bash(git commit -m 'Fix: Pass photo request ID instead of data URL *)",
|
"Bash(git commit -m 'Fix: Pass photo request ID instead of data URL *)",
|
||||||
"Bash(git commit -m 'Feature: Add product selection when starting design from photo request *)"
|
"Bash(git commit -m 'Feature: Add product selection when starting design from photo request *)",
|
||||||
|
"Bash(git commit -m 'Refactor: Move start design server action to separate file *)"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
'use server';
|
||||||
|
|
||||||
|
import { redirect } from 'next/navigation';
|
||||||
|
import { prisma } from '@/lib/prisma';
|
||||||
|
|
||||||
|
export async function startDesignWithProduct(formData: FormData, customRequestId: string) {
|
||||||
|
const productId = String(formData.get('productId') ?? '');
|
||||||
|
if (!productId) return;
|
||||||
|
|
||||||
|
const product = await prisma.product.findUnique({ where: { id: productId } });
|
||||||
|
if (product) {
|
||||||
|
redirect(`/made-to-order/${product.slug}?customRequestId=${customRequestId}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { notFound, redirect } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
import { prisma } from '@/lib/prisma';
|
import { prisma } from '@/lib/prisma';
|
||||||
import AdminNav from '@/components/AdminNav';
|
import AdminNav from '@/components/AdminNav';
|
||||||
|
import { startDesignWithProduct } from './actions';
|
||||||
|
|
||||||
export default async function StartDesignPage({ params }: { params: { id: string } }) {
|
export default async function StartDesignPage({ params }: { params: { id: string } }) {
|
||||||
const request = await prisma.customRequest.findUnique({
|
const request = await prisma.customRequest.findUnique({
|
||||||
@@ -14,15 +15,6 @@ export default async function StartDesignPage({ params }: { params: { id: string
|
|||||||
orderBy: { name: 'asc' },
|
orderBy: { name: 'asc' },
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSubmit = async (formData: FormData) => {
|
|
||||||
'use server';
|
|
||||||
const productId = String(formData.get('productId') ?? '');
|
|
||||||
const product = await prisma.product.findUnique({ where: { id: productId } });
|
|
||||||
if (product) {
|
|
||||||
redirect(`/made-to-order/${product.slug}?customRequestId=${request.id}`);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto max-w-2xl px-6 py-12">
|
<div className="mx-auto max-w-2xl px-6 py-12">
|
||||||
<AdminNav />
|
<AdminNav />
|
||||||
@@ -39,7 +31,10 @@ export default async function StartDesignPage({ params }: { params: { id: string
|
|||||||
<p className="text-sm"><span className="font-medium">Originally requested for:</span> {request.product.name}</p>
|
<p className="text-sm"><span className="font-medium">Originally requested for:</span> {request.product.name}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form action={handleSubmit} className="mt-6 space-y-4 border border-line bg-surface p-6">
|
<form action={async (formData) => {
|
||||||
|
'use server';
|
||||||
|
await startDesignWithProduct(formData, request.id);
|
||||||
|
}} className="mt-6 space-y-4 border border-line bg-surface p-6">
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="productId" className="tag-label mb-2 block">
|
<label htmlFor="productId" className="tag-label mb-2 block">
|
||||||
Which product to design on?
|
Which product to design on?
|
||||||
|
|||||||
Reference in New Issue
Block a user