File size: 1,902 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import MaterialList from '@material-ui/core/List';
import MaterialItem from '@material-ui/core/ListItem';
import gql from 'graphql-tag';
import React from 'react';
import styled from 'styled-components';
import { useApolloClient } from '@apollo/react-hooks';
import { useUsersListQuery, User, UsersListDocument } from '../graphql/types';
import * as fragments from '../graphql/fragments';

const ActualList = styled(MaterialList)`
  padding: 0;
`;

const UserItem = styled(MaterialItem)`
  position: relative;
  padding: 7.5px 15px;
  display: flex;
  cursor: pinter;
`;

const ProfilePicture = styled.img`
  height: 50px;
  width: 50px;
  object-fit: cover;
  border-radius: 50%;
`;

const Name = styled.div`
  padding-left: 15px;
  font-weight: bold;
`;

export const UsersListQuery = gql`
  query UsersList {
    users {
      ...User
    }
  }
  ${fragments.user}
`;

export const useUsersPrefetch = () => {
  const client = useApolloClient();

  return () => {
    client.query({
      query: UsersListDocument,
    });
  };
};

interface ChildComponentProps {
  onUserPick: any;
}

const UsersList: React.FC<ChildComponentProps> = ({
  onUserPick = (user: User) => {},
}) => {
  const { data, loading: loadingUsers } = useUsersListQuery();

  if (data === undefined) return null;
  const users = data.users;

  return (
    <ActualList>
      {!loadingUsers &&
        users.map((user) => (
          <UserItem
            key={user.id}
            data-testid="user"
            onClick={onUserPick.bind(null, user)}
            button>
            {user !== null && user.picture !== null && (
              <React.Fragment>
                <ProfilePicture data-testid="picture" src={user.picture} />
                <Name data-testid="name">{user.name}</Name>
              </React.Fragment>
            )}
          </UserItem>
        ))}
    </ActualList>
  );
};

export default UsersList;