mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-11-18 07:22:06 +00:00
Add option to create zone directly to filter select
This commit is contained in:
@@ -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}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -45,7 +45,7 @@ export const classNames = <Option, IsMulti extends boolean = false>(
|
||||
"border-gray-300 bg-gray-50 text-gray-900",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white",
|
||||
),
|
||||
groupHeading: () => "ml-3 mt-2 mb-1 text-textGray text-sm uppercase",
|
||||
groupHeading: () => "mx-3 mt-2 mb-1 text-textGray text-sm uppercase",
|
||||
option: ({ isFocused, isDisabled }) =>
|
||||
twMerge(
|
||||
"px-3 py-2 hover:cursor-pointer",
|
||||
|
||||
Reference in New Issue
Block a user