Social.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import React, { useEffect, useState } from 'react';
  2. import { Avatar, Box, Divider, List, ListItem, ListItemAvatar, ListItemText, Typography } from '@material-ui/core';
  3. import Navbar from "../Navbar";
  4. import SearchButton from "../Buttons/SearchButton";
  5. import { useAuth0 } from "@auth0/auth0-react";
  6. import { makeStyles } from '@material-ui/styles';
  7. import UserSearch from "./UserSearch";
  8. import { useTranslation } from "react-i18next";
  9. import { fetchContactsOfUser, getContactName, getContactPicture, updateContactsFromAuth0 } from "./social.util";
  10. import { Route, Switch, useHistory, useRouteMatch } from "react-router-dom";
  11. import ContactsContent from "./ContactsContent";
  12. import { withLoginRequired } from "../util";
  13. const useStyles = makeStyles({
  14. infoText: {
  15. textAlign: "center",
  16. margin: "3rem 2rem 1rem",
  17. fontFamily: "Cookie",
  18. fontSize: "2rem",
  19. },
  20. });
  21. /**
  22. * displays a list of current user's contacts and allows searching for all users through Button in Navbar
  23. * on clicking on a contact's name will open ContactContent */
  24. const Social = () => {
  25. const classes = useStyles();
  26. const { user } = useAuth0();
  27. const { t } = useTranslation();
  28. let { path, url } = useRouteMatch();
  29. let history = useHistory();
  30. const [searchOpen, setSearchOpen] = useState(false);
  31. const [contacts, setContacts] = useState([]);
  32. const [noContactsFound, setNoContactsFound] = useState(false);
  33. const fetchContacts = (updateContactsData = false) => {
  34. if (user) {
  35. const userId = user.sub;
  36. fetchContactsOfUser(userId, (contactsFound) => {
  37. setContacts(contactsFound);
  38. if (contactsFound.length === 0) {
  39. setNoContactsFound(true);
  40. } else if (updateContactsData) updateContactsFromAuth0(userId, contactsFound.map(c => c.user_id), setContacts);
  41. });
  42. }
  43. }
  44. useEffect(() => {
  45. fetchContacts(true);
  46. // eslint-disable-next-line
  47. }, [])
  48. const showUser = (userId) => {
  49. history.push(`${url}/contact/${userId}`);
  50. }
  51. console.log(contacts);
  52. const getListItems = () => {
  53. return contacts.map(contact => {
  54. console.log('Contact', contact);
  55. const userId = contact.user_id;
  56. return (
  57. <Box key={userId}>
  58. <ListItem button onClick={() => {history.push(`${url}/contact/${userId}`);}}>
  59. <ListItemAvatar>
  60. <Avatar src={getContactPicture(contact)} alt={getContactName(contact)} />
  61. </ListItemAvatar>
  62. <ListItemText primary={getContactName(contact)} primaryTypographyProps={{ className: classes.listItemText }} />
  63. </ListItem>
  64. <Divider />
  65. </Box>
  66. );
  67. });
  68. }
  69. console.log(url, path);
  70. return (
  71. <>
  72. <Switch>
  73. <Route exact path={path}>
  74. {searchOpen ? <UserSearch open={searchOpen} closeSearch={() => {setSearchOpen(false)}} contacts={contacts} afterUpdateContacts={fetchContacts} openContact={showUser} />
  75. : <Navbar pageTitle={t('Contacts')} rightSideComponent={<SearchButton onClick={() => {setSearchOpen(true)}} />} />}
  76. {contacts.length === 0
  77. ? <Typography className={classes.infoText}>
  78. {noContactsFound
  79. ? <>{t("No contacts yet")}<br />{t("Search for friends in the top right corner")}</>
  80. : t('Loading') + '...'}
  81. </Typography>
  82. : <List component="nav" className={classes.root} aria-label="meal list">
  83. {getListItems()}
  84. </List>
  85. }
  86. </Route>
  87. <Route path={`${path}/contact/:userId`}>
  88. <ContactsContent />
  89. </Route>
  90. </Switch>
  91. </>
  92. )
  93. ;
  94. }
  95. export default withLoginRequired(Social);