Skip to content

Commit 6125b4c

Browse files
committed
i This is a combination of 2 commits.
put saved and upvoted tutorials on profile page
1 parent fabccdc commit 6125b4c

File tree

5 files changed

+106
-62
lines changed

5 files changed

+106
-62
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { Heading, Text, Card, Flex, Box } from './shared/base';
2+
import { Heading, Text, Card, Flex, Box, Button } from './shared/base';
33
import { getTutorialOverviewSlug } from '../utils/getTutorialSlug';
44
import Upvote from './Upvote';
55
import { Link } from 'gatsby';
@@ -114,7 +114,7 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
114114
>
115115
{save => {
116116
return (
117-
<button
117+
<Button
118118
onClick={async () => {
119119
const mutationRes = await handleMutationResponse(
120120
save(),
@@ -129,7 +129,7 @@ const TutorialListing: React.FunctionComponent<TutorialListingProps> = ({
129129
}}
130130
>
131131
Save
132-
</button>
132+
</Button>
133133
);
134134
}}
135135
</Mutation>

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
import * as React from 'react';
22
import { Heading, Flex } from './shared/base';
33
import { VoteButton } from './buttons';
4-
import { loginUser } from '../utils/auth/';
5-
import WithCurrentUser from '../utils/auth/WithCurrentUser';
6-
7-
// Still to-do:
8-
// Query the backend with the ID of the tutorial to see whcih tutorial was upvoted
9-
// Create a way to store which user has upvoted the tutorial so that they can only
10-
// upvote it once
11-
12-
// const Upvote = ({active}) => {
13-
// return (
14-
// <WithCurrentUser>
15-
// {({ user }) => {
16-
// if (user) {
17-
// return <UpvoteData active={active} event={() => console.log('upvoted!')} />;
18-
// }
19-
// return <UpvoteData active={active} event={() => loginUser()} />;
20-
// }}
21-
// </WithCurrentUser>
22-
// );
23-
// };
244

255
type UpvoteDataProps = {
266
onClick: () => any;
@@ -30,7 +10,11 @@ type UpvoteDataProps = {
3010

3111
// place holder until we have a backend that stores the number of upvotes
3212
// and can keep track of which tutorials a user upvotes
33-
const Upvote: React.FunctionComponent<UpvoteDataProps> = ({ onClick: event, active, count = "..." }) => {
13+
const Upvote: React.FunctionComponent<UpvoteDataProps> = ({
14+
onClick: event,
15+
active,
16+
count = '...',
17+
}) => {
3418
return (
3519
<Flex flexDirection="column" alignItems="center" justifyContent="center">
3620
<VoteButton active={active} onClick={event} />

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

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import * as React from 'react';
22
import Layout from '../components/layout';
3-
import { Text, Image, Flex } from '../components/shared/base';
3+
import { Heading, Text, Image, Flex } from '../components/shared/base';
44
import { logoutUser } from '../utils/auth';
55
import { navigate } from 'gatsby';
6-
import WithCurrentUser from '../utils/auth/WithCurrentUser';
6+
import { Query } from 'react-apollo';
7+
import gql from 'graphql-tag';
8+
import { optionalChaining } from '../utils/helpers';
79
import { CenteredLoader } from '../components/Loader';
810

911
const Profile = () => {
1012
return (
11-
<WithCurrentUser>
12-
{({ user, loading }) => {
13+
<Query query={PROFILE_QUERY}>
14+
{({ data, loading }) => {
1315
if (loading) {
1416
return <CenteredLoader />;
1517
}
16-
if (user) {
17-
return <ProfilePage user={user} />;
18+
if (optionalChaining(() => data!.viewer!.user)) {
19+
return <ProfilePage user={data.viewer.user} />;
1820
}
1921
navigate('/signup/');
2022
return null;
2123
}}
22-
</WithCurrentUser>
24+
</Query>
2325
);
2426
};
2527

@@ -32,9 +34,22 @@ type User = {
3234
avatarUrl: string;
3335
name: string;
3436
githubHandle: string;
37+
bio: string;
38+
upvoted: [Tutorials];
39+
saved: [Tutorials];
40+
};
41+
42+
type Tutorials = {
43+
tutorial: Tutorial;
44+
};
45+
46+
type Tutorial = {
47+
id: 'string';
48+
name: 'string';
3549
};
3650

3751
const ProfilePage: React.FunctionComponent<ProfileProps> = ({ user }) => {
52+
console.log(user);
3853
return (
3954
<Layout>
4055
<Flex flexDirection="column">
@@ -52,7 +67,49 @@ const ProfilePage: React.FunctionComponent<ProfileProps> = ({ user }) => {
5267
viverra nibh nec, ultrices risus. */
5368
</Text>
5469
<button onClick={() => logoutUser()}> Log out </button>
70+
<Heading> Upvoted Tutorials </Heading>
71+
<ul>
72+
{user.upvoted.map(a => (
73+
<li key={a.tutorial.id}>
74+
<span>{a.tutorial.name}</span>
75+
</li>
76+
))}
77+
</ul>
78+
<Heading> Saved Tutorials </Heading>
79+
<ul>
80+
{user.saved.map(a => (
81+
<li key={a.tutorial.id}>
82+
<span>{a.tutorial.name}</span>
83+
</li>
84+
))}
85+
</ul>
5586
</Layout>
5687
);
5788
};
5889
export default Profile;
90+
91+
const PROFILE_QUERY = gql`
92+
query profileQuery {
93+
viewer {
94+
user {
95+
id
96+
name
97+
githubHandle
98+
avatarUrl
99+
bio
100+
upvoted: userTutorials(where: { upvoted: true }) {
101+
tutorial {
102+
id
103+
name
104+
}
105+
}
106+
saved: userTutorials(where: { saved: true }) {
107+
tutorial {
108+
id
109+
name
110+
}
111+
}
112+
}
113+
}
114+
}
115+
`;

packages/gatsby-theme/src/utils/auth/WithCurrentUser.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const WithCurrentUser: React.FunctionComponent<WithCurrentUserProps> = ({
1616
children,
1717
}) => {
1818
return (
19-
<Query<CurrentUserQuery> query={CURRENT_USER} >
19+
<Query<CurrentUserQuery> query={CURRENT_USER}>
2020
{({ data, loading }) => {
2121
if (loading) {
2222
return children({ loading });
@@ -27,7 +27,7 @@ const WithCurrentUser: React.FunctionComponent<WithCurrentUserProps> = ({
2727
return children({ user: false });
2828
}}
2929
</Query>
30-
)
30+
);
3131
};
3232

3333
export const CURRENT_USER = gql`
@@ -37,11 +37,11 @@ export const CURRENT_USER = gql`
3737
user {
3838
id
3939
name
40-
avatarUrl
41-
githubHandle
40+
avatarUrl
41+
githubHandle
42+
}
4243
}
4344
}
44-
}
4545
`;
4646

4747
export default WithCurrentUser;

packages/server/src/graphql/Viewer.ts

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
import { queryField, objectType } from "yoga";
1+
import { queryField, objectType } from 'yoga'
22

33
export const Viewer = objectType({
4-
name: "Viewer",
5-
definition: (t) => {
6-
t.id("id");
7-
t.field("user", {
8-
type: "User",
9-
resolve: async (parent, _, ctx) => {
10-
const { id } = parent;
11-
return await ctx.prisma.user({ id })
12-
}
13-
})
14-
}
15-
});
4+
name: 'Viewer',
5+
definition: t => {
6+
t.id('id')
7+
t.field('user', {
8+
type: 'User',
9+
resolve: async (parent, _, ctx) => {
10+
const { id } = parent
11+
return await ctx.prisma.user({ id })
12+
},
13+
})
14+
},
15+
})
1616

17-
export const viewer = queryField("viewer", {
18-
type: Viewer,
19-
nullable: true,
20-
resolve: (_, args, ctx) => {
21-
try {
22-
const id = ctx.currentUserId
23-
return {
24-
id
25-
}
26-
} catch (e) {
27-
return null;
28-
}
17+
export const viewer = queryField('viewer', {
18+
type: Viewer,
19+
nullable: true,
20+
resolve: (_, args, ctx) => {
21+
try {
22+
const id = ctx.currentUserId
23+
if (!id) {
24+
return null
25+
}
26+
return {
27+
id,
28+
}
29+
} catch (e) {
30+
return null
2931
}
30-
})
32+
},
33+
})

0 commit comments

Comments
 (0)