Skip to content

Commit 2a81d2d

Browse files
committed
(refactor) - transform usePreviousValue checks to .then on executeMutation
1 parent 87f62ba commit 2a81d2d

File tree

6 files changed

+27
-43
lines changed

6 files changed

+27
-43
lines changed

server/prisma/prisma.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Specifies the HTTP endpoint of your Prisma API.
22
#endpoint: ''
3-
endpoint: ''
3+
endpoint: https://eu1.prisma.sh/jovi-de-croock-c8d40d/server/dev
44

55
# Defines your models, each model is mapped to the database as a table.
66
datamodel: datamodel.prisma

server/src/generated/prisma-client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var models = [
2020
exports.Prisma = prisma_lib_1.makePrismaClientClass({
2121
typeDefs,
2222
models,
23-
endpoint: ``,
23+
endpoint: `https://eu1.prisma.sh/jovi-de-croock-c8d40d/server/dev`,
2424
secret: `mysecret123`
2525
});
2626
exports.prisma = new exports.Prisma();

src/components/CreateLink.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import { useMutation } from 'urql';
33
import gql from 'graphql-tag';
4-
import usePreviousValue from '../hooks/usePreviousValue';
54

65
const POST_MUTATION = gql`
76
mutation PostMutation($description: String!, $url: String!) {
@@ -17,18 +16,13 @@ const POST_MUTATION = gql`
1716
const CreateLink = ({ history }) => {
1817
const [description, setDescription] = React.useState("");
1918
const [url, setUrl] = React.useState('');
20-
const [state, executeMutation] = useMutation(POST_MUTATION);
21-
const prevFetching = usePreviousValue(state.fetching);
22-
23-
React.useEffect(() => {
24-
if (state.fetching === false && prevFetching === true) {
25-
history.push('/new/1');
26-
}
27-
}, [state.fetching, prevFetching, history]);
19+
const [{ fetching }, executeMutation] = useMutation(POST_MUTATION);
2820

2921
const postMutation = React.useCallback(() => {
30-
executeMutation({ url, description });
31-
}, [url, description, executeMutation]);
22+
executeMutation({ url, description }).then(() => {
23+
history.push('/new/1');
24+
});
25+
}, [url, description, executeMutation, history]);
3226

3327
return (
3428
<div>
@@ -48,7 +42,7 @@ const CreateLink = ({ history }) => {
4842
placeholder="The URL for the link"
4943
/>
5044
</div>
51-
<button onClick={postMutation}>Submit</button>
45+
<button disabled={fetching} onClick={postMutation}>Submit</button>
5246
</div>
5347
);
5448
}

src/components/Link.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const VOTE_MUTATION = gql`
2525

2626
const Link = ({ link, index }) => {
2727
const authToken = localStorage.getItem(AUTH_TOKEN);
28-
const [, executeMutation] = useMutation(VOTE_MUTATION);
28+
const [{ fetching }, executeMutation] = useMutation(VOTE_MUTATION);
2929

3030
const voteMutation = React.useCallback(() => {
3131
executeMutation({ linkId: link.id });
@@ -36,7 +36,7 @@ const Link = ({ link, index }) => {
3636
<div className="flex items-center">
3737
<span className="gray">{index + 1}.</span>
3838
{authToken && (
39-
<div className="ml1 gray f11" onClick={voteMutation}>
39+
<div className="ml1 gray f11" onClick={voteMutation} disabled={fetching}>
4040
4141
</div>
4242
)}

src/components/Login.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import { useMutation } from 'urql';
33
import gql from 'graphql-tag';
4-
import usePreviousValue from '../hooks/usePreviousValue';
54
import { AUTH_TOKEN } from "../constants";
65

76
const SIGNUP_MUTATION = gql`
@@ -26,21 +25,17 @@ const Login = ({ history }) => {
2625
const [password, setPassword] = React.useState('');
2726
const [name, setName] = React.useState('');
2827

29-
const [{ fetching, data }, executeMutation] = useMutation(login ? LOGIN_MUTATION : SIGNUP_MUTATION);
30-
const prevFetching = usePreviousValue(fetching);
28+
const [{ fetching }, executeMutation] = useMutation(login ? LOGIN_MUTATION : SIGNUP_MUTATION);
3129

3230
const mutate = React.useCallback(() => {
33-
executeMutation({ email, password, name });
31+
executeMutation({ email, password, name }).then(result => {
32+
console.log(result);
33+
const { token } = login ? result.login : result.signup;
34+
localStorage.setItem(AUTH_TOKEN, token);
35+
history.push(`/`);
36+
});
3437
}, [email, password, name, executeMutation]);
3538

36-
React.useEffect(() => {
37-
if (prevFetching === true && fetching === false && data) {
38-
const { token } = login ? data.login : data.signup;
39-
localStorage.setItem(AUTH_TOKEN, token);
40-
history.push(`/`);
41-
}
42-
}, [prevFetching, fetching, history, data, login])
43-
4439
return (
4540
<div>
4641
<h4 className="mv3">{login ? "Login" : "Sign Up"}</h4>
@@ -67,15 +62,21 @@ const Login = ({ history }) => {
6762
/>
6863
</div>
6964
<div className="flex mt3">
70-
<div className="pointer mr2 button" onClick={mutate}>
65+
<button
66+
type="button"
67+
className="pointer mr2 button"
68+
onClick={mutate}
69+
disabled={fetching}
70+
>
7171
{login ? "login" : "create account"}
72-
</div>
73-
<div
72+
</button>
73+
<button
74+
type="button"
7475
className="pointer button"
7576
onClick={() => setLogin(!login)}
7677
>
7778
{login ? "need to create an account?" : "already have an account?"}
78-
</div>
79+
</button>
7980
</div>
8081
</div>
8182
);

src/hooks/usePreviousValue.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)