1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/component/common/PermissionButton/PermissionButton.tsx

84 lines
2.3 KiB
TypeScript
Raw Normal View History

feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
import { Button, ButtonProps, Tooltip } from '@material-ui/core';
import { Lock } from '@material-ui/icons';
import AccessContext from 'contexts/AccessContext';
refactor: fix misc TS errors (#729) * refactor: update test deps * refactor: remove unused ts-expect-error annotations * refactor: add missing arg and return types * refactor: the loading prop is optional * refactor: add missing arg and return types * reafactor: fix value arg type * refactor: fix missing array type * refactor: the parameters field is an array * refactor: use undefined instead of null in state * refactor: add missing params type * refactor: add missing children prop * refactor: add missing array type * refactor: add missing React imports * refactor: use correct IProjectEnvironment type * refactor: type errors as unknown * refactor: the index prop is required * refactor: fix date prop type * refactor: fix tooltip placement prop type * refactor: fix environments state type * refactor: add missing arg types * refactor: add guard for undefined field * refactor: fix ChangePassword prop types * refactor: fix MUI import paths * refactor: add missing arg type * refactor: fix showDialog prop type * refactor: remove unused openUpdateDialog prop * refactor: add missing non-null assertion * refactor: remove unused types prop * refactor: stricten API error handler types * refactor: add missing undefined check * refactor: add missing IProject id field * refactor: fix ConditionallyRender condition prop types * refactor: remove unused args * refactor: add AddVariant prop types * refactor: add types to UIContext * refactor: fix event arg type * refactor: add missing default impressionData field * refactor: fix handleDeleteEnvironment prop args * refactor: fix IFeatureMetrics field requirements * refactor: add missing element types to ConditionallyRender * refactor: remove unused ProjectAccess projectId prop * refactor: add missing undefined check * refactor: fix getCreateTogglePath arg type * refactor: add missing IStrategyPayload import * refactor: remove unused user arg * refactor: add missing event arg type * refactor: add missing style object types * refactor: improve userApiErrors prop type * refactor: the Dialogue onClose prop is optional * refactor: fix the AddonEvents setEventValue prop type
2022-02-25 10:55:39 +01:00
import React, { useContext } from 'react';
import ConditionallyRender from '../ConditionallyRender';
export interface IPermissionButtonProps extends ButtonProps {
feat/rbac roles (#562) * feat: create screen * fix: import accordion summary * feat: add accordions * fix: add codebox * feat: select permissions * fix: permission checker * fix: update permission checker * feat: wire up role list * fix: change icon color in project roles list * fix: add color to icon in project roles * add confirm dialog on role deletion * feat: add created screen * fix: cleanup * fix: update access permissions * fix: update admin panel * feat: add edit screen * fix: use color from palette and show toast when fails * fix: refactor * feat: validation * feat: implement checked all * fix: experimental toast * fix: error handling * fix: toast * feat: unique name validation * fix: update toasts * fix: remove toast * fix: reset flag * fix: remove unused vars * fix: update tests * feat: add error icon for toast * fix: replace wrong import for setToastData * feat: Patch keying on ui to handle uniqueness for permissions across multiple envs * fix: hasAccess handles * * fix: update permission switch * fix: use flag for environments rbac * fix: do not include check all keys in payload * fix: filter roles * fix: account for new permissions in variants list * fix: use effect on length property * fix: set polling interval on user * 4.5.0-beta.0 * fix: set initial permissions correctly to avoid race condition * fix: handle activeEnvironment when it is null * fix: remove unused imports * fix: unused imports * fix: Include missing project in hasAccess for deleteinng a tag * fix: Move add/delete tag to use update feature permissions * fix: use rest parameter * fix: remove sandbox from scripts * 4.6.0-beta.1 * fix: remove loading deduping * fix: disable editing on builtin roles * fix: check all * fix: feature overview environment * fix: refetch user on project create * fix: update snaphots * fix: frontend permissions * fix: delete create confirm * fix: remove unused permission * 4.6.0-beta.4 * fix: update permissions * fix: permissions * fix: set error to string * 4.6.0-beta.5 * fix: add permissions for project view * fix: add permissions to useEffect deps * fix: update permission for move feature toggle * fix: add permissions data to useEffect * fix: move settings * fix: key on confetti * fix: refetch project permissions on environment create/delete * fix: optional coalescing error object * fix: remove logging error * fix: reorder disable importance in permissionbutton * fix: add project roles to menu * fix: add disabled check to revive * fix: update snapshots * fix: change text to select all * fix: change text to select * 4.6.0-beta.6 Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: sighphyre <liquidwicked64@gmail.com>
2022-01-14 15:50:02 +01:00
permission: string | string[];
onClick?: (e: any) => void;
disabled?: boolean;
projectId?: string;
feat/rbac roles (#562) * feat: create screen * fix: import accordion summary * feat: add accordions * fix: add codebox * feat: select permissions * fix: permission checker * fix: update permission checker * feat: wire up role list * fix: change icon color in project roles list * fix: add color to icon in project roles * add confirm dialog on role deletion * feat: add created screen * fix: cleanup * fix: update access permissions * fix: update admin panel * feat: add edit screen * fix: use color from palette and show toast when fails * fix: refactor * feat: validation * feat: implement checked all * fix: experimental toast * fix: error handling * fix: toast * feat: unique name validation * fix: update toasts * fix: remove toast * fix: reset flag * fix: remove unused vars * fix: update tests * feat: add error icon for toast * fix: replace wrong import for setToastData * feat: Patch keying on ui to handle uniqueness for permissions across multiple envs * fix: hasAccess handles * * fix: update permission switch * fix: use flag for environments rbac * fix: do not include check all keys in payload * fix: filter roles * fix: account for new permissions in variants list * fix: use effect on length property * fix: set polling interval on user * 4.5.0-beta.0 * fix: set initial permissions correctly to avoid race condition * fix: handle activeEnvironment when it is null * fix: remove unused imports * fix: unused imports * fix: Include missing project in hasAccess for deleteinng a tag * fix: Move add/delete tag to use update feature permissions * fix: use rest parameter * fix: remove sandbox from scripts * 4.6.0-beta.1 * fix: remove loading deduping * fix: disable editing on builtin roles * fix: check all * fix: feature overview environment * fix: refetch user on project create * fix: update snaphots * fix: frontend permissions * fix: delete create confirm * fix: remove unused permission * 4.6.0-beta.4 * fix: update permissions * fix: permissions * fix: set error to string * 4.6.0-beta.5 * fix: add permissions for project view * fix: add permissions to useEffect deps * fix: update permission for move feature toggle * fix: add permissions data to useEffect * fix: move settings * fix: key on confetti * fix: refetch project permissions on environment create/delete * fix: optional coalescing error object * fix: remove logging error * fix: reorder disable importance in permissionbutton * fix: add project roles to menu * fix: add disabled check to revive * fix: update snapshots * fix: change text to select all * fix: change text to select * 4.6.0-beta.6 Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: sighphyre <liquidwicked64@gmail.com>
2022-01-14 15:50:02 +01:00
environmentId?: string;
}
const PermissionButton: React.FC<IPermissionButtonProps> = ({
permission,
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
variant = 'contained',
color = 'primary',
onClick,
children,
disabled,
projectId,
feat/rbac roles (#562) * feat: create screen * fix: import accordion summary * feat: add accordions * fix: add codebox * feat: select permissions * fix: permission checker * fix: update permission checker * feat: wire up role list * fix: change icon color in project roles list * fix: add color to icon in project roles * add confirm dialog on role deletion * feat: add created screen * fix: cleanup * fix: update access permissions * fix: update admin panel * feat: add edit screen * fix: use color from palette and show toast when fails * fix: refactor * feat: validation * feat: implement checked all * fix: experimental toast * fix: error handling * fix: toast * feat: unique name validation * fix: update toasts * fix: remove toast * fix: reset flag * fix: remove unused vars * fix: update tests * feat: add error icon for toast * fix: replace wrong import for setToastData * feat: Patch keying on ui to handle uniqueness for permissions across multiple envs * fix: hasAccess handles * * fix: update permission switch * fix: use flag for environments rbac * fix: do not include check all keys in payload * fix: filter roles * fix: account for new permissions in variants list * fix: use effect on length property * fix: set polling interval on user * 4.5.0-beta.0 * fix: set initial permissions correctly to avoid race condition * fix: handle activeEnvironment when it is null * fix: remove unused imports * fix: unused imports * fix: Include missing project in hasAccess for deleteinng a tag * fix: Move add/delete tag to use update feature permissions * fix: use rest parameter * fix: remove sandbox from scripts * 4.6.0-beta.1 * fix: remove loading deduping * fix: disable editing on builtin roles * fix: check all * fix: feature overview environment * fix: refetch user on project create * fix: update snaphots * fix: frontend permissions * fix: delete create confirm * fix: remove unused permission * 4.6.0-beta.4 * fix: update permissions * fix: permissions * fix: set error to string * 4.6.0-beta.5 * fix: add permissions for project view * fix: add permissions to useEffect deps * fix: update permission for move feature toggle * fix: add permissions data to useEffect * fix: move settings * fix: key on confetti * fix: refetch project permissions on environment create/delete * fix: optional coalescing error object * fix: remove logging error * fix: reorder disable importance in permissionbutton * fix: add project roles to menu * fix: add disabled check to revive * fix: update snapshots * fix: change text to select all * fix: change text to select * 4.6.0-beta.6 Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: sighphyre <liquidwicked64@gmail.com>
2022-01-14 15:50:02 +01:00
environmentId,
...rest
}) => {
const { hasAccess } = useContext(AccessContext);
feat/rbac roles (#562) * feat: create screen * fix: import accordion summary * feat: add accordions * fix: add codebox * feat: select permissions * fix: permission checker * fix: update permission checker * feat: wire up role list * fix: change icon color in project roles list * fix: add color to icon in project roles * add confirm dialog on role deletion * feat: add created screen * fix: cleanup * fix: update access permissions * fix: update admin panel * feat: add edit screen * fix: use color from palette and show toast when fails * fix: refactor * feat: validation * feat: implement checked all * fix: experimental toast * fix: error handling * fix: toast * feat: unique name validation * fix: update toasts * fix: remove toast * fix: reset flag * fix: remove unused vars * fix: update tests * feat: add error icon for toast * fix: replace wrong import for setToastData * feat: Patch keying on ui to handle uniqueness for permissions across multiple envs * fix: hasAccess handles * * fix: update permission switch * fix: use flag for environments rbac * fix: do not include check all keys in payload * fix: filter roles * fix: account for new permissions in variants list * fix: use effect on length property * fix: set polling interval on user * 4.5.0-beta.0 * fix: set initial permissions correctly to avoid race condition * fix: handle activeEnvironment when it is null * fix: remove unused imports * fix: unused imports * fix: Include missing project in hasAccess for deleteinng a tag * fix: Move add/delete tag to use update feature permissions * fix: use rest parameter * fix: remove sandbox from scripts * 4.6.0-beta.1 * fix: remove loading deduping * fix: disable editing on builtin roles * fix: check all * fix: feature overview environment * fix: refetch user on project create * fix: update snaphots * fix: frontend permissions * fix: delete create confirm * fix: remove unused permission * 4.6.0-beta.4 * fix: update permissions * fix: permissions * fix: set error to string * 4.6.0-beta.5 * fix: add permissions for project view * fix: add permissions to useEffect deps * fix: update permission for move feature toggle * fix: add permissions data to useEffect * fix: move settings * fix: key on confetti * fix: refetch project permissions on environment create/delete * fix: optional coalescing error object * fix: remove logging error * fix: reorder disable importance in permissionbutton * fix: add project roles to menu * fix: add disabled check to revive * fix: update snapshots * fix: change text to select all * fix: change text to select * 4.6.0-beta.6 Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: sighphyre <liquidwicked64@gmail.com>
2022-01-14 15:50:02 +01:00
let access;
feat/rbac roles (#562) * feat: create screen * fix: import accordion summary * feat: add accordions * fix: add codebox * feat: select permissions * fix: permission checker * fix: update permission checker * feat: wire up role list * fix: change icon color in project roles list * fix: add color to icon in project roles * add confirm dialog on role deletion * feat: add created screen * fix: cleanup * fix: update access permissions * fix: update admin panel * feat: add edit screen * fix: use color from palette and show toast when fails * fix: refactor * feat: validation * feat: implement checked all * fix: experimental toast * fix: error handling * fix: toast * feat: unique name validation * fix: update toasts * fix: remove toast * fix: reset flag * fix: remove unused vars * fix: update tests * feat: add error icon for toast * fix: replace wrong import for setToastData * feat: Patch keying on ui to handle uniqueness for permissions across multiple envs * fix: hasAccess handles * * fix: update permission switch * fix: use flag for environments rbac * fix: do not include check all keys in payload * fix: filter roles * fix: account for new permissions in variants list * fix: use effect on length property * fix: set polling interval on user * 4.5.0-beta.0 * fix: set initial permissions correctly to avoid race condition * fix: handle activeEnvironment when it is null * fix: remove unused imports * fix: unused imports * fix: Include missing project in hasAccess for deleteinng a tag * fix: Move add/delete tag to use update feature permissions * fix: use rest parameter * fix: remove sandbox from scripts * 4.6.0-beta.1 * fix: remove loading deduping * fix: disable editing on builtin roles * fix: check all * fix: feature overview environment * fix: refetch user on project create * fix: update snaphots * fix: frontend permissions * fix: delete create confirm * fix: remove unused permission * 4.6.0-beta.4 * fix: update permissions * fix: permissions * fix: set error to string * 4.6.0-beta.5 * fix: add permissions for project view * fix: add permissions to useEffect deps * fix: update permission for move feature toggle * fix: add permissions data to useEffect * fix: move settings * fix: key on confetti * fix: refetch project permissions on environment create/delete * fix: optional coalescing error object * fix: remove logging error * fix: reorder disable importance in permissionbutton * fix: add project roles to menu * fix: add disabled check to revive * fix: update snapshots * fix: change text to select all * fix: change text to select * 4.6.0-beta.6 Co-authored-by: Fredrik Oseberg <fredrik.no@gmail.com> Co-authored-by: sighphyre <liquidwicked64@gmail.com>
2022-01-14 15:50:02 +01:00
const handleAccess = () => {
let access;
if (Array.isArray(permission)) {
access = permission.some(permission => {
if (projectId && environmentId) {
return hasAccess(permission, projectId, environmentId);
} else if (projectId) {
return hasAccess(permission, projectId);
} else {
return hasAccess(permission);
}
});
} else {
if (projectId && environmentId) {
access = hasAccess(permission, projectId, environmentId);
} else if (projectId) {
access = hasAccess(permission, projectId);
} else {
access = hasAccess(permission);
}
}
return access;
};
access = handleAccess();
2022-02-23 13:45:37 +01:00
const tooltipText = !access
? "You don't have access to perform this operation"
: '';
return (
<Tooltip title={tooltipText} arrow>
<span>
<Button
onClick={onClick}
disabled={disabled || !access}
feat: add new feature strategy create/edit pages (#739) * refactor: add param helper hooks * refactor: remove first add strategy link * refactor: add more types to useStrategies * refactor: port strategy utils to TS * refactor: replace rollout strategy icon * refactor: use a named export for useFeature * refactor: add more types to useFeature * refactor: adjust code box styles * refactor: add missing PermissionButton variant prop * refactor: add missing button icon label * refactor: move common feature components * refactor: fix StrategyConstraints error prop types * refactor: fix GeneralStrategy prop types * feat: add new feature strategy create/edit pages * refactor: remove feature strategies page * refactor: fix types in GeneralStrategy * refactor: use ConstraintAccordion on the new pages * refactor: use ConditionallyRender for remove button * refactor: rename FeatureStrategyForm component * refactor: use the Edit icon for feature strategies * refactor: fix initial edit mode for new constraints * refactor: add FeatureStrategyMenu to closed accordions * refactor: allow editing multiple constraints * refactor: show single-valued constraint value * refactor: increase feature overview strategy width * refactor: add remove button to feature overview strategies * refactor: move createEmptyConstraint to own file * refactor: disable submit button for invalid constraints * refactor: fix nested paragraphs on the metrics page * refactor: move create/edit feature strategy to modal * refactor: always open new constraints in edit mode * refactor: use a PermissionButton for the save button * refactor: remvoe unsaved constraints on cancel * refactor: clarify useConstraintsValidation logic * refactor: remove unused strategy descriptions * refactor: restore Rollout icon * refactor: remove sidebar modal slide animation * refactor: avoid constraint accordion toggle on edit/delete * refactor: truncate long strategy names * refactor: find the correct remove button
2022-03-09 14:59:24 +01:00
variant={variant}
color={color}
{...rest}
endIcon={
<ConditionallyRender
condition={!access}
show={<Lock />}
/>
}
>
{children}
</Button>
</span>
</Tooltip>
);
};
export default PermissionButton;