import { Controller, useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { createNotification } from "@app/components/notifications"; import { Button, FormControl, Modal, ModalContent, Select, SelectItem } from "@app/components/v2"; import { useProject } from "@app/context"; import { CaStatus, useAddItemToPkiCollection, useListWorkspaceCas } from "@app/hooks/api"; import { PkiItemType, pkiItemTypeToNameMap } from "@app/hooks/api/pkiCollections/constants"; import { useListWorkspaceCertificates } from "@app/hooks/api/projects"; import { UsePopUpState } from "@app/hooks/usePopUp"; const schema = z .object({ // type: z.nativeEnum(PkiItemType), itemId: z.string() }) .required(); type FormData = z.infer; type Props = { collectionId: string; type: PkiItemType; popUp: UsePopUpState<["addPkiCollectionItem"]>; handlePopUpToggle: ( popUpName: keyof UsePopUpState<["addPkiCollectionItem"]>, state?: boolean ) => void; }; // note: this component should be optimized so it is easier // to find certificates and CAs export const AddPkiCollectionItemModal = ({ collectionId, type, popUp, handlePopUpToggle }: Props) => { const { currentProject } = useProject(); const { data: cas } = useListWorkspaceCas({ projectId: currentProject?.id || "", status: CaStatus.ACTIVE }); const { data } = useListWorkspaceCertificates({ projectId: currentProject?.slug || "", offset: 0, limit: 25 }); const { mutateAsync: addItemToPkiCollection } = useAddItemToPkiCollection(); const { control, handleSubmit, reset, formState: { isSubmitting } } = useForm({ resolver: zodResolver(schema) // defaultValues: { // type: PkiItemType.CA // } }); const onFormSubmit = async ({ itemId }: FormData) => { try { const item = await addItemToPkiCollection({ collectionId, type, itemId }); createNotification({ text: `Successfully added ${ pkiItemTypeToNameMap[item.type as PkiItemType] } to PKI collection`, type: "success" }); reset(); handlePopUpToggle("addPkiCollectionItem", false); } catch (err) { console.error(err); } }; return ( { handlePopUpToggle("addPkiCollectionItem", isOpen); reset(); }} >
{/* ( )} /> */} {type === PkiItemType.CA && ( ( )} /> )} {type === PkiItemType.CERTIFICATE && ( ( )} /> )}
); };