Add option to create zone directly to filter select

This commit is contained in:
Timothy Armes
2024-10-21 15:19:28 +02:00
parent d44110649b
commit a422b1441a
6 changed files with 82 additions and 25 deletions
+66 -16
View File
@@ -1,23 +1,34 @@
import { useAtom } from "jotai";
import { useTranslations } from "next-intl";
import { SingleValue } from "react-select";
import { IoAdd } from "react-icons/io5";
import { GroupBase, OptionsOrGroups, SingleValue } from "react-select";
import { regionFilterAtom } from "@/atoms";
import { BaseOption, Select, SelectProps } from "@/components/form/Select";
import { useZones } from "@/hooks/useZones";
import { Zone } from "@/server/myZones";
import { useRouter } from "@/utils/navigation";
type RegionSelectProps = Omit<SelectProps, "options" | "value" | "onChange">;
type RegionSelectProps = Omit<SelectProps, "options" | "value" | "onChange"> & {
syncTitle: string;
};
export const RegionSelect = (selectProps: RegionSelectProps) => {
const t = useTranslations("Tournaments");
type RegionOption = BaseOption<string, Zone | null>;
type GroupedOption = GroupBase<RegionOption>;
export const RegionSelect = ({
syncTitle,
...selectProps
}: RegionSelectProps) => {
const t = useTranslations("Zones");
const router = useRouter();
const [regionFilter, setRegionFilter] = useAtom(regionFilterAtom);
const { zones } = useZones();
const zoneFilterOptions: BaseOption<string, Zone | null>[] = [
const zoneFilterOptions: OptionsOrGroups<RegionOption, GroupedOption> = [
{
value: "map",
label: t("syncWithMapOption"),
label: syncTitle,
data: null,
},
{
@@ -26,32 +37,71 @@ export const RegionSelect = (selectProps: RegionSelectProps) => {
data: null,
},
...zones.map((zone) => ({
value: zone.id,
label: zone.name,
data: zone,
})),
{
label: t("yourZonesSelectGroupHeader"),
options: [
...zones.map((zone) => ({
value: zone.id,
label: zone.name,
data: zone,
})),
{
value: "create",
label: t("createZoneSelectOption"),
data: null,
},
],
},
];
const onChange = (option: SingleValue<BaseOption<string, Zone | null>>) => {
const onChange = (option: SingleValue<RegionOption>) => {
if (!option) return;
if (option.value === "create") {
router.push("/zones/create");
return;
}
if (option.value === "map" || option.value === "all")
setRegionFilter(option.value);
else setRegionFilter(option.data!);
};
const formatGroupLabel = (data: GroupedOption) => (
<div className="relative -translate-y-1/2 border-b-2 border-primary-500 text-xs font-bold uppercase text-primary-500 ">
<div className="relative top-1/2 inline-block translate-y-1/2 bg-gray-50 pr-2 dark:bg-gray-700 ">
{data.label}
</div>
</div>
);
const formatOptionLabel = (option: RegionOption) => {
if (option.value === "create")
return (
<div className="flex items-center gap-1 italic">
<IoAdd />
{option.label}
</div>
);
return option.label;
};
const allOptions = zoneFilterOptions.flatMap((groupOrOption) =>
"options" in groupOrOption ? groupOrOption.options : groupOrOption,
);
return (
<Select
options={zoneFilterOptions}
value={zoneFilterOptions.find((o) =>
value={allOptions.find((o) =>
regionFilter === "all" || regionFilter === "map"
? o.value === regionFilter
: o.value === regionFilter.id,
)}
onChange={(v) =>
onChange(v as SingleValue<BaseOption<string, Zone | null>>)
}
formatGroupLabel={(g) => formatGroupLabel(g as GroupedOption)}
formatOptionLabel={(o) => formatOptionLabel(o as RegionOption)}
onChange={(v) => onChange(v as SingleValue<RegionOption>)}
{...selectProps}
/>
);