Skip to content

Commit 7ed8e22

Browse files
committed
(fix) - make the updates work again
1 parent 5ced74d commit 7ed8e22

File tree

6 files changed

+51
-38
lines changed

6 files changed

+51
-38
lines changed

server/src/resolvers/Mutation.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ function post(parent, { url, description }, context) {
77
return context.prisma.createLink({
88
url,
99
description,
10-
/*
10+
votes: [],
1111
postedBy: {
12-
connect: {
13-
id: userId
14-
}
12+
// connect: {
13+
// id: userId
14+
// }
1515
}
16-
*/
1716
})
1817
}
1918

src/components/CreateLink.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ const POST_MUTATION = gql`
99
createdAt
1010
url
1111
description
12+
postedBy {
13+
id
14+
name
15+
}
16+
votes {
17+
id
18+
user {
19+
id
20+
}
21+
}
1222
}
1323
}
1424
`;
@@ -23,7 +33,7 @@ const CreateLink = props => {
2333
executeMutation({ url, description }).then(() => {
2434
props.history.push('/new/1');
2535
});
26-
}, [executeMutation, url, description, executeMutation, props.history]);
36+
}, [url, description, executeMutation, props.history]);
2737

2838
return (
2939
<div>

src/components/Header.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { withRouter } from 'react-router';
44

55
import { getToken, deleteToken } from '../token';
66

7-
const Header = ({ history }) => {
7+
const Header = (props) => {
88
const isLoggedIn = !!getToken();
99

1010
return (
@@ -32,21 +32,21 @@ const Header = ({ history }) => {
3232
)}
3333
</div>
3434
<div className="flex flex-fixed">
35-
{isLoggedn ? (
35+
{isLoggedIn ? (
3636
<div
3737
className="ml1 pointer black"
3838
onClick={() => {
39-
deleteToken()
40-
props.history.push('/')
39+
deleteToken();
40+
props.history.push("/");
4141
}}
4242
>
4343
logout
4444
</div>
4545
) : (
46-
<Link to="/login" className="ml1 no-underline black">
47-
login
46+
<Link to="/login" className="ml1 no-underline black">
47+
login
4848
</Link>
49-
)}
49+
)}
5050
</div>
5151
</div>
5252
);

src/components/LinkList.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ const LinkList = props => {
9292
useSubscription({ query: NEW_VOTES_SUBSCRIPTION });
9393
useSubscription({ query: NEW_LINKS_SUBSCRIPTION });
9494

95+
console.log(data && data.feed);
9596
const linksToRender = React.useMemo(() => {
96-
if (!data) {
97+
if (!data || !data.feed) {
9798
return [];
9899
} else if (isNewPage) {
99100
return data.feed.links;
@@ -104,12 +105,12 @@ const LinkList = props => {
104105
return rankedLinks;
105106
}
106107
}, [data, isNewPage]);
107-
108+
console.log(linksToRender);
108109
const nextPage = React.useCallback(() => {
109110
if (page <= data.feed.count / 10) {
110111
props.history.push(`/new/${page + 1}`);
111112
}
112-
}, [props.history, data.feed.count, page]);
113+
}, [props.history, data, page]);
113114

114115
const previousPage = React.useCallback(() => {
115116
if (page > 1) {

src/components/Login.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ const LOGIN_MUTATION = gql`
2121
`;
2222

2323
const Login = props => {
24-
const [login, setLogin] = React.useState(true);
24+
const [isLogin, setLogin] = React.useState(true);
2525
const [email, setEmail] = React.useState('');
2626
const [password, setPassword] = React.useState('');
2727
const [name, setName] = React.useState('');
2828

29-
const [state, executeMutation] = useMutation(
29+
const [{ fetching }, executeMutation] = useMutation(
3030
isLogin ? LOGIN_MUTATION : SIGNUP_MUTATION
3131
);
3232

@@ -39,13 +39,13 @@ const Login = props => {
3939
props.history.push('/')
4040
}
4141
});
42-
}, [executeMutation, props.history, setToken, email, password, name]);
42+
}, [executeMutation, props.history, email, password, name, isLogin]);
4343

4444
return (
4545
<div>
46-
<h4 className="mv3">{login ? 'Login' : 'Sign Up'}</h4>
46+
<h4 className="mv3">{isLogin ? "Login" : "Sign Up"}</h4>
4747
<div className="flex flex-column">
48-
{!login && (
48+
{!isLogin && (
4949
<input
5050
value={name}
5151
onChange={e => setName(e.target.value)}
@@ -73,14 +73,14 @@ const Login = props => {
7373
onClick={mutate}
7474
disabled={fetching}
7575
>
76-
{login ? 'login' : 'create account'}
76+
{isLogin ? "login" : "create account"}
7777
</button>
7878
<button
7979
type="button"
8080
className="pointer button"
81-
onClick={() => setLogin(!login)}
81+
onClick={() => setLogin(!isLogin)}
8282
>
83-
{login ? 'need to create an account?' : 'already have an account?'}
83+
{isLogin ? "need to create an account?" : "already have an account?"}
8484
</button>
8585
</div>
8686
</div>

src/index.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,38 @@ import { FEED_QUERY } from './components/LinkList';
2121
import { getToken } from './token';
2222

2323
const cache = cacheExchange({
24+
keys: {
25+
Feed: data => data.id || null,
26+
Link: data => data.id || null,
27+
Post: data => data.id || null
28+
},
2429
updates: {
2530
Mutation: {
2631
post: ({ post }, _args, cache) => {
27-
const variables = { first: 10, skip: 0, orderBy: 'createdAt_DESC' }
32+
const variables = { first: 10, skip: 0, orderBy: "createdAt_DESC" };
2833
cache.updateQuery({ query: FEED_QUERY, variables }, data => {
2934
if (data !== null) {
30-
data.feed.links.unshift(post)
31-
data.feed.count++
32-
return data
35+
data.feed.links.unshift(post);
36+
data.feed.count++;
37+
return data;
3338
} else {
34-
return null
39+
return null;
3540
}
36-
})
41+
});
3742
}
3843
},
3944
Subscription: {
4045
newLink: ({ newLink }, _args, cache) => {
41-
const variables = { first: 10, skip: 0, orderBy: 'createdAt_DESC' }
46+
const variables = { first: 10, skip: 0, orderBy: "createdAt_DESC" };
4247
cache.updateQuery({ query: FEED_QUERY, variables }, data => {
4348
if (data !== null) {
44-
data.feed.links.unshift(newLink)
45-
data.feed.count++
46-
return data
49+
data.feed.links.unshift(newLink);
50+
data.feed.count++;
51+
return data;
4752
} else {
48-
return null
53+
return null;
4954
}
50-
})
55+
});
5156
}
5257
}
5358
}
@@ -89,5 +94,3 @@ ReactDOM.render(
8994
</BrowserRouter>,
9095
document.getElementById('root'),
9196
);
92-
93-
serviceWorker.unregister();

0 commit comments

Comments
 (0)