-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add permissions form and permission table
Fixes #2246
- Loading branch information
1 parent
d650be1
commit 3745f16
Showing
16 changed files
with
619 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useGetPlaybookNames } from "@flanksource-ui/api/query-hooks/playbooks"; | ||
import PlaybookSpecIcon from "@flanksource-ui/components/Playbooks/Settings/PlaybookSpecIcon"; | ||
import { useMemo } from "react"; | ||
import FormikSelectDropdown from "./FormikSelectDropdown"; | ||
|
||
type FormikPlaybooksDropdownProps = { | ||
name: string; | ||
label?: string; | ||
required?: boolean; | ||
hint?: string; | ||
className?: string; | ||
}; | ||
|
||
export default function FormikPlaybooksDropdown({ | ||
name, | ||
label, | ||
required = false, | ||
hint, | ||
className = "flex flex-col space-y-2 py-2" | ||
}: FormikPlaybooksDropdownProps) { | ||
const { isLoading, data: checks } = useGetPlaybookNames(); | ||
|
||
const options = useMemo( | ||
() => | ||
checks?.map((playbook) => ({ | ||
label: playbook.title || playbook.name, | ||
value: playbook.id, | ||
icon: <PlaybookSpecIcon playbook={playbook} /> | ||
})), | ||
[checks] | ||
); | ||
|
||
return ( | ||
<FormikSelectDropdown | ||
name={name} | ||
className={className} | ||
options={options} | ||
label={label} | ||
isLoading={isLoading} | ||
required={required} | ||
hint={hint} | ||
/> | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/components/Permissions/ManagePermissions/Forms/AddPermissionButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useState } from "react"; | ||
import { AiFillPlusCircle } from "react-icons/ai"; | ||
import PermissionForm from "./PermissionForm"; | ||
|
||
export default function AddPermissionButton() { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<button type="button" className="" onClick={() => setIsOpen(true)}> | ||
<AiFillPlusCircle size={32} className="text-blue-600" /> | ||
</button> | ||
<PermissionForm isOpen={isOpen} onClose={() => setIsOpen(false)} /> | ||
</> | ||
); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/components/Permissions/ManagePermissions/Forms/DeletePermission.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { deletePermission } from "@flanksource-ui/api/services/permissions"; | ||
import { | ||
toastError, | ||
toastSuccess | ||
} from "@flanksource-ui/components/Toast/toast"; | ||
import { ConfirmationPromptDialog } from "@flanksource-ui/ui/AlertDialog/ConfirmationPromptDialog"; | ||
import { Button } from "@flanksource-ui/ui/Buttons/Button"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { AxiosError } from "axios"; | ||
import { useCallback, useState } from "react"; | ||
import { FaCircleNotch, FaTrash } from "react-icons/fa"; | ||
|
||
export default function DeletePermission({ | ||
permissionId, | ||
onDeleted = () => {} | ||
}: { | ||
permissionId: string; | ||
onDeleted: () => void; | ||
}) { | ||
const [isConfirmDialogOpen, setIsConfirmDialogOpen] = useState(false); | ||
|
||
const { mutate: deleteResource, isLoading } = useMutation({ | ||
mutationFn: async (id: string) => { | ||
const res = await deletePermission(id); | ||
return res.data; | ||
}, | ||
onSuccess: (_) => { | ||
toastSuccess("Permission deleted"); | ||
onDeleted(); | ||
}, | ||
onError: (error: AxiosError) => { | ||
toastError(error.message); | ||
} | ||
}); | ||
|
||
const onDeleteResource = useCallback(() => { | ||
setIsConfirmDialogOpen(false); | ||
deleteResource(permissionId); | ||
}, [deleteResource, permissionId]); | ||
|
||
return ( | ||
<> | ||
<Button | ||
text="Delete" | ||
disabled={isLoading} | ||
icon={ | ||
!isLoading ? <FaTrash /> : <FaCircleNotch className="animate-spin" /> | ||
} | ||
className="btn-danger" | ||
onClick={() => setIsConfirmDialogOpen(true)} | ||
/> | ||
|
||
{isConfirmDialogOpen && ( | ||
<ConfirmationPromptDialog | ||
title="Delete Permission" | ||
description="Are you sure you want to permission?" | ||
onConfirm={onDeleteResource} | ||
isOpen={isConfirmDialogOpen} | ||
onClose={() => setIsConfirmDialogOpen(false)} | ||
className="z-[9999]" | ||
/> | ||
)} | ||
</> | ||
); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/components/Permissions/ManagePermissions/Forms/FormikPermissionSelectResourceFields.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import FormikCanaryDropdown from "@flanksource-ui/components/Forms/Formik/FormikCanaryDropdown"; | ||
import FormikConnectionField from "@flanksource-ui/components/Forms/Formik/FormikConnectionField"; | ||
import FormikPlaybooksDropdown from "@flanksource-ui/components/Forms/Formik/FormikPlaybooksDropdown"; | ||
import FormikResourceSelectorDropdown from "@flanksource-ui/components/Forms/Formik/FormikResourceSelectorDropdown"; | ||
import { Switch } from "@flanksource-ui/ui/FormControls/Switch"; | ||
import { useFormikContext } from "formik"; | ||
import { useState } from "react"; | ||
|
||
export default function FormikPermissionSelectResourceFields() { | ||
const { setFieldValue } = useFormikContext<Record<string, any>>(); | ||
|
||
const [switchOption, setSwitchOption] = useState< | ||
"Component" | "Catalog" | "Canary" | "Playbook" | "Connection" | ||
>("Catalog"); | ||
|
||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<label className={`form-label`}>Resource</label> | ||
<div> | ||
<div className="flex w-full flex-row"> | ||
<Switch | ||
options={[ | ||
"Catalog", | ||
"Component", | ||
"Canary", | ||
"Connection", | ||
"Playbook" | ||
]} | ||
className="w-auto" | ||
itemsClassName="" | ||
defaultValue="Go Template" | ||
value={switchOption} | ||
onChange={(v) => { | ||
setSwitchOption(v); | ||
setFieldValue("config_id", undefined); | ||
setFieldValue("check_id", undefined); | ||
setFieldValue("canary_id", undefined); | ||
setFieldValue("component_id", undefined); | ||
setFieldValue("playbook_id", undefined); | ||
}} | ||
/> | ||
</div> | ||
|
||
{switchOption === "Catalog" && ( | ||
<FormikResourceSelectorDropdown | ||
required | ||
name="config_id" | ||
configResourceSelector={[{}]} | ||
/> | ||
)} | ||
|
||
{switchOption === "Component" && ( | ||
<FormikResourceSelectorDropdown | ||
required | ||
name="component_id" | ||
componentResourceSelector={[{}]} | ||
/> | ||
)} | ||
|
||
{switchOption === "Playbook" && ( | ||
<FormikPlaybooksDropdown required name="playbook_id" /> | ||
)} | ||
|
||
{switchOption === "Canary" && ( | ||
<FormikCanaryDropdown required name="canary_id" /> | ||
)} | ||
|
||
{switchOption === "Connection" && ( | ||
<FormikConnectionField required name="connection_id" /> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.