mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 13:01:56 +00:00
chore: client validation when select a provider type
This commit is contained in:
@@ -16,11 +16,13 @@ import { FormMessage } from "../ui/form";
|
||||
interface RadioGroupProviderProps {
|
||||
control: Control<z.infer<typeof addProviderFormSchema>>;
|
||||
isInvalid: boolean;
|
||||
errorMessage?: string;
|
||||
}
|
||||
|
||||
export const RadioGroupProvider: React.FC<RadioGroupProviderProps> = ({
|
||||
control,
|
||||
isInvalid,
|
||||
errorMessage,
|
||||
}) => {
|
||||
return (
|
||||
<Controller
|
||||
@@ -32,6 +34,7 @@ export const RadioGroupProvider: React.FC<RadioGroupProviderProps> = ({
|
||||
className="flex flex-wrap"
|
||||
isInvalid={isInvalid}
|
||||
{...field}
|
||||
value={field.value || ""}
|
||||
>
|
||||
<div className="flex flex-col gap-4">
|
||||
<CustomRadio description="Amazon Web Services" value="aws">
|
||||
@@ -60,7 +63,11 @@ export const RadioGroupProvider: React.FC<RadioGroupProviderProps> = ({
|
||||
</CustomRadio>
|
||||
</div>
|
||||
</RadioGroup>
|
||||
<FormMessage className="text-system-error dark:text-system-error" />
|
||||
{errorMessage && (
|
||||
<FormMessage className="text-system-error dark:text-system-error">
|
||||
{errorMessage}
|
||||
</FormMessage>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
/>
|
||||
|
||||
@@ -21,13 +21,14 @@ export type FormValues = z.infer<typeof addProviderFormSchema>;
|
||||
export const ConnectAccountForm = () => {
|
||||
const { toast } = useToast();
|
||||
const [prevStep, setPrevStep] = useState(1);
|
||||
const router = useRouter();
|
||||
|
||||
const formSchema = addProviderFormSchema;
|
||||
|
||||
const form = useForm<FormValues>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
providerType: "",
|
||||
providerType: undefined,
|
||||
providerId: "",
|
||||
providerAlias: "",
|
||||
awsCredentialsType: "",
|
||||
@@ -36,9 +37,8 @@ export const ConnectAccountForm = () => {
|
||||
const providerType = form.watch("providerType");
|
||||
const isLoading = form.formState.isSubmitting;
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const onSubmitClient = async (values: FormValues) => {
|
||||
console.log({ values });
|
||||
const formData = new FormData();
|
||||
|
||||
Object.entries(values).forEach(
|
||||
@@ -46,7 +46,6 @@ export const ConnectAccountForm = () => {
|
||||
);
|
||||
|
||||
const data = await addProvider(formData);
|
||||
console.log(data);
|
||||
|
||||
if (data?.errors && data.errors.length > 0) {
|
||||
data.errors.forEach((error: ApiError) => {
|
||||
@@ -109,6 +108,7 @@ export const ConnectAccountForm = () => {
|
||||
<RadioGroupProvider
|
||||
control={form.control}
|
||||
isInvalid={!!form.formState.errors.providerType}
|
||||
errorMessage={form.formState.errors.providerType?.message}
|
||||
/>
|
||||
{/* Provider UID */}
|
||||
<CustomInput
|
||||
@@ -140,7 +140,11 @@ export const ConnectAccountForm = () => {
|
||||
{prevStep === 2 && (
|
||||
<>
|
||||
{/* Select AWS credentials type */}
|
||||
<RadioGroupAWSViaCredentialsForm control={form.control} />
|
||||
<RadioGroupAWSViaCredentialsForm
|
||||
control={form.control}
|
||||
isInvalid={!!form.formState.errors.awsCredentialsType}
|
||||
errorMessage={form.formState.errors.awsCredentialsType?.message}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
|
||||
@@ -5,15 +5,20 @@ import React from "react";
|
||||
import { Control, Controller } from "react-hook-form";
|
||||
|
||||
import { CustomRadio } from "@/components/ui/custom";
|
||||
import { FormMessage } from "@/components/ui/form";
|
||||
|
||||
import { FormValues } from "./connect-account-form";
|
||||
|
||||
type RadioGroupAWSViaCredentialsFormProps = {
|
||||
control: Control<FormValues>;
|
||||
isInvalid: boolean;
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
export const RadioGroupAWSViaCredentialsForm = ({
|
||||
control,
|
||||
isInvalid,
|
||||
errorMessage,
|
||||
}: RadioGroupAWSViaCredentialsFormProps) => {
|
||||
return (
|
||||
<Controller
|
||||
@@ -21,7 +26,12 @@ export const RadioGroupAWSViaCredentialsForm = ({
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<>
|
||||
<RadioGroup className="flex flex-wrap" {...field}>
|
||||
<RadioGroup
|
||||
className="flex flex-wrap"
|
||||
isInvalid={isInvalid}
|
||||
{...field}
|
||||
value={field.value || ""}
|
||||
>
|
||||
<div className="flex flex-col gap-4">
|
||||
<span className="text-sm text-default-500">Using IAM Role</span>
|
||||
<CustomRadio
|
||||
@@ -53,6 +63,11 @@ export const RadioGroupAWSViaCredentialsForm = ({
|
||||
</CustomRadio>
|
||||
</div>
|
||||
</RadioGroup>
|
||||
{errorMessage && (
|
||||
<FormMessage className="text-system-error dark:text-system-error">
|
||||
{errorMessage}
|
||||
</FormMessage>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
/>
|
||||
|
||||
+36
-6
@@ -31,12 +31,42 @@ export const scheduleScanFormSchema = () =>
|
||||
scheduleDate: z.string(),
|
||||
});
|
||||
|
||||
export const addProviderFormSchema = z.object({
|
||||
providerType: z.string(),
|
||||
providerAlias: z.string(),
|
||||
providerId: z.string(),
|
||||
awsCredentialsType: z.string().optional(),
|
||||
});
|
||||
export const addProviderFormSchema = z
|
||||
.object({
|
||||
providerType: z.enum(["aws", "azure", "gcp", "kubernetes"], {
|
||||
required_error: "Please select a provider type",
|
||||
}),
|
||||
})
|
||||
.and(
|
||||
z.discriminatedUnion("providerType", [
|
||||
z.object({
|
||||
providerType: z.literal("aws"),
|
||||
providerAlias: z.string(),
|
||||
providerId: z.string(),
|
||||
awsCredentialsType: z.string().min(1, {
|
||||
message: "Please select the type of credentials you want to use",
|
||||
}),
|
||||
}),
|
||||
z.object({
|
||||
providerType: z.literal("azure"),
|
||||
providerAlias: z.string(),
|
||||
providerId: z.string(),
|
||||
awsCredentialsType: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
providerType: z.literal("gcp"),
|
||||
providerAlias: z.string(),
|
||||
providerId: z.string(),
|
||||
awsCredentialsType: z.string().optional(),
|
||||
}),
|
||||
z.object({
|
||||
providerType: z.literal("kubernetes"),
|
||||
providerAlias: z.string(),
|
||||
providerId: z.string(),
|
||||
awsCredentialsType: z.string().optional(),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
|
||||
export const addCredentialsFormSchema = (providerType: string) =>
|
||||
z.object({
|
||||
|
||||
Reference in New Issue
Block a user