refactor(TenderDetails): enhance country flag handling and validation

- Updated the TenderDetails component to validate country codes using a new utility function, isValidAlpha2CountryCode.
- Modified the useGetFlagQuery hook to accept an options parameter for conditional fetching based on the validity of the country code.
- Improved the rendering logic for the country flag to ensure it only displays when valid flag data is available.
This commit is contained in:
AmirReza Jamali
2026-04-13 08:16:21 +03:30
parent 50db71843b
commit 76cb29d31e
3 changed files with 20 additions and 8 deletions
+14 -7
View File
@@ -1,12 +1,12 @@
"use client"; "use client";
import Loading from "@/components/loading";
import { LocationIcon } from "@/assets/icons"; import { LocationIcon } from "@/assets/icons";
import Breadcrumb from "@/components/Breadcrumbs/Breadcrumb"; import Breadcrumb from "@/components/Breadcrumbs/Breadcrumb";
import { ShowcaseSection } from "@/components/Layouts/showcase-section"; import { ShowcaseSection } from "@/components/Layouts/showcase-section";
import Loading from "@/components/loading";
import Status from "@/components/ui/Status"; import Status from "@/components/ui/Status";
import { useTenderDetailQuery } from "@/hooks/queries"; import { useTenderDetailQuery } from "@/hooks/queries";
import { useGetFlagQuery } from "@/hooks/queries/useFlagsQueries"; import { useGetFlagQuery } from "@/hooks/queries/useFlagsQueries";
import { msToDate } from "@/utils/shared"; import { isValidAlpha2CountryCode, msToDate } from "@/utils/shared";
import getSymbolFromCurrency from "currency-symbol-map"; import getSymbolFromCurrency from "currency-symbol-map";
import Link from "next/link"; import Link from "next/link";
import { use } from "react"; import { use } from "react";
@@ -20,12 +20,17 @@ interface IProps {
const TenderDetails = ({ params }: IProps) => { const TenderDetails = ({ params }: IProps) => {
const { details } = use(params); const { details } = use(params);
const { data, isLoading, isError } = useTenderDetailQuery(details); const { data, isLoading, isError } = useTenderDetailQuery(details);
const countryCodeNormalized =
data?.data.country_code?.trim().toUpperCase() ?? "";
const countryCodeValid = isValidAlpha2CountryCode(countryCodeNormalized);
const { data: flagData } = useGetFlagQuery(countryCodeNormalized, {
enabled: countryCodeValid,
});
const breadcrumbItems = [ const breadcrumbItems = [
{ name: "Dashboard", href: "/" }, { name: "Dashboard", href: "/" },
{ name: "Tenders", href: "/tenders" }, { name: "Tenders", href: "/tenders" },
{ name: "Tender details", href: `/tenders/${details}` }, { name: "Tender details", href: `/tenders/${details}` },
]; ];
const { data: flagData } = useGetFlagQuery(data?.data.country_code ?? "EU");
if (isLoading) return <Loading />; if (isLoading) return <Loading />;
if (isError) return <div>Error</div>; if (isError) return <div>Error</div>;
return ( return (
@@ -83,10 +88,12 @@ const TenderDetails = ({ params }: IProps) => {
<div className="flex items-center gap-1"> <div className="flex items-center gap-1">
<LocationIcon /> <LocationIcon />
<p className="text-sm uppercase">{data?.data.country_code}</p> <p className="text-sm uppercase">{data?.data.country_code}</p>
<div {flagData && (
dangerouslySetInnerHTML={{ __html: flagData }} <div
className="flex w-7 items-center justify-center overflow-hidden rounded-md border p-0" dangerouslySetInnerHTML={{ __html: flagData }}
/> className="flex w-7 items-center justify-center overflow-hidden rounded-md border p-0"
/>
)}
</div> </div>
</div> </div>
{data?.data.currency && ( {data?.data.currency && (
+5 -1
View File
@@ -3,7 +3,10 @@ import { flagsService } from "@/lib/api/services/flags-service";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react"; import { useMemo } from "react";
export const useGetFlagQuery = (countryCode: string) => { export const useGetFlagQuery = (
countryCode: string,
options?: { enabled?: boolean },
) => {
const queryKey = useMemo( const queryKey = useMemo(
() => [API_ENDPOINTS.FLAGS(countryCode)], () => [API_ENDPOINTS.FLAGS(countryCode)],
[countryCode], [countryCode],
@@ -12,5 +15,6 @@ export const useGetFlagQuery = (countryCode: string) => {
return useQuery({ return useQuery({
queryKey, queryKey,
queryFn: () => flagsService.getCountryFlag(countryCode), queryFn: () => flagsService.getCountryFlag(countryCode),
enabled: options?.enabled ?? true,
}); });
}; };
+1
View File
@@ -110,3 +110,4 @@ export const deleteEmptyKeys = (obj: Record<string, any>) => {
} }
return newObj; return newObj;
}; };
export const isValidAlpha2CountryCode = (code: string) => /^[A-Za-z]{2}$/.test(code);