diff --git a/jams-react-client/src/components/Drawer/Drawer.js b/jams-react-client/src/components/Drawer/Drawer.js index 2ce4dd17f1a1b94438235bd8904ca0f7c62b9ec4..ead8123354738dde20d431b96a061796ed7ad82a 100644 --- a/jams-react-client/src/components/Drawer/Drawer.js +++ b/jams-react-client/src/components/Drawer/Drawer.js @@ -37,6 +37,7 @@ export default function TemporaryDrawer(props) { const history = useHistory(); const [direction, setDirection] = React.useState(props.direction); const [users, setUsers] = React.useState([]); + const [existingContacts, setExistingContacts] = React.useState([]); const [userAdded, setUserAdded] = React.useState(false); let addingToGroup = props.addingToGroup; @@ -44,6 +45,25 @@ export default function TemporaryDrawer(props) { /** * Get users list to pass to the drawer to add contacts to user or users to group */ + + axios(configApiCall(api_path_get_admin_contacts+"?username="+props.username, 'GET', null, null)).then((response)=>{ + /* + TODO: Include the username of the user of witch we want to display contacts + at the moment the admin sees his contacts in each user profile he visits + */ + let originalContacts = response.data + originalContacts.map((contact) => { + contact.display = "" + }) + setExistingContacts(originalContacts) + }).catch((error) =>{ + console.log(error); + if(error.response.status == 401){ + auth.authenticated = false + history.push('/') + } + }); + axios(configApiCall(api_path_get_user_directory_search, 'GET', {"queryString":"*"}, null)).then((response)=>{ let resp = response.data if(!addingToGroup){ @@ -78,8 +98,8 @@ export default function TemporaryDrawer(props) { }; if(props.isAdmin){ axios(configApiCall(api_path_get_admin_contacts+"?username="+props.username,'PUT', data, null)).then((response)=>{ + setUserAdded(true); props.setOpenDrawer(false) - props.getAllUsers() }).catch((error) =>{ console.log("Error adding user: " + error) props.setOpenDrawer(false) @@ -87,8 +107,8 @@ export default function TemporaryDrawer(props) { } else { axios(configApiCall(api_path_get_auth_contacts, 'PUT', data, null)).then((response)=>{ + setUserAdded(true); props.setOpenDrawer(false) - props.getAllUsers() }).catch((error) =>{ console.log("Error adding user: " + error) props.setOpenDrawer(false) @@ -134,18 +154,29 @@ export default function TemporaryDrawer(props) { const listUsers = () => ( <List> - {users.map((user) => ( + { addingToGroup ? - <ListItem button key={user.username} onClick={() => {addUserToGroup(user.username)}} > - <AddCircleOutlineIcon style={{ marginRight: "10px"}} /><ListItemText primary={user.firstName+" "+user.lastName} /> - </ListItem> + users.map((user) => ( + <ListItem button key={user.username} onClick={() => {addUserToGroup(user.username)}} > + <AddCircleOutlineIcon style={{ marginRight: "10px"}} /><ListItemText primary={user.firstName+" "+user.lastName} /> + </ListItem> + )) : - <ListItem button key={user.username} onClick={() => {addContactToUser(user.firstName, user.lastName, user.id)}} > - <AddCircleOutlineIcon style={{ marginRight: "10px"}} /><ListItemText primary={user.firstName+" "+user.lastName} /> - </ListItem> - - )) + users.filter((data)=>{ + var added = false; + existingContacts.forEach((contact) => { + if(contact.uri === data.id) + added = true + }) + if(!added) + return data + + }).map((user) => ( + <ListItem button key={user.username} onClick={() => {addContactToUser(user.firstName, user.lastName, user.id)}} > + <AddCircleOutlineIcon style={{ marginRight: "10px"}} /><ListItemText primary={user.firstName+" "+user.lastName} /> + </ListItem> + )) } </List> ); diff --git a/jams-react-client/src/views/Contacts/Contacts.js b/jams-react-client/src/views/Contacts/Contacts.js index 500e1b504d9c69847f4d6d527498fb88ca9f2d4a..c964226aab0677ba4a0c17060c9025226c9b6287 100644 --- a/jams-react-client/src/views/Contacts/Contacts.js +++ b/jams-react-client/src/views/Contacts/Contacts.js @@ -93,7 +93,7 @@ export default function Users(props) { setLoading(false) }).catch((error) =>{ console.log(error); - if(error.response.status == 401){ + if(error.response.status === 401){ auth.authenticated = false history.push('/') } @@ -165,8 +165,8 @@ export default function Users(props) { return( <div> - <TemporaryDrawer isAdmin={auth.hasAdminScope()} username={props.username} openDrawer={openDrawer} setOpenDrawer={setOpenDrawer} getAllContacts={getAllContacts} direction="right"/> - <Dialog + <TemporaryDrawer isAdmin={auth.hasAdminScope()} username={props.username} openDrawer={openDrawer} setOpenDrawer={setOpenDrawer} direction="right" addingToGroup={false} /> + <Dialog open={open} onClose={() => setOpen(false)} aria-labelledby="alert-dialog-title"