aboutsummaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-06-30 01:47:27 +0300
committerGitHub <noreply@github.com>2020-06-30 01:47:27 +0300
commit720a32c4cb1697f3a8f90973f28c334551ab87ff (patch)
treec9028ea3ff850774d33cbc510eb19dd0e9f7aade /src/components
parentb301bf24c5037403a1e5fc32fc8c10794941b528 (diff)
parente170d04d5d2c8c86d2683f3accb4feb2d94c881a (diff)
downloadwhich-ui-720a32c4cb1697f3a8f90973f28c334551ab87ff.tar.gz
Merge pull request #54 from which-ecosystem/hooks
Use hooks logic
Diffstat (limited to 'src/components')
-rw-r--r--src/components/Feed/Feed.tsx5
-rw-r--r--src/components/Header/Header.tsx20
-rw-r--r--src/components/Header/SearchBar.tsx9
-rw-r--r--src/components/PollCard/PollCard.tsx5
-rw-r--r--src/components/UserStrip/UserStrip.tsx16
5 files changed, 23 insertions, 32 deletions
diff --git a/src/components/Feed/Feed.tsx b/src/components/Feed/Feed.tsx
index 0c4d84f..7636573 100644
--- a/src/components/Feed/Feed.tsx
+++ b/src/components/Feed/Feed.tsx
@@ -5,7 +5,6 @@ import PollCard from '../PollCard/PollCard';
interface PropTypes {
polls: Poll[];
- navigate: (prefix: string, id: string) => void;
}
const useStyles = makeStyles(theme => ({
@@ -16,11 +15,11 @@ const useStyles = makeStyles(theme => ({
}
}));
-const Feed: React.FC<PropTypes> = ({ polls, navigate }) => {
+const Feed: React.FC<PropTypes> = ({ polls }) => {
const classes = useStyles();
return (
<div className={classes.root}>
- {polls.map(poll => <PollCard initialPoll={poll} key={poll._id} navigate={navigate} />)}
+ {polls.map(poll => <PollCard initialPoll={poll} key={poll._id} />)}
</div>
);
};
diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx
index d0d9081..72e40f8 100644
--- a/src/components/Header/Header.tsx
+++ b/src/components/Header/Header.tsx
@@ -9,14 +9,11 @@ import { makeStyles } from '@material-ui/core/styles';
import AccountCircle from '@material-ui/icons/AccountCircle';
import NotificationsIcon from '@material-ui/icons/Notifications';
import HomeIcon from '@material-ui/icons/Home';
+import { useAuth } from '../../hooks/useAuth';
+import { useNavigate } from '../../hooks/useNavigate';
import SearchBar from './SearchBar';
-interface PropTypes {
- userImage: string | undefined;
- navigate: (prefix: string) => void;
-}
-
const useStyles = makeStyles({
root: {
display: 'flex',
@@ -33,15 +30,18 @@ const useStyles = makeStyles({
}
});
-const Header: React.FC<PropTypes> = ({ navigate, userImage }) => {
+const Header: React.FC = () => {
const classes = useStyles();
+ const { user } = useAuth();
+ const { navigate } = useNavigate();
const handleHome = (): void => {
navigate('feed');
};
const handleProfile = (): void => {
- navigate('profile');
+ if (user) navigate('profile');
+ else navigate('auth');
};
const handleNotifications = (): void => {};
@@ -52,7 +52,7 @@ const Header: React.FC<PropTypes> = ({ navigate, userImage }) => {
<Typography variant="h5" className={classes.logo}>
Which
</Typography>
- <SearchBar navigate={navigate} />
+ <SearchBar />
<div>
<IconButton onClick={handleHome}>
<HomeIcon />
@@ -62,8 +62,8 @@ const Header: React.FC<PropTypes> = ({ navigate, userImage }) => {
</IconButton>
<IconButton onClick={handleProfile}>
{
- userImage?.match(/\.(jpeg|jpg|gif|png)$/)
- ? <Avatar className={classes.avatar} src={userImage} />
+ user?.avatarUrl?.match(/\.(jpeg|jpg|gif|png)$/)
+ ? <Avatar className={classes.avatar} src={user?.avatarUrl} />
: <AccountCircle />
}
</IconButton>
diff --git a/src/components/Header/SearchBar.tsx b/src/components/Header/SearchBar.tsx
index bff16a0..253e77f 100644
--- a/src/components/Header/SearchBar.tsx
+++ b/src/components/Header/SearchBar.tsx
@@ -12,10 +12,8 @@ import { makeStyles } from '@material-ui/core/styles';
import { User } from 'which-types';
import { get } from '../../requests';
import UserStrip from '../UserStrip/UserStrip';
+import { useNavigate } from '../../hooks/useNavigate';
-interface PropTypes {
- navigate: (prefix: string, id: string) => void;
-}
const INTERVAL = 300;
const LIMIT = 7;
@@ -36,10 +34,11 @@ const useStyles = makeStyles(theme => ({
}
}));
-const SearchBar: React.FC<PropTypes> = ({ navigate }) => {
+const SearchBar: React.FC = () => {
const [results, setResults] = useState<User[]>([]);
const [query, setQuery] = useState<string>('');
const [debouncedQuery, setDebouncedQuery] = useState<string>(query);
+ const { navigate } = useNavigate();
const classes = useStyles();
useEffect(() => {
@@ -79,7 +78,7 @@ const SearchBar: React.FC<PropTypes> = ({ navigate }) => {
results.map((result, index) => (
<div key={result._id}>
<ListItem button onClick={handleNavigate(index)}>
- <UserStrip user={result} navigate={navigate} />
+ <UserStrip user={result} />
</ListItem>
{(index < results.length - 1) && <Divider />}
</div>
diff --git a/src/components/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx
index 156315a..2a23522 100644
--- a/src/components/PollCard/PollCard.tsx
+++ b/src/components/PollCard/PollCard.tsx
@@ -13,7 +13,6 @@ import { post } from '../../requests';
interface PropTypes {
initialPoll: Poll;
- navigate: (prefix: string, id: string) => void;
}
const DATE_FORMAT = {
@@ -54,7 +53,7 @@ const useStyles = makeStyles(theme => ({
}
}));
-const PollCard: React.FC<PropTypes> = ({ initialPoll, navigate }) => {
+const PollCard: React.FC<PropTypes> = ({ initialPoll }) => {
const [poll, setPoll] = useState<Poll>(initialPoll);
const classes = useStyles();
const { author, contents: { left, right }, vote } = poll;
@@ -87,7 +86,7 @@ const PollCard: React.FC<PropTypes> = ({ initialPoll, navigate }) => {
return (
<Card className={classes.root}>
- <UserStrip user={author} info={date} navigate={navigate} />
+ <UserStrip user={author} info={date} />
<div className={classes.imagesBlock}>
<CardActionArea onDoubleClick={handleLeft}>
<CardMedia
diff --git a/src/components/UserStrip/UserStrip.tsx b/src/components/UserStrip/UserStrip.tsx
index f02adc3..3ac47b3 100644
--- a/src/components/UserStrip/UserStrip.tsx
+++ b/src/components/UserStrip/UserStrip.tsx
@@ -1,16 +1,13 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import VerifiedIcon from '@material-ui/icons/CheckCircleOutline';
-import {
- Avatar,
- CardHeader
-} from '@material-ui/core/';
+import { Avatar, CardHeader } from '@material-ui/core/';
import { User } from 'which-types';
+import { useNavigate } from '../../hooks/useNavigate';
interface PropTypes {
user: User;
- navigate: (prefix: string, id: string) => void;
info?: string | JSX.Element
}
@@ -31,13 +28,10 @@ const useStyles = makeStyles(theme => ({
}));
-const UserStrip: React.FC<PropTypes> = ({ user, info, navigate }) => {
+const UserStrip: React.FC<PropTypes> = ({ user, info }) => {
const classes = useStyles();
- const {
- username,
- avatarUrl,
- verified
- } = user;
+ const { navigate } = useNavigate();
+ const { username, avatarUrl, verified } = user;
const handleNavigate = () => {
navigate('profile', user._id);