Code cleanup, convert promises to async/await. (#6726)

This commit is contained in:
Xander Luciano 2024-03-16 15:21:52 -07:00 committed by GitHub
parent 06c7ebfc21
commit 1d9b39ab33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 27 deletions

View File

@ -22,12 +22,9 @@ export function setApiDefaults() {
api.defaults.baseURL = host; api.defaults.baseURL = host;
api.defaults.timeout = 2500; api.defaults.timeout = 2500;
api.defaults.headers.common['Authorization'] = token
if (!!token) { ? `Token ${token}`
api.defaults.headers.common['Authorization'] = `Token ${token}`; : undefined;
} else {
api.defaults.headers.common['Authorization'] = undefined;
}
if (!!getCsrfCookie()) { if (!!getCsrfCookie()) {
api.defaults.withCredentials = true; api.defaults.withCredentials = true;

View File

@ -114,16 +114,14 @@ export function OptionsApiForm({
props.pk, props.pk,
props.pathParams props.pathParams
], ],
queryFn: () => queryFn: async () => {
api.options(url).then((res) => { let response = await api.options(url);
let fields: Record<string, ApiFormFieldType> | null = {}; let fields: Record<string, ApiFormFieldType> | null = {};
if (!props.ignorePermissionCheck) {
if (!props.ignorePermissionCheck) { fields = extractAvailableFields(response, props.method);
fields = extractAvailableFields(res, props.method); }
} return fields;
},
return fields;
}),
throwOnError: (error: any) => { throwOnError: (error: any) => {
if (error.response) { if (error.response) {
invalidResponse(error.response.status); invalidResponse(error.response.status);
@ -134,7 +132,6 @@ export function OptionsApiForm({
color: 'red' color: 'red'
}); });
} }
return false; return false;
} }
}); });

View File

@ -37,20 +37,22 @@ export function Header() {
const notifications = useQuery({ const notifications = useQuery({
queryKey: ['notification-count'], queryKey: ['notification-count'],
queryFn: async () => { queryFn: async () => {
return api try {
.get(apiUrl(ApiEndpoints.notifications_list), { const params = {
params: { params: {
read: false, read: false,
limit: 1 limit: 1
} }
}) };
.then((response) => { let response = await api.get(
setNotificationCount(response.data.count); apiUrl(ApiEndpoints.notifications_list),
return response.data; params
}) );
.catch((error) => { setNotificationCount(response.data.count);
return error; return response.data;
}); } catch (error) {
return error;
}
}, },
refetchInterval: 30000, refetchInterval: 30000,
refetchOnMount: true, refetchOnMount: true,