feat(product): 優化編輯後的跳轉邏輯,支援依來源回傳詳情頁或列表
This commit is contained in:
@@ -260,6 +260,10 @@ class ProductController extends Controller
|
||||
|
||||
$product->update($validated);
|
||||
|
||||
if ($request->input('from') === 'show') {
|
||||
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
|
||||
}
|
||||
|
||||
return redirect()->route('products.index')->with('success', '商品已更新');
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ export default function ProductForm({
|
||||
units,
|
||||
}: ProductFormProps) {
|
||||
const isEdit = !!initialData;
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const from = urlParams.get('from');
|
||||
|
||||
const { data, setData, post, put, processing, errors } = useForm({
|
||||
code: initialData?.code || "",
|
||||
@@ -45,7 +47,7 @@ export default function ProductForm({
|
||||
e.preventDefault();
|
||||
|
||||
if (isEdit) {
|
||||
put(route("products.update", initialData.id), {
|
||||
put(route("products.update", { product: initialData.id, from }), {
|
||||
onSuccess: () => toast.success("商品已更新"),
|
||||
onError: () => toast.error("更新失敗,請檢查輸入資料"),
|
||||
});
|
||||
|
||||
@@ -14,6 +14,11 @@ interface 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 (
|
||||
<AuthenticatedLayout
|
||||
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">
|
||||
{/* Header */}
|
||||
<div className="mb-6">
|
||||
<Link href={route("products.index")}>
|
||||
<Link href={backUrl}>
|
||||
<Button
|
||||
variant="outline"
|
||||
className="gap-2 button-outlined-primary mb-4"
|
||||
>
|
||||
<ArrowLeft className="h-4 w-4" />
|
||||
返回商品列表
|
||||
{backText}
|
||||
</Button>
|
||||
</Link>
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ export default function ProductShow({ product }: Props) {
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user