Skip to content

Commit

Permalink
fix: check whether it's undefined (#2817)
Browse files Browse the repository at this point in the history
* fix: check whether it's undefined

* Adds correct validation, fixes errors

This adds correct validation to the form, it also fixes an issue where
we don't set interval back to undefined if it set to none. So the form
is always refilling.

* remove defaultvalues

* [autofix.ci] apply automated fixes

---------

Co-authored-by: James Perkins <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent abf169d commit f6ffe80
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export const UpdateKeyRemaining: React.FC<Props> = ({ apiKey }) => {
delete values.refill;
}
// make sure they aren't sent to the server if they are disabled.
if (values.refill?.interval !== "none" && !values.refill?.amount) {
if (
values.refill?.interval !== undefined &&
values.refill?.interval !== "none" &&
!values.refill?.amount
) {
form.setError("refill.amount", {
type: "manual",
message: "Please enter a value if interval is selected",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
limitEnabled: false,
metaEnabled: false,
ratelimitEnabled: false,
limit: {
remaining: undefined,
refill: {
interval: "none",
amount: undefined,
refillDay: undefined,
},
},
},
});

Expand Down Expand Up @@ -111,12 +119,11 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
}
const refill = values.limit?.refill;
if (refill?.interval === "daily") {
refill?.refillDay === undefined;
refill.refillDay = undefined;
}
if (refill?.interval === "monthly" && !refill.refillDay) {
refill.refillDay = 1;
}

await key.mutateAsync({
keyAuthId,
...values,
Expand Down Expand Up @@ -516,10 +523,7 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
render={({ field }) => (
<FormItem className="">
<FormLabel>Refill Rate</FormLabel>
<Select
onValueChange={field.onChange}
value={field.value || "none"}
>
<Select onValueChange={field.onChange} value={field.value}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const formSchema = z.object({
.int()
.min(1)
.max(31)
.default(1),
.optional(),
})
.optional(),
})
Expand Down

0 comments on commit f6ffe80

Please sign in to comment.