Skip to content

Commit

Permalink
fixes UI for update and create key
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelUnkey committed Jan 8, 2025
1 parent 2cb3b62 commit cd6f2a1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
import { toast } from "@/components/ui/toaster";
import { trpc } from "@/lib/trpc/client";
Expand All @@ -36,10 +43,12 @@ const formSchema = z.object({
remaining: z.coerce.number().positive({ message: "Please enter a positive number" }).optional(),
refill: z
.object({
interval: z.enum(["none", "daily", "monthly"]).optional(),
amount: z
.literal("")
.transform(() => undefined)
.or(z.coerce.number().int().positive()),
.or(z.coerce.number().int().positive())
.optional(),
refillDay: z
.literal("")
.transform(() => undefined)
Expand Down Expand Up @@ -67,9 +76,10 @@ export const UpdateKeyRemaining: React.FC<Props> = ({ apiKey }) => {
delayError: 100,
defaultValues: {
keyId: apiKey.id,
limitEnabled: Boolean(apiKey.refillAmount),
limitEnabled: Boolean(apiKey.remaining) || Boolean(apiKey.refillAmount),
remaining: apiKey.remaining ? apiKey.remaining : undefined,
refill: {
interval: apiKey.refillDay ? "monthly" : apiKey.refillAmount ? "daily" : "none",
amount: apiKey.refillAmount ? apiKey.refillAmount : undefined,
refillDay: apiKey.refillDay ?? undefined,
},
Expand All @@ -94,21 +104,33 @@ export const UpdateKeyRemaining: React.FC<Props> = ({ apiKey }) => {
});

async function onSubmit(values: z.infer<typeof formSchema>) {
if (!values.refill?.amount && values.refill?.refillDay) {
if (values.refill?.interval === "none") {
delete values.refill;
}

if (!values.refill?.amount && values.refill?.refillDay && values.limitEnabled) {
form.setError("refill.amount", {
message: "Please enter the number of uses per interval or remove the refill day",
});
return;
}
if (values.remaining === undefined) {
if (values.remaining === undefined && values.limitEnabled) {
form.setError("remaining", { message: "Please enter a value" });
return;
}
if (values.limitEnabled === false) {
if (!values.limitEnabled) {
delete values.refill;
delete values.remaining;
}
await updateRemaining.mutateAsync(values);
if (values.refill?.interval === "monthly" && !values.refill?.refillDay) {
values.refill.refillDay = 1;
}
await updateRemaining.mutateAsync({
keyId: values.keyId,
limitEnabled: values.limitEnabled,
remaining: values.remaining,
refill: values.refill,
});
}

return (
Expand Down Expand Up @@ -151,12 +173,38 @@ export const UpdateKeyRemaining: React.FC<Props> = ({ apiKey }) => {
</FormItem>
)}
/>
<FormField
control={form.control}
name="refill.interval"
disabled={
!form.watch("limitEnabled") ||
form.watch("remaining") === undefined ||
form.watch("remaining") === null
}
render={({ field }) => (
<FormItem className="">
<FormLabel>Refill Rate</FormLabel>
<Select onValueChange={field.onChange} value={field.value || "none"}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="none">None</SelectItem>
<SelectItem value="daily">Daily</SelectItem>
<SelectItem value="monthly">Monthly</SelectItem>
</SelectContent>
</Select>
<FormDescription>Interval key will be refilled.</FormDescription>
</FormItem>
)}
/>
<FormField
control={form.control}
disabled={
form.watch("remaining") === undefined ||
form.watch("remaining") === null ||
!form.watch("limitEnabled") === true
!form.watch("limitEnabled") === true ||
form.watch("refill.interval") === "none"
}
name="refill.amount"
render={({ field }) => (
Expand All @@ -183,19 +231,26 @@ export const UpdateKeyRemaining: React.FC<Props> = ({ apiKey }) => {
disabled={
form.watch("remaining") === undefined ||
form.watch("remaining") === null ||
!form.watch("limitEnabled") === true
!form.watch("limitEnabled") === true ||
form.watch("refill.interval") === "none" ||
form.watch("refill.interval") === "daily"
}
name="refill.refillDay"
render={({ field }) => (
<FormItem className="mt-4">
<FormLabel>Day of the month to refill uses.</FormLabel>
<FormControl>
<Input
placeholder="day of the month"
placeholder="1"
className="w-full"
type="number"
{...field}
value={form.getValues("limitEnabled") ? field.value : undefined}
value={
form.getValues("limitEnabled") &&
form.getValues("refill.interval") === "monthly"
? field.value
: undefined
}
/>
</FormControl>
<FormDescription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,7 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
<FormLabel>Refill Rate</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue="none"
value={field.value}
value={field.value || "none"}
>
<SelectTrigger>
<SelectValue />
Expand All @@ -527,6 +526,10 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
/>
<FormField
control={form.control}
disabled={
form.watch("limit.refill.interval") === "none" ||
form.watch("limit.refill.interval") === undefined
}
name="limit.refill.amount"
render={({ field }) => (
<FormItem className="mt-4">
Expand All @@ -538,7 +541,7 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
type="number"
{...field}
value={
form.getValues("limitEnabled") ? field.value : undefined
form.getValues("limitEnabled") ? field.value : "none"
}
/>
</FormControl>
Expand All @@ -549,7 +552,6 @@ export const CreateKey = ({ apiId, keyAuthId, defaultBytes, defaultPrefix }: Pro
</FormItem>
)}
/>

<FormField
control={form.control}
disabled={
Expand Down

0 comments on commit cd6f2a1

Please sign in to comment.