Filtering by zone

This commit is contained in:
Timothy Armes
2024-10-21 15:19:28 +02:00
parent 45ea8960e0
commit d44110649b
17 changed files with 336 additions and 136 deletions
+58
View File
@@ -0,0 +1,58 @@
import { useAtom } from "jotai";
import { useTranslations } from "next-intl";
import { 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";
type RegionSelectProps = Omit<SelectProps, "options" | "value" | "onChange">;
export const RegionSelect = (selectProps: RegionSelectProps) => {
const t = useTranslations("Tournaments");
const [regionFilter, setRegionFilter] = useAtom(regionFilterAtom);
const { zones } = useZones();
const zoneFilterOptions: BaseOption<string, Zone | null>[] = [
{
value: "map",
label: t("syncWithMapOption"),
data: null,
},
{
value: "all",
label: t("ignoreMapOption"),
data: null,
},
...zones.map((zone) => ({
value: zone.id,
label: zone.name,
data: zone,
})),
];
const onChange = (option: SingleValue<BaseOption<string, Zone | null>>) => {
if (!option) return;
if (option.value === "map" || option.value === "all")
setRegionFilter(option.value);
else setRegionFilter(option.data!);
};
return (
<Select
options={zoneFilterOptions}
value={zoneFilterOptions.find((o) =>
regionFilter === "all" || regionFilter === "map"
? o.value === regionFilter
: o.value === regionFilter.id,
)}
onChange={(v) =>
onChange(v as SingleValue<BaseOption<string, Zone | null>>)
}
{...selectProps}
/>
);
};
+13 -1
View File
@@ -1,6 +1,6 @@
"use client";
import { useState } from "react";
import { useEffect, useState } from "react";
import { zodResolver } from "@hookform/resolvers/zod";
import { signIn } from "next-auth/react";
@@ -37,6 +37,18 @@ export const SignInForm = ({ callbackPath }: SignInFormProps) => {
resolver: zodResolver(signInSchema),
});
useEffect(() => {
// Clear the response message when the email field changes
const subscription = form.watch((value, { name, type }) => {
if (type === "change") {
if (name === "email" && responseMessage.message === "") {
setResponseMessage({ isSuccessful: true, message: "" });
}
}
});
return () => subscription.unsubscribe();
}, [form, form.watch]);
const onSubmit = async ({ email }: SignInFormValues) => {
try {
const result = await signIn("nodemailer", {
+2 -1
View File
@@ -15,7 +15,8 @@ import AsyncSelect from "react-select/async";
import { Prettify } from "@/types";
import { Field, GenericFieldProps } from "./Field";
import { BaseOption, classNames } from "./SelectField";
import { classNames } from "./Select";
import { BaseOption } from "./SelectField";
export type AsyncSelectFieldProps<
TFieldValues extends FieldValues = FieldValues,
+120
View File
@@ -0,0 +1,120 @@
import React from "react";
import { useTranslations } from "next-intl";
import ReactSelect, { ClassNamesConfig, GroupBase, Props } from "react-select";
import { twMerge } from "tailwind-merge";
import { Prettify } from "@/types";
export type BaseOption<T = string, D = unknown> = {
value: T;
label: string | JSX.Element;
disabled?: boolean;
data?: D;
};
export const classNames = <Option, IsMulti extends boolean = false>(
hasError: boolean,
separators: boolean,
): ClassNamesConfig<Option, IsMulti, GroupBase<Option>> => ({
container: () => "w-full",
valueContainer: () => "text-gray-900 dark:text-white",
indicatorsContainer: () => "flex items-center self-stretch shrink-0",
clearIndicator: () => "flex items-center pr-2 text-gray-900 dark:text-white",
dropdownIndicator: () =>
"pointer-events-none flex items-center pr-2 text-gray-900 dark:text-white",
indicatorSeparator: () => "w-px text-gray-900 dark:text-white",
control: (state) =>
twMerge(
"group flex w-full items-center justify-between rounded-lg border p-3 text-sm",
"border-gray-300 bg-gray-50 text-gray-900 shadow-sm focus-within:border-primary-500 focus-within:ring-primary-500",
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus-within:border-primary-500 dark:focus-within:ring-primary-500",
hasError && "ring-1 ring-error",
state.isDisabled && "cursor-not-allowed",
state.isFocused && "border-primary ring ring-primary ring-opacity-50",
),
multiValue: () => "bg-fieldGray border rounded-lg flex space-x-1 pl-1 m-1",
multiValueLabel: () => "",
multiValueRemove: () => "items-center px-1 hover:text-primary",
placeholder: () =>
"block truncate pr-2 placeholder-gray dark:placeholder-gray-400",
menu: () =>
twMerge(
"!z-30 mt-2 rounded-lg border border-gray-200 bg-white p-1",
"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",
option: ({ isFocused, isDisabled }) =>
twMerge(
"px-3 py-2 hover:cursor-pointer",
separators && "border-b border-gray-200",
isDisabled && "opacity-50",
isFocused && "hover:bg-primary-500 hover:text-white",
),
noOptionsMessage: () =>
"text-textGray p-2 bg-gray-50 border border-dashed border-gray-200 rounded-sm",
});
export type SelectProps<
IsMulti extends boolean = false,
T = string,
D = unknown,
> = Prettify<
Omit<
Props<BaseOption<T, D>, IsMulti, GroupBase<BaseOption<T, D>>>,
"classNames"
> & {
required?: boolean;
separators?: boolean;
hasError?: boolean;
classNameOverrides?: ClassNamesConfig<
BaseOption<T, D>,
IsMulti,
GroupBase<BaseOption<T, D>>
>;
}
>;
export const Select = <
IsMulti extends boolean = false,
T = string,
D = unknown,
>({
className,
required,
placeholder,
separators,
hasError,
options,
classNameOverrides,
...selectProps
}: SelectProps<IsMulti, T, D>) => {
const t = useTranslations("App");
return (
<ReactSelect
options={options}
noOptionsMessage={() => t("noOptionsMessage")}
placeholder={placeholder ?? t("selectPlaceholder")}
unstyled
styles={{
input: (base) => ({
...base,
"input:focus": {
boxShadow: "none",
},
}),
}}
classNames={{
...classNames<BaseOption<T, D>, IsMulti>(
hasError ?? false,
separators ?? false,
),
...(classNameOverrides ?? {}),
}}
{...selectProps}
/>
);
};
+4 -67
View File
@@ -8,14 +8,9 @@ import {
FieldValues,
useFormContext,
} from "react-hook-form";
import Select, {
ClassNamesConfig,
GroupBase,
OnChangeValue,
Props,
} from "react-select";
import { twMerge } from "tailwind-merge";
import { GroupBase, OnChangeValue } from "react-select";
import { Select, SelectProps } from "@/components/form/Select";
import { Prettify } from "@/types";
import { Field, GenericFieldProps } from "./Field";
@@ -27,50 +22,6 @@ export type BaseOption<T = string, D = unknown> = {
data?: D;
};
export const classNames = <Option, IsMulti extends boolean = false>(
hasError: boolean,
separators: boolean,
): ClassNamesConfig<Option, IsMulti, GroupBase<Option>> => ({
container: () => "w-full",
valueContainer: () => "text-gray-900 dark:text-white",
indicatorsContainer: () => "flex items-center self-stretch shrink-0",
clearIndicator: () => "flex items-center pr-2 text-gray-900 dark:text-white",
dropdownIndicator: () =>
"pointer-events-none flex items-center pr-2 text-gray-900 dark:text-white",
indicatorSeparator: () => "w-px text-gray-900 dark:text-white",
control: (state) =>
twMerge(
"group flex w-full items-center justify-between rounded-lg border p-3",
"border-gray-300 bg-gray-50 text-gray-900 shadow-sm focus-within:border-primary-500 focus-within:ring-primary-500",
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus-within:border-primary-500 dark:focus-within:ring-primary-500",
hasError && "ring-1 ring-error",
state.isDisabled && "cursor-not-allowed",
state.isFocused && "border-primary ring ring-primary ring-opacity-50",
),
multiValue: () => "bg-fieldGray border rounded-lg flex space-x-1 pl-1 m-1",
multiValueLabel: () => "",
multiValueRemove: () => "items-center px-1 hover:text-primary",
placeholder: () =>
"block truncate pr-2 placeholder-gray dark:placeholder-gray-400",
menu: () =>
twMerge(
"!z-30 mt-2 rounded-lg border border-gray-200 bg-white p-1",
"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",
option: ({ isFocused, isDisabled }) =>
twMerge(
"px-3 py-2 hover:cursor-pointer",
separators && "border-b border-gray-200",
isDisabled && "opacity-50",
isFocused && "hover:bg-primary-500 hover:text-white",
),
noOptionsMessage: () =>
"text-textGray p-2 bg-gray-50 border border-dashed border-gray-200 rounded-sm",
});
export type SelectFieldProps<
TFieldValues extends FieldValues = FieldValues,
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
@@ -80,7 +31,7 @@ export type SelectFieldProps<
> = Prettify<
GenericFieldProps<TFieldValues, TFieldName> &
Omit<
Props<BaseOption<T, D>, IsMulti, GroupBase<BaseOption<T, D>>>,
SelectProps<IsMulti, T, D>,
"onChange" | "value" | "classNames" | "name"
> & {
required?: boolean;
@@ -168,21 +119,7 @@ export const SelectField = <
value={optionValue}
onChange={onSelectChange}
options={options}
noOptionsMessage={() => t("noOptionsMessage")}
placeholder={placeholder ?? t("selectPlaceholder")}
unstyled
styles={{
input: (base) => ({
...base,
"input:focus": {
boxShadow: "none",
},
}),
}}
classNames={classNames<BaseOption<T, D>, IsMulti>(
hasError,
separators ?? false,
)}
hasError={hasError}
{...selectProps}
/>
);