Skip to content

Commit

Permalink
Add better validation for setup keys
Browse files Browse the repository at this point in the history
  • Loading branch information
heisbrot committed Apr 16, 2024
1 parent e988e39 commit 546579f
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/modules/setup-keys/SetupKeyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type ModalProps = {
};

export function SetupKeyModalContent({ onSuccess }: ModalProps) {
const setupKeyRequest = useApiCall<SetupKey>("/setup-keys");
const setupKeyRequest = useApiCall<SetupKey>("/setup-keys", true);
const { mutate } = useSWRConfig();

const [name, setName] = useState("");
Expand All @@ -143,10 +143,18 @@ export function SetupKeyModalContent({ onSuccess }: ModalProps) {
return reusable ? "Unlimited" : "1";
}, [reusable]);

const expiresInError = useMemo(() => {
const expires = parseInt(expiresIn);
if (expires < 1 || expires > 365) {
return "Days should be between 1 and 365";
}
return "";
}, [expiresIn]);

const isDisabled = useMemo(() => {
const trimmedName = trim(name);
return trimmedName.length === 0;
}, [name]);
return trimmedName.length === 0 || expiresInError.length > 0;
}, [name, expiresInError]);

const submit = () => {
if (!selectedGroups) return;
Expand Down Expand Up @@ -245,6 +253,8 @@ export function SetupKeyModalContent({ onSuccess }: ModalProps) {
min={1}
max={365}
value={expiresIn}
error={expiresInError}
errorTooltip={true}
type={"number"}
onChange={(e) => setExpiresIn(e.target.value)}
customPrefix={
Expand Down

0 comments on commit 546579f

Please sign in to comment.