import { useEffect, useMemo } from "react"; import { Control, Controller, UseFormSetValue, useWatch } from "react-hook-form"; import { faChevronDown, faChevronRight } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { createNotification } from "@app/components/notifications"; import { Checkbox, Select, SelectItem, Td, Tr } from "@app/components/v2"; import { OrgPermissionBillingActions } from "@app/context/OrgPermissionContext/types"; import { useToggle } from "@app/hooks"; import { TFormSchema } from "../OrgRoleModifySection.utils"; const PERMISSION_ACTIONS = [ { action: OrgPermissionBillingActions.Read, label: "View bills" }, { action: OrgPermissionBillingActions.ManageBilling, label: "Manage billing" } ] as const; type Props = { isEditable: boolean; setValue: UseFormSetValue; control: Control; }; enum Permission { NoAccess = "no-access", ReadOnly = "read-only", FullAccess = "full-access", Custom = "custom" } export const OrgPermissionBillingRow = ({ isEditable, control, setValue }: Props) => { const [isRowExpanded, setIsRowExpanded] = useToggle(); const [isCustom, setIsCustom] = useToggle(); const rule = useWatch({ control, name: "permissions.billing" }); const selectedPermissionCategory = useMemo(() => { const actions = Object.keys(rule || {}) as Array; const totalActions = PERMISSION_ACTIONS.length; const score = actions.map((key) => (rule?.[key] ? 1 : 0)).reduce((a, b) => a + b, 0 as number); if (isCustom) return Permission.Custom; if (score === 0) return Permission.NoAccess; if (score === totalActions) return Permission.FullAccess; if (score === 1 && rule?.[OrgPermissionBillingActions.Read]) return Permission.ReadOnly; return Permission.Custom; }, [rule, isCustom]); useEffect(() => { if (selectedPermissionCategory === Permission.Custom) setIsCustom.on(); else setIsCustom.off(); }, [selectedPermissionCategory]); const handlePermissionChange = (val: Permission) => { if (val === Permission.Custom) { setIsRowExpanded.on(); setIsCustom.on(); return; } setIsCustom.off(); switch (val) { case Permission.NoAccess: setValue( "permissions.billing", { [OrgPermissionBillingActions.Read]: false, [OrgPermissionBillingActions.ManageBilling]: false }, { shouldDirty: true } ); break; case Permission.ReadOnly: setValue( "permissions.billing", { [OrgPermissionBillingActions.Read]: true, [OrgPermissionBillingActions.ManageBilling]: false }, { shouldDirty: true } ); break; case Permission.FullAccess: setValue( "permissions.billing", { [OrgPermissionBillingActions.Read]: true, [OrgPermissionBillingActions.ManageBilling]: true }, { shouldDirty: true } ); break; default: setValue( "permissions.billing", { [OrgPermissionBillingActions.Read]: false, [OrgPermissionBillingActions.ManageBilling]: false }, { shouldDirty: true } ); break; } }; return ( <> setIsRowExpanded.toggle()} > Billing {isRowExpanded && (
{PERMISSION_ACTIONS.map(({ action, label }) => { return ( ( { if (!isEditable) { createNotification({ type: "error", text: "Failed to update default role" }); return; } field.onChange(e); }} id={`permissions.billing.${action}`} > {label} )} /> ); })}
)} ); };