import React, { useEffect, useState } from 'react'; import { Empty, List, message, Pagination } from 'antd'; import styled from 'styled-components'; import * as QueryString from 'query-string'; import { useLocation } from 'react-router'; import UserListItem from './UserListItem'; import { Message } from '../../shared/Message'; import { useListUsersQuery } from '../../../graphql/user.generated'; import { CorpUser } from '../../../types.generated'; import TabToolbar from '../../entity/shared/components/styled/TabToolbar'; import { SearchBar } from '../../search/SearchBar'; import { useEntityRegistry } from '../../useEntityRegistry'; 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 = () => { 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); useEffect(() => setQuery(paramsQuery), [paramsQuery]); const [page, setPage] = useState(1); const [removedUrns, setRemovedUrns] = useState([]); const pageSize = DEFAULT_PAGE_SIZE; const start = (page - 1) * pageSize; const { loading, error, data, refetch } = useListUsersQuery({ variables: { input: { start, count: pageSize, query, }, }, 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 && } {error && message.error('Failed to load users :(')}
<>
null} onQueryChange={(q) => setQuery(q)} entityRegistry={entityRegistry} />
, }} dataSource={filteredUsers} renderItem={(item: any) => ( handleDelete(item.urn as string)} user={item as CorpUser} /> )} />
); };