Skip to content

Commit

Permalink
UI chore: Custom OAuth apps - improve form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhopperlowe committed Oct 31, 2024
1 parent 191a5ce commit 55ec2f7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions ui/admin/app/components/oauth-apps/custom/CustomOAuthAppForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const nameSchema = z.object({
/^[a-z0-9-]+$/,
"Must contain only lowercase letters, numbers, and dashes (-)"
),
authURL: z.string().min(1, "Required"),
tokenURL: z.string().min(1, "Required"),
authURL: z.string().url("Invalid URL").min(1, "Required"),
tokenURL: z.string().url("Invalid URL").min(1, "Required"),
});

const finalSchema = nameSchema.extend({
Expand Down Expand Up @@ -80,8 +80,6 @@ export function CustomOAuthAppForm({
const isEdit = !!app;

const [step, setStep] = useState<Step>(defaultStep);
const { isFinalStep, nextLabel, prevLabel, onBack, onNext, isLoading } =
getStepInfo(step);

const defaultValues = useMemo(() => {
if (defaultData) return { ...defaultData, clientSecret: "" };
Expand All @@ -98,7 +96,9 @@ export function CustomOAuthAppForm({
if (step === Step.INFO && initialIsEdit)
// clientSecret is not required for editing
// leaving secret empty indicates that it's unchanged
return finalSchema.partial({ clientSecret: true });
return finalSchema.extend({
clientSecret: z.string(),
});

return SchemaMap[step];
};
Expand All @@ -108,6 +108,16 @@ export function CustomOAuthAppForm({
defaultValues,
});

const {
isFinalStep,
nextLabel,
prevLabel,
onBack,
onNext,
isLoading,
disableSubmit,
} = getStepInfo(step);

useEffect(() => {
form.reset(defaultValues);
}, [defaultValues, form]);
Expand Down Expand Up @@ -235,6 +245,7 @@ export function CustomOAuthAppForm({
loading={isLoading}
className="flex-1 w-full"
type="submit"
disabled={disableSubmit}
>
{nextLabel}
</Button>
Expand All @@ -251,6 +262,8 @@ export function CustomOAuthAppForm({
prevLabel: "Back",
onBack: () => setStep((prev) => (prev - 1) as Step),
isLoading: updateApp.isLoading,
disableSubmit:
!form.formState.isValid || !form.formState.isDirty,
} as const;
}

Expand Down

0 comments on commit 55ec2f7

Please sign in to comment.