Skip to content

Commit 2cbb14d

Browse files
committed
refactor: add useProfile() hook
1 parent 10e7ed6 commit 2cbb14d

4 files changed

Lines changed: 27 additions & 19 deletions

File tree

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"web-vitals": "^1.0.1"
2828
},
2929
"devDependencies": {
30+
"@types/react-gravatar": "^2.6.8",
3031
"@types/react-router-dom": "^5.1.7",
3132
"@typescript-eslint/eslint-plugin": "4.24.0",
3233
"@typescript-eslint/parser": "4.24.0",

src/api/users.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import axios from 'axios';
2+
import { useQuery } from 'react-query';
3+
import useToken from '../utils/useToken';
4+
5+
function useProfile() {
6+
const { token } = useToken();
7+
8+
return useQuery<{ email: string }, Error>('profile', async () => {
9+
const { data } = await axios.get('http://localhost:3000/users/stan', {
10+
headers: {
11+
Authorization: `Bearer ${token}`,
12+
},
13+
});
14+
return data;
15+
});
16+
}
17+
18+
export default useProfile;

src/components/Dashboard.tsx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Editor from '@monaco-editor/react';
44
import axios, { AxiosResponse } from 'axios';
55
import { Fragment, useRef, useState } from 'react';
66
import Gravatar from 'react-gravatar';
7-
import { useQuery } from 'react-query';
7+
import useProfile from '../api/users';
88
import useToken from '../utils/useToken';
99
import Result from './Result';
1010

@@ -21,18 +21,8 @@ export default function Example() {
2121
const [status, setStatus] = useState('');
2222
const [output, setOutput] = useState('');
2323
const { token } = useToken();
24-
const { isLoading, isError, data, error } = useQuery<
25-
{ email: string },
26-
Error
27-
>('profile', () =>
28-
axios
29-
.get('http://localhost:3000/users/stan', {
30-
headers: {
31-
Authorization: `Bearer ${token}`,
32-
},
33-
})
34-
.then((res) => res.data)
35-
);
24+
25+
const { isLoading, isError, data, error } = useProfile();
3626

3727
if (isLoading) {
3828
return <span>Loading...</span>;
@@ -44,8 +34,6 @@ export default function Example() {
4434
}
4535
}
4636

47-
console.log(data);
48-
4937
function handleEditorDidMount(editor: any, monaco: any) {
5038
editorRef.current = editor;
5139
}
@@ -76,7 +64,7 @@ export default function Example() {
7664
headers: {
7765
Authorization: `Bearer ${token}`,
7866
},
79-
}
67+
},
8068
);
8169

8270
setJobID(res.data.id);
@@ -125,7 +113,7 @@ export default function Example() {
125113
>
126114
{item}
127115
</a>
128-
)
116+
),
129117
)}
130118
</div>
131119
</div>
@@ -171,7 +159,7 @@ export default function Example() {
171159
href="/"
172160
className={classNames(
173161
active ? 'bg-gray-100' : '',
174-
'block px-4 py-2 text-sm text-gray-700'
162+
'block px-4 py-2 text-sm text-gray-700',
175163
)}
176164
>
177165
{item}
@@ -221,7 +209,7 @@ export default function Example() {
221209
>
222210
{item}
223211
</a>
224-
)
212+
),
225213
)}
226214
</div>
227215
<div className="pt-4 pb-3 border-t border-gray-700">

0 commit comments

Comments
 (0)