Skip to content

Commit b5e5dda

Browse files
committed
chore: clean up axios calls
1 parent df0fbf4 commit b5e5dda

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/ui/components/Navbars/DashboardNavbarLinks.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,16 @@ const DashboardNavbarLinks: React.FC = () => {
5151

5252
const logout = async () => {
5353
try {
54-
await axios.post(
54+
const { data } = await axios.post(
5555
`${process.env.VITE_API_URI || 'http://localhost:3000'}/api/auth/logout`,
5656
{},
57-
getAxiosConfig(),
58-
)
59-
.then((res) => {
60-
if (!res.data.isAuth && !res.data.user) {
61-
setAuth(false);
62-
navigate(0);
63-
}
64-
});
57+
getAxiosConfig()
58+
);
59+
60+
if (!data.isAuth && !data.user) {
61+
setAuth(false);
62+
navigate(0);
63+
}
6564
} catch (error) {
6665
console.error('Logout failed:', error);
6766
}

src/ui/services/git-push.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ const baseUrl = import.meta.env.VITE_API_URI
55
? `${import.meta.env.VITE_API_URI}/api/v1`
66
: `${location.origin}/api/v1`;
77

8-
const getPush = async (id, setIsLoading, setData, setAuth, setIsError) => {
9-
const url = `${baseUrl}/push/${id}`;
10-
await axios(url, getAxiosConfig())
11-
.then((response) => {
8+
const getPush = async (id, setIsLoading, setData, setAuth, setIsError) => {
9+
const url = `${baseUrl}/push/${id}`;
10+
setIsLoading(true);
11+
12+
try {
13+
const response = await axios(url, getAxiosConfig());
1214
const data = response.data;
1315
data.diff = data.steps.find((x) => x.stepName === 'diff');
1416
setData(data);
15-
setIsLoading(false);
16-
})
17-
.catch((error) => {
18-
if (error.response && error.response.status === 401) setAuth(false);
17+
} catch (error) {
18+
if (error.response?.status === 401) setAuth(false);
1919
else setIsError(true);
20+
} finally {
2021
setIsLoading(false);
21-
});
22-
};
22+
}
23+
};
2324

2425
const getPushes = async (
2526
setIsLoading,
@@ -32,28 +33,29 @@ const getPushes = async (
3233
canceled: false,
3334
authorised: false,
3435
rejected: false,
35-
},
36+
}
3637
) => {
3738
const url = new URL(`${baseUrl}/push`);
3839
url.search = new URLSearchParams(query);
3940

4041
setIsLoading(true);
41-
await axios(url.toString(), getAxiosConfig())
42-
.then((response) => {
43-
const data = response.data;
44-
setData(data);
45-
})
46-
.catch((error) => {
47-
setIsError(true);
48-
if (error.response && error.response.status === 401) {
49-
setAuth(false);
50-
setErrorMessage(processAuthError(error));
51-
} else {
52-
setErrorMessage(`Error fetching pushes: ${error.response.data.message}`);
53-
}
54-
}).finally(() => {
55-
setIsLoading(false);
56-
});
42+
43+
try {
44+
const response = await axios(url.toString(), getAxiosConfig());
45+
setData(response.data);
46+
} catch (error) {
47+
setIsError(true);
48+
49+
if (error.response?.status === 401) {
50+
setAuth(false);
51+
setErrorMessage(processAuthError(error));
52+
} else {
53+
const message = error.response?.data?.message || error.message;
54+
setErrorMessage(`Error fetching pushes: ${message}`);
55+
}
56+
} finally {
57+
setIsLoading(false);
58+
}
5759
};
5860

5961
const authorisePush = async (id, setMessage, setUserAllowedToApprove, attestation) => {

0 commit comments

Comments
 (0)