Skip to content

Commit

Permalink
feat: lots of features
Browse files Browse the repository at this point in the history
  • Loading branch information
brmk committed Nov 6, 2019
1 parent 68b9dff commit 4dd7ca5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint": "eslint ./src",
"lint:fix": "eslint ./src --fix",
"build": "webpack",
"start": "webpack-dev-server",
"start": "webpack-dev-server --port 3000 --hot --host 0.0.0.0",
"release": "standard-version",
"release:major": "npm run release -- --release-as major",
"release:minor": "npm run release -- --release-as minor",
Expand Down
14 changes: 13 additions & 1 deletion src/hooks/useAuthHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ const LOGIN = gql`
}
`;

const LOGOUT = gql`
mutation logout {
logout
}
`;

const useAuthHandlers = () => {
const client = useApolloClient();

Expand All @@ -65,6 +71,12 @@ const useAuthHandlers = () => {
},
});

const logout = useCallback(() => {
client.mutate({ mutation: LOGOUT });
UserModel.setToken('');
client.cache.writeQuery({ query: ME_QUERY, data: { me: null } });
}, [client]);

const createAccount = useCallback(async (username, password, profile) => {
const { data: { createAccount: data } } = await createAccountMutate({
variables: { username, password, profile },
Expand All @@ -82,7 +94,7 @@ const useAuthHandlers = () => {
return data;
}, [loginMutate]);

return { createAccount, login };
return { createAccount, login, logout };
};

export default useAuthHandlers;
27 changes: 25 additions & 2 deletions src/hooks/useFeedPage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable new-cap */
import { useCallback } from 'react';
import { useQuery, useMutation } from '@apollo/react-hooks';
import { toast } from 'react-toastify';
import uuid from 'uuid';
import { gql } from 'apollo-boost';
import useMe from './useMe';
// import PostsModel from '../modules/posts';
Expand Down Expand Up @@ -43,14 +45,35 @@ const ADD_POST_MUTATION = gql`

const useFeedPage = () => {
const [user] = useMe();
const { loading, data } = useQuery(POSTS_QUERY);
const [addPost] = useMutation(ADD_POST_MUTATION);
const { loading, data } = useQuery(POSTS_QUERY, {
pollInterval: 5000,
});
const [addPost] = useMutation(ADD_POST_MUTATION, {
refetchQueries: [{ query: POSTS_QUERY }],
update: (cache, { data: { addPost: post } }) => {
const { posts: prevPosts } = cache.readQuery({ query: POSTS_QUERY });
const posts = [post, ...prevPosts];
cache.writeQuery({
query: POSTS_QUERY,
data: { posts },
});
},
});
const posts = data && data.posts ? data.posts : [];

const handleAddPost = useCallback(async (body) => {
try {
await addPost({
variables: { body, createdBy: user && user._id },
optimisticResponse: {
addPost: {
_id: uuid(),
body,
createdAt: String(new Date().getTime()),
creator: user,
__typename: 'Post',
},
},
});
toast.success('Published a new post');
} catch (error) {
Expand Down
11 changes: 4 additions & 7 deletions src/layouts/MainLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ import {
NavLink,
} from 'reactstrap';
import { Link } from 'react-router-dom';
import UsersModel from '../modules/users';
import routes from '../router/routes';
import useAuthHandlers from '../hooks/useAuthHandlers';
import ThemeContext from '../contexts/Theme';

const MainLayout = ({ children, history }) => {
const MainLayout = ({ children }) => {
const [isOpen, setIsOpen] = useState(false);

const toggle = useCallback(() => setIsOpen(!isOpen), [isOpen]);

const handleLogout = useCallback(() => {
UsersModel.logout();
history.replace(routes.login);
}, [history]);
const { logout } = useAuthHandlers();

const [theme, setTheme] = React.useState('light');

Expand Down Expand Up @@ -52,7 +49,7 @@ const MainLayout = ({ children, history }) => {
</NavLink>
</NavItem>
<NavItem>
<NavLink href="#" onClick={handleLogout}>
<NavLink href="#" onClick={logout}>
Log out
</NavLink>
</NavItem>
Expand Down
5 changes: 1 addition & 4 deletions src/modules/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ export class Users {
this.currentUser = this.me(this.token);
}

logout = (skipServer) => {
if (!skipServer) {
UsersServer.logout(this.token);
}
logout = () => {
this.setToken({ token: '' });
this.currentUser = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/router/AppRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import NotFoundPage from '../pages/NotFoundPage';
import MainLayout from '../layouts/MainLayout';
import routes from './routes';

const AppRouter = (p) => (
<MainLayout {...p}>
const AppRouter = () => (
<MainLayout>
<Switch>
<Route path={routes.feed} render={(props) => <FeedPage {...props} />} />
<Route
Expand Down
2 changes: 1 addition & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import AppRouter from './AppRouter';

const RootRouter = () => {
const [isLoggedIn, loading] = useMe();
console.log(isLoggedIn);

if (loading) {
return (
<Spinner />
Expand Down

0 comments on commit 4dd7ca5

Please sign in to comment.