Fixed file upload

This commit is contained in:
Benjamin Sherriff
2023-10-24 08:42:21 -04:00
parent f3eff8e310
commit ece4154b4e
7 changed files with 41 additions and 30 deletions

View File

@@ -15,4 +15,5 @@ export interface User {
role: string;
first_name: string;
last_name: string;
profile_picture?: string;
}

View File

@@ -21,19 +21,23 @@ interface PostOptions {
export async function post(endpoint: string, body: any, options?: PostOptions): Promise<Response> {
const url = `${baseURL}/${endpoint}`;
const headers = options?.headers || {};
let response;
if (!options?.type || options.type === 'json') {
body = JSON.stringify(body);
headers['Content-Type'] = 'application/json';
} else if (options.type === 'form') {
headers['Content-Type'] = 'multipart/form-data';
response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(body)
});
} else {
response = await fetch(url, {
method: 'POST',
credentials: 'include',
body
});
}
const response = await fetch(url, {
method: 'POST',
headers: headers,
credentials: 'include',
body
});
return response;
}

View File

@@ -28,11 +28,13 @@ export default function Header() {
if (response) {
setRefreshId(refreshLoggedIn());
setUser(response.user);
getPicture().then((response) => {
if (response) {
setProfilePicture(response as File);
}
});
if (response.user.profile_picture) {
getPicture().then((response) => {
if (response) {
setProfilePicture(response as File);
}
});
}
}
});
}
@@ -172,11 +174,13 @@ export default function Header() {
toggle={toggle}
setUser={(u) => {
setUser(u);
getPicture().then((response) => {
if (response) {
setProfilePicture(response as File);
}
});
if (u.profile_picture) {
getPicture().then((response) => {
if (response) {
setProfilePicture(response as File);
}
});
}
}}
setRefreshId={setRefreshId}
/>