Skip to content

Commit 464f502

Browse files
committed
updates
1 parent c4a343a commit 464f502

File tree

7 files changed

+86
-33
lines changed

7 files changed

+86
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hackernews-react-apollo",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"private": true,
55
"dependencies": {
66
"@apollo/client": "^3.2.2",

server/prisma/dev.db

0 Bytes
Binary file not shown.

server/src/resolvers/Query.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,25 @@ const feed = async (parent, args, context) => {
2525
take: args.take
2626
});
2727

28-
const count = links.length;
28+
const count = await context.prisma.link.count({
29+
where: {
30+
OR: [
31+
{
32+
url: {
33+
contains: args.filter
34+
}
35+
},
36+
{
37+
description: {
38+
contains: args.filter
39+
}
40+
}
41+
]
42+
}
43+
});
2944

3045
return {
46+
id: args.skip,
3147
links,
3248
count
3349
};

server/src/schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const schema = gql`
2323
}
2424
2525
type Feed {
26+
id: ID!
2627
links: [Link!]!
2728
count: Int!
2829
}

src/components/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class App extends Component {
1313
<Header />
1414
<div className="ph3 pv1 background-gray">
1515
<Switch>
16-
{/* <Route exact path="/" render={() => <Redirect to="/new/1" />} />
17-
*/}
16+
<Route exact path="/" render={() => <Redirect to="/new/1" />} />
17+
1818
<Route
1919
exact
2020
path="/create"

src/components/Link.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { FEED_QUERY } from './LinkList';
23
import { AUTH_TOKEN } from '../constants';
34
import { timeDifferenceForDate } from '../utils';
45

@@ -30,6 +31,30 @@ const Link = (props) => {
3031
{
3132
variables: {
3233
linkId: link.id
34+
},
35+
update(cache, { data: { vote } }) {
36+
const { feed } = cache.readQuery({
37+
query: FEED_QUERY
38+
});
39+
40+
const updatedLinks = feed.links.map((feedLink) => {
41+
if (feedLink.id === link.id) {
42+
return {
43+
...feedLink,
44+
votes: [...feedLink.votes, vote]
45+
};
46+
}
47+
return feedLink;
48+
});
49+
50+
cache.writeQuery({
51+
query: FEED_QUERY,
52+
data: {
53+
feed: {
54+
links: updatedLinks
55+
}
56+
}
57+
});
3358
}
3459
}
3560
);
@@ -45,28 +70,6 @@ const Link = (props) => {
4570
>
4671
4772
</div>
48-
{/* {authToken && (
49-
<Mutation
50-
mutation={VOTE_MUTATION}
51-
variables={{ linkId: this.props.link.id }}
52-
update={(store, { data: { vote } }) =>
53-
this.props.updateStoreAfterVote(
54-
store,
55-
vote,
56-
this.props.link.id
57-
)
58-
}
59-
>
60-
{(voteMutation) => (
61-
<div
62-
className="ml1 gray f11"
63-
onClick={voteMutation}
64-
>
65-
66-
</div>
67-
)}
68-
</Mutation>
69-
)} */}
7073
</div>
7174
<div className="ml1">
7275
<div>

src/components/LinkList.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const FEED_QUERY = gql`
1212
$orderBy: LinkOrderByInput
1313
) {
1414
feed(take: $take, skip: $skip, orderBy: $orderBy) {
15+
id
1516
links {
1617
id
1718
createdAt
@@ -92,13 +93,36 @@ const getLinksToRender = (isNewPage, data) => {
9293
return rankedLinks;
9394
};
9495

96+
const getQueryVariables = (isNewPage, page) => {
97+
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0;
98+
const take = isNewPage ? LINKS_PER_PAGE : 100;
99+
const orderBy = isNewPage ? 'createdAt_DESC' : null;
100+
return { take, skip, orderBy };
101+
};
102+
95103
const LinkList = () => {
96-
const { data, loading, error } = useQuery(FEED_QUERY);
97104
const history = useHistory();
98105
const isNewPage = history.location.pathname.includes(
99106
'new'
100107
);
101-
const pageIndex = 1;
108+
const pageIndexParams = history.location.pathname.split(
109+
'/'
110+
);
111+
const page = parseInt(
112+
pageIndexParams[pageIndexParams.length - 1]
113+
);
114+
115+
const pageIndex = page ? (page - 1) * LINKS_PER_PAGE : 0;
116+
117+
const {
118+
data,
119+
loading,
120+
error,
121+
subscribeToMore
122+
} = useQuery(FEED_QUERY, {
123+
variables: getQueryVariables(isNewPage, page)
124+
});
125+
102126
return (
103127
<>
104128
{loading && <p>Loading...</p>}
@@ -111,23 +135,32 @@ const LinkList = () => {
111135
key={link.id}
112136
link={link}
113137
index={index + pageIndex}
114-
updateStoreAfterVote={() =>
115-
console.log('update')
116-
}
117138
/>
118139
)
119140
)}
120141
{isNewPage && (
121142
<div className="flex ml4 mv3 gray">
122143
<div
123144
className="pointer mr2"
124-
onClick={() => console.log('prev page')}
145+
onClick={() => {
146+
if (page > 1) {
147+
history.push(`/new/${page - 1}`);
148+
}
149+
}}
125150
>
126151
Previous
127152
</div>
128153
<div
129154
className="pointer"
130-
onClick={() => console.log('next page')}
155+
onClick={() => {
156+
if (
157+
page <=
158+
data.feed.count / LINKS_PER_PAGE
159+
) {
160+
const nextPage = page + 1;
161+
history.push(`/new/${nextPage}`);
162+
}
163+
}}
131164
>
132165
Next
133166
</div>

0 commit comments

Comments
 (0)