import React from 'react';
import { Form, Select, Switch, Tag, Typography } from 'antd';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { useEntityRegistry } from '../useEntityRegistry';
import { ActorFilter, EntityType, PolicyType, SearchResult } from '../../types.generated';
import { useGetSearchResultsLazyQuery } from '../../graphql/search.generated';
type Props = {
policyType: PolicyType;
actors: ActorFilter;
setActors: (actors: ActorFilter) => void;
};
const SearchResultContainer = styled.div`
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
`;
const ActorForm = styled(Form)`
margin: 12px;
margin-top: 36px;
margin-bottom: 40px;
`;
const ActorFormHeader = styled.div`
margin-bottom: 28px;
`;
/**
* Component used to construct the "actors" portion of a DataHub
* access Policy by populating an ActorFilter object.
*/
export default function PolicyActorForm({ policyType, actors, setActors }: Props) {
const entityRegistry = useEntityRegistry();
// Search for actors while building policy.
const [userSearch, { data: userSearchData }] = useGetSearchResultsLazyQuery();
const [groupSearch, { data: groupSearchData }] = useGetSearchResultsLazyQuery();
// Toggle the "Owners" switch
const onToggleAppliesToOwners = (value: boolean) => {
setActors({
...actors,
resourceOwners: value,
});
};
// When a user search result is selected, add the urn to the ActorFilter
const onSelectUserActor = (newUser: string) => {
if (newUser === 'All') {
setActors({
...actors,
allUsers: true,
});
} else {
const newUserActors = [...(actors.users || []), newUser];
setActors({
...actors,
users: newUserActors,
});
}
};
// When a user search result is deselected, remove the urn from the ActorFilter
const onDeselectUserActor = (user: string) => {
if (user === 'All') {
setActors({
...actors,
allUsers: false,
});
} else {
const newUserActors = actors.users?.filter((u) => u !== user);
setActors({
...actors,
users: newUserActors,
});
}
};
// When a group search result is selected, add the urn to the ActorFilter
const onSelectGroupActor = (newGroup: string) => {
if (newGroup === 'All') {
setActors({
...actors,
allGroups: true,
});
} else {
const newGroupActors = [...(actors.groups || []), newGroup];
setActors({
...actors,
groups: newGroupActors,
});
}
};
// When a group search result is deselected, remove the urn from the ActorFilter
const onDeselectGroupActor = (group: string) => {
if (group === 'All') {
setActors({
...actors,
allGroups: false,
});
} else {
const newGroupActors = actors.groups?.filter((g) => g !== group);
setActors({
...actors,
groups: newGroupActors,
});
}
};
// Invokes the search API as the user types
const handleSearch = (type: EntityType, text: string, searchQuery: any) => {
if (text.length > 2) {
searchQuery({
variables: {
input: {
type,
query: text,
start: 0,
count: 10,
},
},
});
}
};
// Invokes the user search API as the user types
const handleUserSearch = (text: string) => {
return handleSearch(EntityType.CorpUser, text, userSearch);
};
// Invokes the group search API as the user types
const handleGroupSearch = (text: string) => {
return handleSearch(EntityType.CorpGroup, text, groupSearch);
};
// Renders a search result in the select dropdown.
const renderSearchResult = (result: SearchResult) => {
return (
{entityRegistry.getDisplayName(result.entity.type, result.entity)}
`/${entityRegistry.getPathName(result.entity.type)}/${result.entity.urn}`}
>
View
{' '}
);
};
// Whether to show "owners" switch.
const showAppliesToOwners = policyType === PolicyType.Metadata;
// User and group dropdown search results!
const userSearchResults = userSearchData?.search?.searchResults;
const groupSearchResults = groupSearchData?.search?.searchResults;
// Select dropdown values.
const usersSelectValue = actors.allUsers ? ['All'] : actors.users || [];
const groupsSelectValue = actors.allGroups ? ['All'] : actors.groups || [];
return (
Applies toSelect the users & groups that this policy should apply to.
{showAppliesToOwners && (
Owners} labelAlign="right">
Whether this policy should be apply to owners of the Metadata asset. If true, those who are
marked as owners of a Metadata Asset, either directly or indirectly via a Group, will have the
selected privileges.
)}
Users}>
Search for specific users that this policy should apply to, or select `All Users` to apply it to all
users.
Groups}>
Search for specific groups that this policy should apply to, or select `All Groups` to apply it to
all groups.
);
}