From 2f55fbf1a60077059aad34099591453578a03673 Mon Sep 17 00:00:00 2001 From: nisrinasalm Date: Thu, 10 Oct 2024 02:46:54 +0700 Subject: [PATCH] feat: add delete product --- app/Http/Controllers/ProductController.php | 9 ++++++++- package.json | 1 + pnpm-lock.yaml | 23 ++++++++++++++++++++++ resources/js/Layouts/GuestLayout.tsx | 6 +++--- resources/js/Pages/Product/Create.tsx | 6 +++++- resources/js/Pages/Product/Edit.tsx | 2 +- resources/js/Pages/Product/Index.tsx | 6 +++++- resources/js/Pages/Product/Show.tsx | 21 +++++++++++++++++--- 8 files changed, 64 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index 800f127..9c4e762 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -34,7 +34,7 @@ public function store(ProductRequest $request): RedirectResponse $product->save(); - return redirect()->intended(route('product.show')); + return redirect()->intended(route('product.show', $product->id)); } public function edit(string $id) { @@ -76,4 +76,11 @@ public function show(string $id) { return Inertia::render('Product/Show', compact('product')); } + public function destroy(string $id) { + $product = Product::findOrFail($id); + $product->delete(); + + return redirect()->route('product.index'); + } + } diff --git a/package.json b/package.json index a8c3140..97385d1 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "vite": "^5.0" }, "dependencies": { + "@inertiajs/inertia": "^0.11.1", "clsx": "^2.1.1", "lucide-react": "^0.451.0", "tailwind-merge": "^2.5.3" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 67f9ae5..ae3b1df 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + '@inertiajs/inertia': + specifier: ^0.11.1 + version: 0.11.1 clsx: specifier: ^2.1.1 version: 2.1.1 @@ -470,6 +473,9 @@ packages: '@inertiajs/core@1.2.0': resolution: {integrity: sha512-6U0gqCPbGGGMcLoDm+ckKipc5gptZMmfVFfPGdO7vlO7yipWf1RD+TKkcZGJklFvfgFMKwK2VPw8GAv1OctuQA==} + '@inertiajs/inertia@0.11.1': + resolution: {integrity: sha512-btmV53c54oW4Z9XF0YyTdIUnM7ue0ONy3/KJOz6J1C5CYIwimiKfDMpz8ZbGJuxS+SPdOlNsqj2ZhlHslpJRZg==} + '@inertiajs/react@1.2.0': resolution: {integrity: sha512-Q3wTaQJdoUbUB8YIGeQ0y2Tf/k8dNtz9Nu2dYr1pbYUBv++6d45iC/CFB/lIpqVvvUw8XuIai2bdsUcRSIbPCQ==} peerDependencies: @@ -734,6 +740,9 @@ packages: peerDependencies: postcss: ^8.1.0 + axios@0.21.4: + resolution: {integrity: sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==} + axios@1.7.7: resolution: {integrity: sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==} @@ -2144,6 +2153,14 @@ snapshots: transitivePeerDependencies: - debug + '@inertiajs/inertia@0.11.1': + dependencies: + axios: 0.21.4 + deepmerge: 4.3.1 + qs: 6.13.0 + transitivePeerDependencies: + - debug + '@inertiajs/react@1.2.0(react@18.3.1)': dependencies: '@inertiajs/core': 1.2.0 @@ -2406,6 +2423,12 @@ snapshots: postcss: 8.4.47 postcss-value-parser: 4.2.0 + axios@0.21.4: + dependencies: + follow-redirects: 1.15.9 + transitivePeerDependencies: + - debug + axios@1.7.7: dependencies: follow-redirects: 1.15.9 diff --git a/resources/js/Layouts/GuestLayout.tsx b/resources/js/Layouts/GuestLayout.tsx index 4e201fc..1f2a9b7 100644 --- a/resources/js/Layouts/GuestLayout.tsx +++ b/resources/js/Layouts/GuestLayout.tsx @@ -1,14 +1,14 @@ import ApplicationLogo from "@/Components/ApplicationLogo"; -import { Link } from "@inertiajs/react"; +import UnstyledLink from "@/Components/UnstyledLink"; import type { PropsWithChildren } from "react"; export default function Guest({ children }: PropsWithChildren) { return (
- + - +
diff --git a/resources/js/Pages/Product/Create.tsx b/resources/js/Pages/Product/Create.tsx index 9542f69..204d2ae 100644 --- a/resources/js/Pages/Product/Create.tsx +++ b/resources/js/Pages/Product/Create.tsx @@ -35,7 +35,11 @@ export default function Create() {
- + Back to Home Add Product diff --git a/resources/js/Pages/Product/Edit.tsx b/resources/js/Pages/Product/Edit.tsx index 7c8288b..e730bc4 100644 --- a/resources/js/Pages/Product/Edit.tsx +++ b/resources/js/Pages/Product/Edit.tsx @@ -48,7 +48,7 @@ export default function Edit({ product }: { product: ProductType }) { Back to Home - Add Product + Edit Product
diff --git a/resources/js/Pages/Product/Index.tsx b/resources/js/Pages/Product/Index.tsx index 492ca6c..68e8ee5 100644 --- a/resources/js/Pages/Product/Index.tsx +++ b/resources/js/Pages/Product/Index.tsx @@ -17,7 +17,11 @@ const ProductIndex = () => { All Product - + Add Product
diff --git a/resources/js/Pages/Product/Show.tsx b/resources/js/Pages/Product/Show.tsx index 338c0c0..b96f633 100644 --- a/resources/js/Pages/Product/Show.tsx +++ b/resources/js/Pages/Product/Show.tsx @@ -1,11 +1,22 @@ +import Button from "@/Components/Button"; import ButtonLink from "@/Components/ButtonLink"; import Typography from "@/Components/Typography"; import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout"; import { ProductType } from "@/types/entities/product"; +import { Inertia } from "@inertiajs/inertia"; import { Head } from "@inertiajs/react"; import { ArrowLeft } from "lucide-react"; export default function Show({ product }: { product: ProductType }) { + const handleDelete = () => { + if (confirm("Are you sure you want to delete this product?")) { + Inertia.delete(`/product/${product.id}/delete`, { + onSuccess: () => { + alert("Product deleted successfully!"); + }, + }); + } + }; return ( @@ -45,12 +56,16 @@ export default function Show({ product }: { product: ProductType }) {
- + Edit Product - +