feat: add action to getTask and implement the last step in the workflow - launch scan

This commit is contained in:
Pablo Lara
2024-11-03 11:31:47 +01:00
parent ff9d5442ab
commit 258d18112c
7 changed files with 186 additions and 1 deletions
+1
View File
@@ -0,0 +1 @@
export * from "./tasks";
+24
View File
@@ -0,0 +1,24 @@
"use server";
import { auth } from "@/auth.config";
import { getErrorMessage, parseStringify } from "@/lib";
export const getTask = async (taskId: string) => {
const session = await auth();
const keyServer = process.env.API_BASE_URL;
const url = new URL(`${keyServer}/tasks/${taskId}`);
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
});
const data = await response.json();
return parseStringify(data);
} catch (error) {
return { error: getErrorMessage(error) };
}
};
@@ -0,0 +1,33 @@
import { redirect } from "next/navigation";
import React from "react";
import { getProvider } from "@/actions/providers";
import { LaunchScanForm } from "@/components/providers/workflow/forms";
interface Props {
searchParams: { type: string; id: string; connected: boolean };
}
export default async function LaunchScanPage({ searchParams }: Props) {
const providerId = searchParams.id;
const connectedSearchParam = searchParams.connected;
if (!providerId) {
redirect("/providers/connect-account");
}
const formData = new FormData();
formData.append("id", providerId);
const providerData = await getProvider(formData);
const isConnected = providerData?.data?.attributes?.connection?.connected;
if (!isConnected || connectedSearchParam !== isConnected.toString()) {
redirect("/providers/connect-account");
}
return (
<LaunchScanForm searchParams={searchParams} providerData={providerData} />
);
}
@@ -1,4 +1,5 @@
export * from "./connect-account-form";
export * from "./launch-scan-form";
export * from "./radio-group-aws-via-credentials-form";
export * from "./test-connection-form";
export * from "./via-credentials-form";
@@ -0,0 +1,113 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { SaveIcon } from "@/components/icons";
// import { useToast } from "@/components/ui";
import { CustomButton } from "@/components/ui/custom";
import { Form } from "@/components/ui/form";
import { ProviderProps } from "@/types"; // Asegúrate de importar la interfaz correcta
import { launchScanFormSchema } from "@/types/formSchemas";
import { ProviderInfo } from "../../provider-info";
type FormValues = z.infer<ReturnType<typeof launchScanFormSchema>>;
interface LaunchScanFormProps {
searchParams: { type: string; id: string; connected: boolean };
providerData: {
data: {
type: string;
id: string;
attributes: ProviderProps["attributes"];
};
};
}
export const LaunchScanForm = ({
searchParams,
providerData,
}: LaunchScanFormProps) => {
const providerType = searchParams.type;
const providerId = searchParams.id;
const connected = searchParams.connected;
// const [apiErrorMessage, setApiErrorMessage] = useState<string | null>(null);
const formSchema = launchScanFormSchema();
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
providerId,
providerType,
scannerArgs: {
checksToExecute: [],
},
},
});
console.log({ providerData });
const isLoading = form.formState.isSubmitting;
const onSubmitClient = async (values: FormValues) => {
console.log({ values }, "values from test connection form");
if (connected) {
console.log("connected");
} else {
console.log("not connected");
}
};
return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmitClient)}
className="flex flex-col space-y-4"
>
<div className="text-left">
<div className="text-2xl font-bold leading-9 text-default-foreground">
Launch scan
</div>
<div className="py-2 text-default-500">
Launch the scan now or schedule it for a later date and time.
</div>
</div>
{/* {apiErrorMessage && (
<div className="mt-4 rounded-md bg-red-100 p-3 text-red-700">
<p>{`Provider ID ${apiErrorMessage.toLowerCase()}. Please check and try again.`}</p>
</div>
)} */}
<ProviderInfo
connected={providerData.data.attributes.connection.connected}
provider={providerData.data.attributes.provider}
providerAlias={providerData.data.attributes.alias}
/>
<input type="hidden" name="providerId" value={providerId} />
<input type="hidden" name="providerType" value={providerType} />
<div className="flex w-full justify-end sm:space-x-6">
<CustomButton
type="submit"
ariaLabel={"Save"}
className="w-1/2"
variant="solid"
color="action"
size="lg"
isLoading={isLoading}
startContent={!isLoading && <SaveIcon size={24} />}
>
{isLoading ? <>Loading</> : <span>Launch scan</span>}
</CustomButton>
</div>
</form>
</Form>
);
};
@@ -152,7 +152,9 @@ export const ViaCredentialsForm = ({
}
});
} else {
router.push(`/providers/test-connection?id=${providerId}`);
router.push(
`/providers/test-connection?type=${providerType}&id=${providerId}`,
);
}
};
+11
View File
@@ -108,6 +108,17 @@ export const testConnectionFormSchema = z.object({
providerId: z.string(),
});
export const launchScanFormSchema = () =>
z.object({
providerId: z.string(),
providerType: z.string(),
scannerArgs: z
.object({
checksToExecute: z.array(z.string()).optional(),
})
.optional(),
});
export const editProviderFormSchema = (currentAlias: string) =>
z.object({
alias: z