feat(product): 優化編輯後的跳轉邏輯,支援依來源回傳詳情頁或列表

This commit is contained in:
2026-02-05 16:01:29 +08:00
parent 075b9f1c98
commit 1d5bc68444
4 changed files with 15 additions and 4 deletions

View File

@@ -260,6 +260,10 @@ class ProductController extends Controller
$product->update($validated); $product->update($validated);
if ($request->input('from') === 'show') {
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
}
return redirect()->route('products.index')->with('success', '商品已更新'); return redirect()->route('products.index')->with('success', '商品已更新');
} }

View File

@@ -22,6 +22,8 @@ export default function ProductForm({
units, units,
}: ProductFormProps) { }: ProductFormProps) {
const isEdit = !!initialData; const isEdit = !!initialData;
const urlParams = new URLSearchParams(window.location.search);
const from = urlParams.get('from');
const { data, setData, post, put, processing, errors } = useForm({ const { data, setData, post, put, processing, errors } = useForm({
code: initialData?.code || "", code: initialData?.code || "",
@@ -45,7 +47,7 @@ export default function ProductForm({
e.preventDefault(); e.preventDefault();
if (isEdit) { if (isEdit) {
put(route("products.update", initialData.id), { put(route("products.update", { product: initialData.id, from }), {
onSuccess: () => toast.success("商品已更新"), onSuccess: () => toast.success("商品已更新"),
onError: () => toast.error("更新失敗,請檢查輸入資料"), onError: () => toast.error("更新失敗,請檢查輸入資料"),
}); });

View File

@@ -14,6 +14,11 @@ interface Props {
} }
export default function Edit({ product, categories, units }: Props) { export default function Edit({ product, categories, units }: Props) {
const urlParams = new URLSearchParams(window.location.search);
const from = urlParams.get('from');
const backUrl = from === 'show' ? route('products.show', product.id) : route('products.index');
const backText = from === 'show' ? "返回商品詳情" : "返回商品列表";
return ( return (
<AuthenticatedLayout <AuthenticatedLayout
breadcrumbs={getEditBreadcrumbs("products")} breadcrumbs={getEditBreadcrumbs("products")}
@@ -23,13 +28,13 @@ export default function Edit({ product, categories, units }: Props) {
<div className="container mx-auto p-6 max-w-7xl"> <div className="container mx-auto p-6 max-w-7xl">
{/* Header */} {/* Header */}
<div className="mb-6"> <div className="mb-6">
<Link href={route("products.index")}> <Link href={backUrl}>
<Button <Button
variant="outline" variant="outline"
className="gap-2 button-outlined-primary mb-4" className="gap-2 button-outlined-primary mb-4"
> >
<ArrowLeft className="h-4 w-4" /> <ArrowLeft className="h-4 w-4" />
{backText}
</Button> </Button>
</Link> </Link>

View File

@@ -67,7 +67,7 @@ export default function ProductShow({ product }: Props) {
</div> </div>
<div className="flex gap-2"> <div className="flex gap-2">
<Can permission="products.edit"> <Can permission="products.edit">
<Link href={route('products.edit', product.id)}> <Link href={route('products.edit', { product: product.id, from: 'show' })}>
<Button className="button-filled-primary"> <Button className="button-filled-primary">
</Button> </Button>