From dde2fc5a051ab701c1ac86ff6ee1f3cf731d26ac Mon Sep 17 00:00:00 2001 From: AmirReza Jamali Date: Sun, 28 Sep 2025 23:19:12 +0330 Subject: [PATCH] fix: Prevent edit page crash and correct date formatting On the company edit page, added optional chaining to the `defaultValues` prop. This prevents a potential runtime error if the company data is undefined when the page loads. Additionally, this commit corrects the `unixToDate` utility function. The previous logic could incorrectly include 'false' in the format string when time was not required. It now uses a ternary operator to conditionally add the time format, ensuring correct output. Minor code formatting adjustments are also included. --- .../companies/details/[id]/page.tsx | 116 ++++++++++++++++++ .../(companies)/companies/edit/[id]/page.tsx | 2 +- .../notifications/CreateNotification.tsx | 2 +- src/lib/api/services/companies-service.ts | 2 +- src/utils/shared.ts | 2 +- 5 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 src/app/(companies)/companies/details/[id]/page.tsx diff --git a/src/app/(companies)/companies/details/[id]/page.tsx b/src/app/(companies)/companies/details/[id]/page.tsx new file mode 100644 index 0000000..072a6b4 --- /dev/null +++ b/src/app/(companies)/companies/details/[id]/page.tsx @@ -0,0 +1,116 @@ +"use client"; +import Loading from "@/app/loading"; +import Breadcrumb from "@/components/Breadcrumbs/Breadcrumb"; +import { ShowcaseSection } from "@/components/Layouts/showcase-section"; +import IsVisible from "@/components/ui/IsVisible"; +import Status from "@/components/ui/Status"; +import { useCompanyDetails } from "@/hooks/queries"; +import { BreadcrumbItem } from "@/types/shared"; +import { formatPhoneNumber, unixToDate } from "@/utils/shared"; +import getSymbolFromCurrency from "currency-symbol-map"; +import Link from "next/link"; +import { useRouter } from "next/navigation"; +import { use } from "react"; + +interface IProps { + params: Promise<{ id: string }>; +} + +const CompanyDetailsPage = ({ params }: IProps) => { + const { id } = use(params); + const breadcrumbItems: BreadcrumbItem[] = [ + { + href: "/", + name: "Dashboard", + }, + { + href: "/companies", + name: "Companies", + }, + { + href: `/companies/details/${id}`, + name: "Company details", + }, + ]; + const { data, isLoading } = useCompanyDetails(id); + const router = useRouter(); + if (isLoading) return ; + + return ( + <> + + + + {data.data.name} + {data.data.status} + + } + > +
+ + + {`${data?.data?.currency} ${getSymbolFromCurrency(data?.data.currency ?? "USD")}`} + + + + {data?.data?.language} + + + {unixToDate({ unix: data?.data?.created_at })} + + + {data?.data?.annual_revenue} + + + {data?.data?.email} + + + {data?.data?.employee_count} + + + {data?.data?.founded_year} + + + {data?.data?.industry} + + + {data?.data?.is_compliant ? "Yes" : "No"} + + + {data?.data?.is_verified ? "Yes" : "No"} + + + {formatPhoneNumber(data?.data?.phone ?? 0)} + + + {data?.data?.registration_number} + + + {data?.data?.tax_id} + + + {data?.data?.type} + + + {unixToDate({ unix: data?.data?.updated_at ?? 0 })} + + + + + {data.data.website} + + + +
+
+ + ); +}; + +export default CompanyDetailsPage; diff --git a/src/app/(companies)/companies/edit/[id]/page.tsx b/src/app/(companies)/companies/edit/[id]/page.tsx index 23544d0..d078789 100644 --- a/src/app/(companies)/companies/edit/[id]/page.tsx +++ b/src/app/(companies)/companies/edit/[id]/page.tsx @@ -18,7 +18,7 @@ const EditCompanyPage = ({ params }: { params: Promise<{ id: string }> }) => { return ( <> - + ); }; diff --git a/src/components/forms/notifications/CreateNotification.tsx b/src/components/forms/notifications/CreateNotification.tsx index 40a2574..fa2c8fe 100644 --- a/src/components/forms/notifications/CreateNotification.tsx +++ b/src/components/forms/notifications/CreateNotification.tsx @@ -100,7 +100,7 @@ const CreateNotificationForm = () => { {...register("link", { pattern: { value: REGEX.URL, - message: FormErrorMessages.invalidPattern, + message: FormErrorMessages.invalidPattern, }, })} name="link" diff --git a/src/lib/api/services/companies-service.ts b/src/lib/api/services/companies-service.ts index c78ce6a..12a35b8 100644 --- a/src/lib/api/services/companies-service.ts +++ b/src/lib/api/services/companies-service.ts @@ -128,7 +128,7 @@ export const companyCategoriesService = { throw error; } }, - updateCategories: async ({ + updateCategories: async ({ id, credentials, }: { diff --git a/src/utils/shared.ts b/src/utils/shared.ts index 5cca52b..b451b25 100644 --- a/src/utils/shared.ts +++ b/src/utils/shared.ts @@ -7,7 +7,7 @@ export const unixToDate = ({ unix: number; hasTime?: boolean; }) => { - return moment.unix(unix).format(`YYYY/MM/DD ${hasTime && "HH:MM"}`); + return moment.unix(unix).format(`YYYY/MM/DD ${hasTime ? "HH:MM" : ""}`); }; export const msToDate = (ms: number) => { return moment.default(ms).format("YYYY/MM/DD");