|
| 1 | +"use client"; |
| 2 | +import { DashboardSidebar } from "@/components"; |
| 3 | +import { useRouter } from "next/navigation"; |
| 4 | +import React, { useEffect, useState, use } from "react"; |
| 5 | +import toast from "react-hot-toast"; |
| 6 | +import { formatCategoryName } from "../../../../../utils/categoryFormating"; |
| 7 | +import { convertCategoryNameToURLFriendly } from "../../../../../utils/categoryFormating"; |
| 8 | +import apiClient from "@/lib/api"; |
| 9 | + |
| 10 | +interface DashboardSingleCategoryProps { |
| 11 | + params: Promise<{ id: string }>; |
| 12 | +} |
| 13 | + |
| 14 | +const DashboardSingleCategory = ({ |
| 15 | + params, |
| 16 | +}: DashboardSingleCategoryProps) => { |
| 17 | + const resolvedParams = use(params); |
| 18 | + const id = resolvedParams.id; |
| 19 | + |
| 20 | + const [categoryInput, setCategoryInput] = useState<{ name: string }>({ |
| 21 | + name: "", |
| 22 | + }); |
| 23 | + const router = useRouter(); |
| 24 | + |
| 25 | + const deleteCategory = async () => { |
| 26 | + const requestOptions = { |
| 27 | + method: "DELETE", |
| 28 | + }; |
| 29 | + // sending API request for deleting a category |
| 30 | + apiClient.delete(`/api/categories/${id}`, requestOptions) |
| 31 | + .then((response) => { |
| 32 | + if (response.status === 204) { |
| 33 | + toast.success("Category deleted successfully"); |
| 34 | + router.push("/admin/categories"); |
| 35 | + } else { |
| 36 | + throw Error("There was an error deleting a category"); |
| 37 | + } |
| 38 | + }) |
| 39 | + .catch((error) => { |
| 40 | + toast.error("There was an error deleting category"); |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + const updateCategory = async () => { |
| 45 | + if (categoryInput.name.length > 0) { |
| 46 | + const requestOptions = { |
| 47 | + method: "PUT", |
| 48 | + headers: { "Content-Type": "application/json" }, |
| 49 | + body: JSON.stringify({ |
| 50 | + name: convertCategoryNameToURLFriendly(categoryInput.name), |
| 51 | + }), |
| 52 | + }; |
| 53 | + // sending API request for updating a category |
| 54 | + apiClient.put(`/api/categories/${id}`, requestOptions) |
| 55 | + .then((response) => { |
| 56 | + if (response.status === 200) { |
| 57 | + return response.json(); |
| 58 | + } else { |
| 59 | + throw Error("Error updating a category"); |
| 60 | + } |
| 61 | + }) |
| 62 | + .then((data) => toast.success("Category successfully updated")) |
| 63 | + .catch((error) => { |
| 64 | + toast.error("There was an error while updating a category"); |
| 65 | + }); |
| 66 | + } else { |
| 67 | + toast.error("For updating a category you must enter all values"); |
| 68 | + return; |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + useEffect(() => { |
| 73 | + // sending API request for getting single categroy |
| 74 | + apiClient.get(`/api/categories/${id}`) |
| 75 | + .then((res) => { |
| 76 | + return res.json(); |
| 77 | + }) |
| 78 | + .then((data) => { |
| 79 | + setCategoryInput({ |
| 80 | + name: data?.name, |
| 81 | + }); |
| 82 | + }); |
| 83 | + }, [id]); |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="bg-white flex justify-start max-w-screen-2xl mx-auto xl:h-full max-xl:flex-col max-xl:gap-y-5"> |
| 87 | + <DashboardSidebar /> |
| 88 | + <div className="flex flex-col gap-y-7 xl:pl-5 max-xl:px-5 w-full"> |
| 89 | + <h1 className="text-3xl font-semibold">Category details</h1> |
| 90 | + <div> |
| 91 | + <label className="form-control w-full max-w-xs"> |
| 92 | + <div className="label"> |
| 93 | + <span className="label-text">Category name:</span> |
| 94 | + </div> |
| 95 | + <input |
| 96 | + type="text" |
| 97 | + className="input input-bordered w-full max-w-xs" |
| 98 | + value={formatCategoryName(categoryInput.name)} |
| 99 | + onChange={(e) => |
| 100 | + setCategoryInput({ ...categoryInput, name: e.target.value }) |
| 101 | + } |
| 102 | + /> |
| 103 | + </label> |
| 104 | + </div> |
| 105 | + |
| 106 | + <div className="flex gap-x-2 max-sm:flex-col"> |
| 107 | + <button |
| 108 | + type="button" |
| 109 | + className="uppercase bg-blue-500 px-10 py-5 text-lg border border-black border-gray-300 font-bold text-white shadow-sm hover:bg-blue-600 hover:text-white focus:outline-none focus:ring-2" |
| 110 | + onClick={updateCategory} |
| 111 | + > |
| 112 | + Update category |
| 113 | + </button> |
| 114 | + <button |
| 115 | + type="button" |
| 116 | + className="uppercase bg-red-600 px-10 py-5 text-lg border border-black border-gray-300 font-bold text-white shadow-sm hover:bg-red-700 hover:text-white focus:outline-none focus:ring-2" |
| 117 | + onClick={deleteCategory} |
| 118 | + > |
| 119 | + Delete category |
| 120 | + </button> |
| 121 | + </div> |
| 122 | + <p className="text-xl text-error max-sm:text-lg"> |
| 123 | + Note: if you delete this category, you will delete all products |
| 124 | + associated with the category. |
| 125 | + </p> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +export default DashboardSingleCategory; |
0 commit comments