Skip to content

Commit 64d68b9

Browse files
committed
create WithCurrentUser component
1 parent f65f4dc commit 64d68b9

File tree

6 files changed

+99
-76
lines changed

6 files changed

+99
-76
lines changed

packages/gatsby-theme/src/components/Account.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
import React from 'react';
2-
import { Query } from 'react-apollo';
3-
import { CurrentUserQuery } from '../graphqlTypes';
42
import { Text, Image, Flex } from './shared/base';
53
import { Link } from 'gatsby';
6-
import { CenteredLoader } from './Loader';
74
import CustomButton from './CustomButton';
8-
import { optionalChaining } from '../utils/helpers';
95
import { loginUser } from '../utils/auth';
10-
import { CURRENT_USER } from './queries/userQueries';
6+
import WithCurrentUser from '../utils/WithCurrentUser';
7+
import { CenteredLoader } from '../components/Loader';
118

129
const Account = () => {
1310
return (
14-
<Query<CurrentUserQuery> query={CURRENT_USER}>
15-
{({ data, error, loading }) => {
16-
if (error || loading) {
11+
<WithCurrentUser>
12+
{({ user, loading }) => {
13+
if (loading) {
1714
return <CenteredLoader />;
1815
}
19-
if (optionalChaining(() => data.viewer.user)) {
20-
return <Profile user={data.viewer.user} />;
16+
if (user) {
17+
return <Profile user={user} />;
18+
} else {
19+
return (
20+
<CustomButton onClick={() => loginUser()} type="github">
21+
Sign up
22+
</CustomButton>
23+
);
2124
}
22-
return (
23-
<CustomButton onClick={() => loginUser()} type="github">
24-
Sign up
25-
</CustomButton>
26-
);
2725
}}
28-
</Query>
26+
</WithCurrentUser>
2927
);
3028
};
3129

packages/gatsby-theme/src/components/Upvote.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,31 @@
11
import * as React from 'react';
2-
import { Query } from 'react-apollo';
3-
import { CurrentUserQuery } from '../graphqlTypes';
4-
import { Image, Heading, Flex } from './shared/base';
2+
import { Heading, Flex } from './shared/base';
53
import CustomButton from './CustomButton';
6-
import { optionalChaining } from '../utils/helpers';
74
import { loginUser } from '../utils/auth';
8-
import { CURRENT_USER } from './queries/userQueries';
5+
import WithCurrentUser from '../utils/WithCurrentUser';
96

107
const Upvote = () => {
118
return (
12-
<Query<CurrentUserQuery> query={CURRENT_USER}>
13-
{({ data, error, loading }) => {
14-
if (error || loading) {
15-
return null;
16-
}
17-
if (optionalChaining(() => data.viewer.user)) {
9+
<WithCurrentUser>
10+
{({ user }) => {
11+
if (user) {
1812
return <UpvoteData event={() => console.log('upvoted!')} />;
13+
} else {
14+
return <UpvoteData event={() => loginUser()} />;
1915
}
20-
return <UpvoteData event={() => loginUser()} />;
2116
}}
22-
</Query>
17+
</WithCurrentUser>
2318
);
2419
};
2520

26-
const UpvoteData = props => {
21+
type UpvoteDataProps = {
22+
event: string;
23+
};
24+
25+
const UpvoteData: React.FunctionComponent<UpvoteDataProps> = ({ event }) => {
2726
return (
2827
<Flex flexDirection="column" alignItems="center" justifyContent="center">
29-
<CustomButton type="vote" onClick={props.event} />
28+
<CustomButton type="vote" onClick={event} />
3029
<Heading>{Math.floor(Math.random() * 100)}</Heading>
3130
</Flex>
3231
);

packages/gatsby-theme/src/components/layout.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { RouterProps } from "@reach/router";
2-
import * as React from "react";
3-
import Helmet from "react-helmet";
4-
import { theme, ThemeProvider, styled } from "../styles";
5-
import { useLayoutQuery } from "../hooks/useLayoutQuery";
6-
import Header from "./shared/Header";
1+
import { RouterProps } from '@reach/router';
2+
import * as React from 'react';
3+
import Helmet from 'react-helmet';
4+
import { theme, ThemeProvider, styled } from '../styles';
5+
import { useLayoutQuery } from '../hooks/useLayoutQuery';
6+
import Header from './shared/Header';
77

88
const MainLayout = styled.main`
99
max-width: 90%;
@@ -13,7 +13,7 @@ const MainLayout = styled.main`
1313

1414
type LayoutProps = React.ReactNode & RouterProps;
1515

16-
const Layout: React.FunctionComponent<LayoutProps> = ({ children }) => {
16+
const Layout = ({ children }) => {
1717
const { site } = useLayoutQuery();
1818

1919
const { title, description, keywords } = site!.siteMetadata;
@@ -24,8 +24,8 @@ const Layout: React.FunctionComponent<LayoutProps> = ({ children }) => {
2424
<Helmet
2525
title={title}
2626
meta={[
27-
{ name: "description", content: description },
28-
{ name: "keywords", content: keywords || undefined }
27+
{ name: 'description', content: description },
28+
{ name: 'keywords', content: keywords || undefined },
2929
]}
3030
>
3131
<html lang="en" />

packages/gatsby-theme/src/pages/profile.tsx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
import * as React from 'react';
22
import Layout from '../components/layout';
3-
import { Query } from 'react-apollo';
4-
import gql from 'graphql-tag';
5-
import { ViewerQuery } from '../graphqlTypes';
63
import { Text, Image, Flex } from '../components/shared/base';
74
import { logoutUser } from '../utils/auth';
85
import { navigate } from 'gatsby';
6+
import WithCurrentUser from '../utils/WithCurrentUser';
97
import { CenteredLoader } from '../components/Loader';
10-
import { optionalChaining } from '../utils/helpers';
118

12-
import { CURRENT_USER } from '../components/queries/userQueries';
13-
14-
const Profile = () => (
15-
<Query<ViewerQuery> query={CURRENT_USER}>
16-
{({ data, error, loading }) => {
17-
if (error || loading) {
18-
return <CenteredLoader />;
19-
}
20-
if (optionalChaining(() => data.viewer.user)) {
21-
return <ProfilePage user={data.viewer.user} />;
22-
}
23-
navigate('/signup/');
24-
return null;
25-
}}
26-
</Query>
27-
);
9+
const Profile = () => {
10+
return (
11+
<WithCurrentUser>
12+
{({ user, loading }) => {
13+
if (loading) {
14+
return <CenteredLoader />;
15+
}
16+
if (user) {
17+
return <ProfilePage user={user} />;
18+
} else {
19+
navigate('/signup/');
20+
return null;
21+
}
22+
}}
23+
</WithCurrentUser>
24+
);
25+
};
2826

2927
type ProfileProps = {
3028
user: User;

packages/gatsby-theme/src/pages/signup.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
import * as React from 'react';
22
import Layout from '../components/layout';
3-
import { Query } from 'react-apollo';
4-
import { CurrentUserQuery } from '../graphqlTypes';
53
import { navigate } from 'gatsby';
64
import { CenteredLoader } from '../components/Loader';
75
import { Flex, Text, Box } from '../components/shared/base';
86
import CustomButton from '../components/CustomButton';
97
import { loginUser } from '../utils/auth';
8+
<<<<<<< HEAD
109
import { CURRENT_USER } from '../components/queries/userQueries';
1110
import { optionalChaining } from '../utils/helpers';
11+
=======
12+
import WithCurrentUser from '../utils/WithCurrentUser';
13+
>>>>>>> create WithCurrentUser component
1214

13-
const Signup = () => (
14-
<Query<CurrentUserQuery> query={CURRENT_USER}>
15-
{({ data, error, loading }) => {
16-
if (error || loading) {
17-
return <CenteredLoader />;
18-
}
19-
if (optionalChaining(() => data.viewer.user)) {
20-
navigate('/profile/');
21-
return null;
22-
}
23-
return <SignupPage />;
24-
}}
25-
</Query>
26-
);
15+
const Signup = () => {
16+
return (
17+
<WithCurrentUser>
18+
{({ user, loading }) => {
19+
if (loading) {
20+
return <CenteredLoader />;
21+
}
22+
if (user) {
23+
navigate('/profile/');
24+
return null;
25+
} else {
26+
return <SignupPage />;
27+
}
28+
}}
29+
</WithCurrentUser>
30+
);
31+
};
2732

2833
const SignupPage = () => {
2934
return (
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { Component } from 'react';
2+
import { Query } from 'react-apollo';
3+
import { CURRENT_USER } from '../components/queries/userQueries';
4+
import { optionalChaining } from './helpers';
5+
6+
const WithCurrentUser = ({ children }) => {
7+
return (
8+
<Query query={CURRENT_USER}>
9+
{({ data, error, loading }) => {
10+
if (loading) {
11+
return children({ loading });
12+
}
13+
if (optionalChaining(() => data.viewer.user)) {
14+
return children({ user: data.viewer.user });
15+
} else {
16+
return children({ user: false });
17+
}
18+
}}
19+
</Query>
20+
);
21+
};
22+
23+
export default WithCurrentUser;

0 commit comments

Comments
 (0)