-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jumper requirements and remove deprecated ones
- Loading branch information
1 parent
9c741b0
commit 4eb8dd7
Showing
13 changed files
with
317 additions
and
35 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { FormControl, FormField, FormItem, FormLabel } from "@/components/ui/Form" | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/components/ui/Select" | ||
import { Separator } from "@/components/ui/Separator" | ||
import { ComponentType } from "react" | ||
import { useFormContext, useWatch } from "react-hook-form" | ||
import { RequirementFormProps } from "requirements/types" | ||
import { JumperLevelForm } from "./components/JumperLevelForm" | ||
import { JumperTraitForm } from "./components/JumperTraitForm" | ||
import { JumperTypeForm } from "./components/JumperTypeForm" | ||
import { JumperRequirementType } from "./types" | ||
|
||
const jumperRequirementTypes = [ | ||
{ | ||
label: "Have at least level x", | ||
value: "JUMPER_LEVEL", | ||
JumperRequirement: JumperLevelForm, | ||
}, | ||
{ | ||
label: "Have a reward of a certain type", | ||
value: "JUMPER_TYPE", | ||
JumperRequirement: JumperTypeForm, | ||
}, | ||
{ | ||
label: "Have a trait", | ||
value: "JUMPER_TRAITS", | ||
JumperRequirement: JumperTraitForm, | ||
}, | ||
] satisfies { | ||
label: string | ||
value: JumperRequirementType | ||
JumperRequirement: ComponentType<RequirementFormProps> | ||
}[] | ||
|
||
const JumperForm = ({ baseFieldPath, field }: RequirementFormProps) => { | ||
const { control, resetField } = useFormContext() | ||
|
||
const type = useWatch({ name: `${baseFieldPath}.type` }) | ||
const selected = jumperRequirementTypes.find((reqType) => reqType.value === type) | ||
|
||
const resetForm = () => { | ||
resetField(`${baseFieldPath}.data.minAmount`, { defaultValue: undefined }) | ||
resetField(`${baseFieldPath}.data.rewardType`, { defaultValue: "" }) | ||
resetField(`${baseFieldPath}.data.category`, { defaultValue: "" }) | ||
resetField(`${baseFieldPath}.data.name`, { defaultValue: "" }) | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col items-start gap-4"> | ||
<FormField | ||
control={control} | ||
name={`${baseFieldPath}.type`} | ||
render={({ field }) => ( | ||
<FormItem className="w-full"> | ||
<FormLabel>Type</FormLabel> | ||
<Select | ||
onValueChange={(e) => { | ||
resetForm() | ||
field.onChange(e) | ||
}} | ||
defaultValue={field.value} | ||
value={field.value} | ||
> | ||
<FormControl> | ||
<SelectTrigger> | ||
<SelectValue placeholder="Select one..." /> | ||
</SelectTrigger> | ||
</FormControl> | ||
<SelectContent> | ||
{jumperRequirementTypes.map(({ value, label }) => ( | ||
<SelectItem key={value} value={value}> | ||
{label} | ||
</SelectItem> | ||
))} | ||
</SelectContent> | ||
</Select> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
{selected?.JumperRequirement && ( | ||
<> | ||
<Separator /> | ||
<selected.JumperRequirement baseFieldPath={baseFieldPath} field={field} /> | ||
</> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default JumperForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
Requirement, | ||
RequirementProps, | ||
} from "components/[guild]/Requirements/components/Requirement" | ||
import { useRequirementContext } from "components/[guild]/Requirements/components/RequirementContext" | ||
import { ComponentType } from "react" | ||
import REQUIREMENTS from "requirements" | ||
import { JumperRequirementType } from "./types" | ||
|
||
const JumperLevelDisplay = () => { | ||
const { data } = useRequirementContext<"JUMPER_LEVEL">() | ||
|
||
return `Wallet level ${data.minAmount} or above` | ||
} | ||
|
||
const JumperTypeDisplay = () => { | ||
const { data } = useRequirementContext<"JUMPER_TYPE">() | ||
|
||
return `Get a reward with type ${data.rewardType}` | ||
} | ||
|
||
const JumperTraitsDisplay = () => { | ||
const { data } = useRequirementContext<"JUMPER_TRAITS">() | ||
|
||
if ("category" in data && "name" in data) | ||
return `Get a trait in the ${data.category} category with title ${data.name}` | ||
|
||
if ("category" in data) return `Get a trait in the ${data.category} category` | ||
|
||
return `Get a trait with title ${data.name}` | ||
} | ||
|
||
const DisplayComponents = { | ||
JUMPER_LEVEL: JumperLevelDisplay, | ||
JUMPER_TYPE: JumperTypeDisplay, | ||
JUMPER_TRAITS: JumperTraitsDisplay, | ||
} satisfies Record<JumperRequirementType, ComponentType> | ||
|
||
const JumperRequirement = (props: RequirementProps) => { | ||
const { type } = useRequirementContext<JumperRequirementType>() | ||
const DisplayComponent = DisplayComponents[type] | ||
|
||
return ( | ||
<Requirement image={REQUIREMENTS.JUMPER_LEVEL.icon as string}> | ||
<DisplayComponent /> | ||
</Requirement> | ||
) | ||
} | ||
|
||
export default JumperRequirement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
FormErrorMessage, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
} from "@/components/ui/Form" | ||
import { Input } from "@/components/ui/Input" | ||
import { useFormContext } from "react-hook-form" | ||
import { RequirementFormProps } from "requirements/types" | ||
|
||
export const JumperLevelForm = ({ baseFieldPath }: RequirementFormProps) => { | ||
const { control } = useFormContext() | ||
|
||
return ( | ||
<FormField | ||
control={control} | ||
name={`${baseFieldPath}.data.minAmount`} | ||
rules={{ | ||
required: true, | ||
min: 1, | ||
}} | ||
render={({ field }) => ( | ||
<FormItem className="w-full"> | ||
<FormLabel>Level</FormLabel> | ||
<Input | ||
type="number" | ||
{...field} | ||
onChange={(e) => { | ||
const newValue = e.target.value | ||
|
||
// We need this to allow typing in a decimal point | ||
if (/^[0-9]*\.[0-9]*0*$/i.test(newValue)) { | ||
field.onChange?.(newValue, Number(newValue)) | ||
return field.onChange(newValue) | ||
} | ||
|
||
const parsedValue = parseInt(newValue) | ||
const returnedValue = isNaN(parsedValue) ? "" : parsedValue | ||
|
||
return field.onChange(returnedValue) | ||
}} | ||
/> | ||
<FormErrorMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
FormErrorMessage, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
} from "@/components/ui/Form" | ||
import { Input } from "@/components/ui/Input" | ||
import { useFormContext } from "react-hook-form" | ||
import { RequirementFormProps } from "requirements/types" | ||
|
||
export const JumperTraitForm = ({ baseFieldPath }: RequirementFormProps) => { | ||
const { control } = useFormContext() | ||
|
||
return ( | ||
<> | ||
<FormField | ||
control={control} | ||
name={`${baseFieldPath}.data.category`} | ||
render={({ field }) => ( | ||
<FormItem className="w-full"> | ||
<FormLabel>Category</FormLabel> | ||
<Input {...field} /> | ||
<FormErrorMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={control} | ||
name={`${baseFieldPath}.data.name`} | ||
render={({ field }) => ( | ||
<FormItem className="w-full"> | ||
<FormLabel>Name</FormLabel> | ||
<Input {...field} /> | ||
<FormErrorMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
FormErrorMessage, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
} from "@/components/ui/Form" | ||
import { Input } from "@/components/ui/Input" | ||
import { useFormContext } from "react-hook-form" | ||
import { RequirementFormProps } from "requirements/types" | ||
|
||
export const JumperTypeForm = ({ baseFieldPath }: RequirementFormProps) => { | ||
const { control } = useFormContext() | ||
|
||
return ( | ||
<FormField | ||
control={control} | ||
name={`${baseFieldPath}.data.rewardType`} | ||
rules={{ | ||
required: true, | ||
minLength: 1, | ||
}} | ||
render={({ field }) => ( | ||
<FormItem className="w-full"> | ||
<FormLabel>Reward type</FormLabel> | ||
<Input {...field} /> | ||
<FormErrorMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { RequirementType } from "requirements/types" | ||
|
||
export type JumperRequirementType = Extract<RequirementType, `JUMPER_${string}`> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.