2022-05-16 13:24:56 -07:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-10-07 16:14:35 -07:00
|
|
|
import { Empty, List, message, Pagination } from 'antd';
|
|
|
|
import styled from 'styled-components';
|
2022-05-16 13:24:56 -07:00
|
|
|
import * as QueryString from 'query-string';
|
|
|
|
import { useLocation } from 'react-router';
|
2021-10-07 16:14:35 -07:00
|
|
|
import UserListItem from './UserListItem';
|
|
|
|
import { Message } from '../../shared/Message';
|
|
|
|
import { useListUsersQuery } from '../../../graphql/user.generated';
|
|
|
|
import { CorpUser } from '../../../types.generated';
|
2022-05-16 13:24:56 -07:00
|
|
|
import TabToolbar from '../../entity/shared/components/styled/TabToolbar';
|
|
|
|
import { SearchBar } from '../../search/SearchBar';
|
|
|
|
import { useEntityRegistry } from '../../useEntityRegistry';
|
2021-10-07 16:14:35 -07:00
|
|
|
|
|
|
|
const UserContainer = styled.div``;
|
|
|
|
|
|
|
|
const UserStyledList = styled(List)`
|
|
|
|
&&& {
|
|
|
|
width: 100%;
|
|
|
|
border-color: ${(props) => props.theme.styles['border-color-base']};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const UserPaginationContainer = styled.div`
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const DEFAULT_PAGE_SIZE = 25;
|
|
|
|
|
|
|
|
export const UserList = () => {
|
2022-05-16 13:24:56 -07:00
|
|
|
const entityRegistry = useEntityRegistry();
|
|
|
|
const location = useLocation();
|
|
|
|
const params = QueryString.parse(location.search, { arrayFormat: 'comma' });
|
|
|
|
const paramsQuery = (params?.query as string) || undefined;
|
|
|
|
const [query, setQuery] = useState<undefined | string>(undefined);
|
|
|
|
useEffect(() => setQuery(paramsQuery), [paramsQuery]);
|
|
|
|
|
2021-10-07 16:14:35 -07:00
|
|
|
const [page, setPage] = useState(1);
|
|
|
|
const [removedUrns, setRemovedUrns] = useState<string[]>([]);
|
|
|
|
|
|
|
|
const pageSize = DEFAULT_PAGE_SIZE;
|
|
|
|
const start = (page - 1) * pageSize;
|
|
|
|
|
|
|
|
const { loading, error, data, refetch } = useListUsersQuery({
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
start,
|
|
|
|
count: pageSize,
|
2022-05-16 13:24:56 -07:00
|
|
|
query,
|
2021-10-07 16:14:35 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
});
|
|
|
|
|
|
|
|
const totalUsers = data?.listUsers?.total || 0;
|
|
|
|
const users = data?.listUsers?.users || [];
|
|
|
|
const filteredUsers = users.filter((user) => !removedUrns.includes(user.urn));
|
|
|
|
|
|
|
|
const onChangePage = (newPage: number) => {
|
|
|
|
setPage(newPage);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDelete = (urn: string) => {
|
|
|
|
// Hack to deal with eventual consistency.
|
|
|
|
const newRemovedUrns = [...removedUrns, urn];
|
|
|
|
setRemovedUrns(newRemovedUrns);
|
|
|
|
setTimeout(function () {
|
|
|
|
refetch?.();
|
|
|
|
}, 3000);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!data && loading && <Message type="loading" content="Loading users..." />}
|
|
|
|
{error && message.error('Failed to load users :(')}
|
|
|
|
<UserContainer>
|
2022-05-16 13:24:56 -07:00
|
|
|
<TabToolbar>
|
|
|
|
<div>
|
|
|
|
<></>
|
|
|
|
</div>
|
|
|
|
<SearchBar
|
|
|
|
initialQuery={query || ''}
|
|
|
|
placeholderText="Search users..."
|
|
|
|
suggestions={[]}
|
|
|
|
style={{
|
|
|
|
maxWidth: 220,
|
|
|
|
padding: 0,
|
|
|
|
}}
|
|
|
|
inputStyle={{
|
|
|
|
height: 32,
|
|
|
|
fontSize: 12,
|
|
|
|
}}
|
|
|
|
onSearch={() => null}
|
|
|
|
onQueryChange={(q) => setQuery(q)}
|
|
|
|
entityRegistry={entityRegistry}
|
|
|
|
/>
|
|
|
|
</TabToolbar>
|
2021-10-07 16:14:35 -07:00
|
|
|
<UserStyledList
|
|
|
|
bordered
|
|
|
|
locale={{
|
|
|
|
emptyText: <Empty description="No Users!" image={Empty.PRESENTED_IMAGE_SIMPLE} />,
|
|
|
|
}}
|
|
|
|
dataSource={filteredUsers}
|
|
|
|
renderItem={(item: any) => (
|
|
|
|
<UserListItem onDelete={() => handleDelete(item.urn as string)} user={item as CorpUser} />
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<UserPaginationContainer>
|
|
|
|
<Pagination
|
|
|
|
style={{ margin: 40 }}
|
|
|
|
current={page}
|
|
|
|
pageSize={pageSize}
|
|
|
|
total={totalUsers}
|
|
|
|
showLessItems
|
|
|
|
onChange={onChangePage}
|
|
|
|
showSizeChanger={false}
|
|
|
|
/>
|
|
|
|
</UserPaginationContainer>
|
|
|
|
</UserContainer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|