import React from "react"; import { WithTranslation, withTranslation } from "react-i18next"; import { PermissionCollection, PermissionCreateEntry, RepositoryRole, SelectValue } from "@scm-manager/ui-types"; import { Button, GroupAutocomplete, LabelWithHelpIcon, Radio, Level, SubmitButton, Subtitle, UserAutocomplete } from "@scm-manager/ui-components"; import * as validator from "../components/permissionValidation"; import RoleSelector from "../components/RoleSelector"; import AdvancedPermissionsDialog from "./AdvancedPermissionsDialog"; import { findVerbsForRole } from "../modules/permissions"; type Props = WithTranslation & { availableRoles: RepositoryRole[]; availableVerbs: string[]; createPermission: (permission: PermissionCreateEntry) => void; loading: boolean; currentPermissions: PermissionCollection; groupAutocompleteLink: string; userAutocompleteLink: string; }; type State = { name: string; role?: string; verbs?: string[]; groupPermission: boolean; valid: boolean; value?: SelectValue; showAdvancedDialog: boolean; }; class CreatePermissionForm extends React.Component { constructor(props: Props) { super(props); this.state = { name: "", role: props.availableRoles[0].name, verbs: undefined, groupPermission: false, valid: true, value: undefined, showAdvancedDialog: false }; } groupPermissionScopeChanged = (value: boolean) => { if (value) { this.permissionScopeChanged(true); } }; userPermissionScopeChanged = (value: boolean) => { if (value) { this.permissionScopeChanged(false); } }; permissionScopeChanged = (groupPermission: boolean) => { this.setState({ value: undefined, name: "", groupPermission, valid: false }); }; renderAutocompletionField = () => { const group = this.state.groupPermission; if (group) { return ( ); } return ( ); }; selectName = (value: SelectValue) => { this.setState({ value, name: value.value.id, valid: validator.isPermissionValid(value.value.id, this.state.groupPermission, this.props.currentPermissions) }); }; render() { const { t, availableRoles, availableVerbs, loading } = this.props; const { role, verbs, showAdvancedDialog } = this.state; const availableRoleNames = availableRoles.map(r => r.name); const selectedVerbs = role ? findVerbsForRole(availableRoles, role) : verbs; const advancedDialog = showAdvancedDialog ? ( ) : null; return ( <>
{advancedDialog}
{this.renderAutocompletionField()}
} /> ); } toggleAdvancedPermissionsDialog = () => { this.setState(prevState => ({ showAdvancedDialog: !prevState.showAdvancedDialog })); }; submitAdvancedPermissionsDialog = (newVerbs: string[]) => { this.setState({ showAdvancedDialog: false, role: undefined, verbs: newVerbs }); }; submit = e => { this.props.createPermission({ name: this.state.name, role: this.state.role, verbs: this.state.verbs, groupPermission: this.state.groupPermission }); this.removeState(); e.preventDefault(); }; removeState = () => { this.setState({ name: "", role: this.props.availableRoles[0].name, verbs: undefined, valid: true, value: undefined }); }; handleRoleChange = (role: string) => { const selectedRole = this.findAvailableRole(role); if (!selectedRole) { return; } this.setState({ role: selectedRole.name, verbs: [] }); }; findAvailableRole = (roleName: string) => { return this.props.availableRoles.find(role => role.name === roleName); }; } export default withTranslation("repos")(CreatePermissionForm);