Skip to content

Commit 9d52b6d

Browse files
committed
(chore) - make some code quality optimisations like moving to getToken setToken helpers
1 parent 495adf7 commit 9d52b6d

File tree

7 files changed

+28
-15
lines changed

7 files changed

+28
-15
lines changed

src/components/CreateLink.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ const CreateLink = ({ history }) => {
4242
placeholder="The URL for the link"
4343
/>
4444
</div>
45-
<button type="button" disabled={fetching} onClick={postMutation}>Submit</button>
45+
<button type="button" disabled={fetching} onClick={postMutation}>
46+
Submit
47+
</button>
4648
</div>
4749
);
4850
}

src/components/Header.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22
import { Link } from 'react-router-dom';
33
import { withRouter } from 'react-router';
4-
import { AUTH_TOKEN } from '../constants';
4+
import { getToken, removeToken } from "../utils";
55

66
const Header = ({ history }) => {
7-
const authToken = localStorage.getItem(AUTH_TOKEN);
7+
const authToken = getToken();
88
return (
99
<div className="flex pa1 justify-between nowrap orange">
1010
<div className="flex flex-fixed black">
@@ -34,8 +34,8 @@ const Header = ({ history }) => {
3434
<div
3535
className="ml1 pointer black"
3636
onClick={() => {
37-
localStorage.removeItem(AUTH_TOKEN)
38-
history.push(`/`)
37+
removeToken();
38+
history.push(`/`);
3939
}}
4040
>
4141
logout

src/components/Link.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import { useMutation } from 'urql';
33
import gql from 'graphql-tag';
4-
import { timeDifferenceForDate } from "../utils";
5-
import { AUTH_TOKEN } from "../constants";
4+
import { timeDifferenceForDate, getToken } from "../utils";
65

76
const VOTE_MUTATION = gql`
87
mutation VoteMutation($linkId: ID!) {
@@ -24,13 +23,13 @@ const VOTE_MUTATION = gql`
2423
`;
2524

2625
const Link = ({ link, index }) => {
27-
const authToken = localStorage.getItem(AUTH_TOKEN);
26+
const authToken = getToken();
2827
const [{ fetching }, executeMutation] = useMutation(VOTE_MUTATION);
2928

3029
const voteMutation = React.useCallback(() => {
3130
executeMutation({ linkId: link.id });
3231
}, [link, executeMutation]);
33-
console.log(link);
32+
3433
return (
3534
<div className="flex mt2 items-start">
3635
<div className="flex items-center">

src/components/LinkList.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ const NEW_VOTES_SUBSCRIPTION = gql`
7878

7979
const LinkList = ({ location, match, history }) => {
8080
const isNewPage = location.pathname.includes("new");
81-
const getQueryVariables = React.useCallback(() => {
81+
82+
const variables = React.useMemo(() => {
8283
const page = parseInt(match.params.page, 10);
8384

8485
const skip = isNewPage ? (page - 1) * LINKS_PER_PAGE : 0;
@@ -89,7 +90,7 @@ const LinkList = ({ location, match, history }) => {
8990

9091
const [{ data, error, fetching }] = useQuery({
9192
query: FEED_QUERY,
92-
variables: getQueryVariables(),
93+
variables,
9394
});
9495

9596
useSubscription({ query: NEW_VOTES_SUBSCRIPTION });

src/components/Login.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { useMutation } from 'urql';
33
import gql from 'graphql-tag';
4-
import { AUTH_TOKEN } from "../constants";
4+
import { setToken } from '../utils';
55

66
const SIGNUP_MUTATION = gql`
77
mutation SignupMutation($email: String!, $password: String!, $name: String!) {
@@ -30,7 +30,7 @@ const Login = ({ history }) => {
3030
const mutate = React.useCallback(() => {
3131
executeMutation({ email, password, name }).then(({ data }) => {
3232
const { token } = login ? data.login : data.signup;
33-
localStorage.setItem(AUTH_TOKEN, token);
33+
setToken(token);
3434
history.push(`/`);
3535
});
3636
}, [email, password, name, executeMutation, history, login]);

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import { cacheExchange } from '@urql/exchange-graphcache';
1313
import { BrowserRouter } from 'react-router-dom';
1414
import "./styles/index.css";
1515
import App from "./components/App";
16-
import { AUTH_TOKEN, LINKS_PER_PAGE } from './constants';
16+
import { LINKS_PER_PAGE } from './constants';
1717
import { FEED_QUERY } from './components/LinkList';
1818
import * as serviceWorker from "./serviceWorker";
19+
import { getToken } from './utils';
1920

2021
const cache = cacheExchange({
2122
keys: {
@@ -27,6 +28,9 @@ const cache = cacheExchange({
2728
// Optimistic is not possible due to the nature of the data-structure
2829
// every vote needs to have an unique id for a unique userId...
2930

31+
// We could write a resolver to format createdAt to the new value as well.
32+
// This would be dependent on a new version of the graphcache.
33+
3034
// Mutations that need updates due to adding/removing/... to a list
3135
// Will most likely come here. We can't make assumptions for adding to
3236
// a list.
@@ -67,14 +71,15 @@ const subscriptionClient = new SubscriptionClient(
6771

6872
const client = createClient({
6973
fetchOptions: () => {
70-
const token = localStorage.getItem(AUTH_TOKEN);
74+
const token = getToken();
7175
return {
7276
headers: {
7377
authorization: token ? `Bearer ${token}` : ""
7478
}
7579
}
7680
},
7781
url: "http://localhost:4000",
82+
// TODO: suspense is waiting for new urql version
7883
// suspense: true,
7984
exchanges: [
8085
dedupExchange,

src/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AUTH_TOKEN } from "./constants";
2+
13
function timeDifference(current, previous) {
24
const milliSecondsPerMinute = 60 * 1000;
35
const milliSecondsPerHour = milliSecondsPerMinute * 60;
@@ -31,3 +33,7 @@ export function timeDifferenceForDate(date) {
3133
const updated = new Date(date).getTime();
3234
return timeDifference(now, updated);
3335
}
36+
37+
export const getToken = () => window.localStorage.getItem(AUTH_TOKEN);
38+
export const setToken = (token) => window.localStorage.setItem(AUTH_TOKEN, token);
39+
export const removeToen = () => window.localStorage.removeItem(AUTH_TOKEN);

0 commit comments

Comments
 (0)