From 1d9b39ab33fab390d867e9fe1b767b1114a9e9c7 Mon Sep 17 00:00:00 2001 From: Xander Luciano Date: Sat, 16 Mar 2024 15:21:52 -0700 Subject: [PATCH] Code cleanup, convert promises to async/await. (#6726) --- src/frontend/src/App.tsx | 9 +++----- src/frontend/src/components/forms/ApiForm.tsx | 19 +++++++--------- src/frontend/src/components/nav/Header.tsx | 22 ++++++++++--------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 2ce7225d00..0ec3e8576c 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -22,12 +22,9 @@ export function setApiDefaults() { api.defaults.baseURL = host; api.defaults.timeout = 2500; - - if (!!token) { - api.defaults.headers.common['Authorization'] = `Token ${token}`; - } else { - api.defaults.headers.common['Authorization'] = undefined; - } + api.defaults.headers.common['Authorization'] = token + ? `Token ${token}` + : undefined; if (!!getCsrfCookie()) { api.defaults.withCredentials = true; diff --git a/src/frontend/src/components/forms/ApiForm.tsx b/src/frontend/src/components/forms/ApiForm.tsx index 53fdabe9c7..dbdc37e132 100644 --- a/src/frontend/src/components/forms/ApiForm.tsx +++ b/src/frontend/src/components/forms/ApiForm.tsx @@ -114,16 +114,14 @@ export function OptionsApiForm({ props.pk, props.pathParams ], - queryFn: () => - api.options(url).then((res) => { - let fields: Record | null = {}; - - if (!props.ignorePermissionCheck) { - fields = extractAvailableFields(res, props.method); - } - - return fields; - }), + queryFn: async () => { + let response = await api.options(url); + let fields: Record | null = {}; + if (!props.ignorePermissionCheck) { + fields = extractAvailableFields(response, props.method); + } + return fields; + }, throwOnError: (error: any) => { if (error.response) { invalidResponse(error.response.status); @@ -134,7 +132,6 @@ export function OptionsApiForm({ color: 'red' }); } - return false; } }); diff --git a/src/frontend/src/components/nav/Header.tsx b/src/frontend/src/components/nav/Header.tsx index 22320c24ae..b4f139cfb8 100644 --- a/src/frontend/src/components/nav/Header.tsx +++ b/src/frontend/src/components/nav/Header.tsx @@ -37,20 +37,22 @@ export function Header() { const notifications = useQuery({ queryKey: ['notification-count'], queryFn: async () => { - return api - .get(apiUrl(ApiEndpoints.notifications_list), { + try { + const params = { params: { read: false, limit: 1 } - }) - .then((response) => { - setNotificationCount(response.data.count); - return response.data; - }) - .catch((error) => { - return error; - }); + }; + let response = await api.get( + apiUrl(ApiEndpoints.notifications_list), + params + ); + setNotificationCount(response.data.count); + return response.data; + } catch (error) { + return error; + } }, refetchInterval: 30000, refetchOnMount: true,