diff --git a/apps/edr-freight-web/portal/src/components/dynamic-select/DynamicSelect.tsx b/apps/edr-freight-web/portal/src/components/dynamic-select/DynamicSelect.tsx new file mode 100644 index 000000000..2003457f0 --- /dev/null +++ b/apps/edr-freight-web/portal/src/components/dynamic-select/DynamicSelect.tsx @@ -0,0 +1,99 @@ +import { useQuery } from "@tanstack/react-query"; +import { cn } from "@/lib/utils"; +import { api } from "@/services/api"; +import { Loader2, AlertCircle } from "lucide-react"; +import * as React from "react"; + +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@edr/ui-common"; + +export interface DynamicSelectProps { + code: string; + placeholder?: string; + value?: string; + onValueChange?: (value: string) => void; + disabled?: boolean; + className?: string; +} + +export function DynamicSelect({ + code, + placeholder, + value, + onValueChange, + disabled, + className, +}: DynamicSelectProps) { + const { data, isLoading, isError } = useQuery( + api.dropdownSettings.getByCode.queryOptions({ input: { code } }), + ); + + if (isLoading) { + return ( +
+ + Loading... +
+ ); + } + + if (isError || !data) { + return ( +
+ + Failed to load options +
+ ); + } + + const options = [...data.children].sort((a, b) => a.order - b.order); + + return ( + + ); +} diff --git a/apps/edr-freight-web/portal/src/components/dynamic-select/index.ts b/apps/edr-freight-web/portal/src/components/dynamic-select/index.ts new file mode 100644 index 000000000..4e6c58b62 --- /dev/null +++ b/apps/edr-freight-web/portal/src/components/dynamic-select/index.ts @@ -0,0 +1,2 @@ +export { DynamicSelect } from "./DynamicSelect"; +export type { DynamicSelectProps } from "./DynamicSelect";