async function apiLogin(email, password){ try { const formData = new URLSearchParams(); formData.append('username', email); formData.append('password', password); const response = await fetch(`/api/login`, { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: formData.toString() }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching login:", error); throw error; } } async function apiLogout(){ try { const response = await fetch(`/api/logout`, { method: 'POST', headers: {'Content-Type': 'application/json'} }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching logout:", error); throw error; } } async function apiGetCurrentUser(){ try { const response = await fetch(`/api/current_user`, { method: 'GET', headers: { 'Content-Type': 'application/json', } }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const userData = await response.json(); return userData; } catch (error) { return {id: null} // console.error("Error fetching current user:", error); // throw error; // Re-throw the error for the calling code to handle } } async function apiUpdateUser(userId, updateData){ try { const response = await fetch(`/api/users/${userId}`, { method: 'PATCH', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(updateData) }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching user:", error); throw error; } } async function apiCreateUser(name, surname, email, password1, securityCode){ try { const response = await fetch(`/api/users`, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ name: name, surname: surname, mail: email, password: password1, registration_security_key: securityCode }) }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { // console.error(error); throw error; } } async function apiGetUser(userId){ try { const response = await fetch(`/api/users/${userId}`, { method: 'GET', headers: {'Content-Type': 'application/json'} }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching project:", error); throw error; } } async function apiGetUsers(){ try { const response = await fetch(`/api/users`, { method: 'GET', headers: {'Content-Type': 'application/json'} }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching projects:", error); throw error; } } async function apiCreateProject(){ try { const response = await fetch(`/api/projects`, { method: 'POST', headers: {'Content-Type': 'application/json'} }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching project:", error); throw error; } } async function apiGetProject(projectId){ try { const response = await fetch(`/api/projects/${projectId}`, { method: 'GET', headers: {'Content-Type': 'application/json'} }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching project:", error); throw error; } } function createFormDataFromObject(data) { const formData = new URLSearchParams(); for (const key in data) { if (Object.hasOwnProperty.call(data, key)) { const value = data[key]; // Handle different data types if needed if (Array.isArray(value)) { // If the value is an array, append each element with the same key value.forEach(item => { formData.append(key, item); }); } else if (value !== undefined && value !== null) { // Append non-null and non-undefined values directly formData.append(key, value); } // You might want to add handling for other complex types (e.g., nested objects) // depending on your API's expectations. } } return formData; } async function apiUpdateProject(projectId, updateData){ try { const response = await fetch(`/api/projects/${projectId}`, { method: 'PATCH', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(updateData) }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching project:", error); throw error; } } async function apiGetProjects(){ try { const response = await fetch(`/api/projects`, { method: 'GET', headers: {'Content-Type': 'application/json'} }); if (!response.ok) { const errorData = await response.json(); if (errorData?.detail) { throw new Error(errorData?.detail); } throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Unknown error'}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching projects:", error); throw error; } } async function apiDeleteIfcFile(projectId) { try { const response = await fetch(`/api/ifc_file/${projectId}`, { method: 'DELETE', headers: { }, }); if (!response.ok) { const errorData = await response.json(); throw new Error(`HTTP error! status: ${response.status}, message: ${errorData?.detail || 'Failed to delete file'}`); } console.log(`File for project ${projectId} deleted successfully.`); return true; } catch (error) { console.error("Error deleting file:", error); throw error; } } async function apiUploadIfcFile(projectId) { const fileInput = document.createElement('input'); fileInput.type = 'file'; fileInput.accept = '.ifc'; fileInput.onchange = async (event) => { const file = event.target.files[0]; if (!file) { return; // No file selected } if (!file.name.endsWith('.ifc')) { alert('Only .ifc files are allowed.'); return; } const formData = new FormData(); formData.append('file', file); try { const response = await fetch(`/api/ifc_file/${projectId}`, { method: 'POST', headers: { }, body: formData, }); if (response.ok) { const data = await response.json(); app.updateState(); // alert(`File "${data.filename}" uploaded successfully to project ${data.project_id}. Message: ${data.message}`); // Optionally, you can update the UI or perform other actions upon successful upload } else { const errorData = await response.json(); alert(`File upload failed: ${errorData.detail || response.statusText}`); } } catch (error) { console.error('Error uploading file:', error); alert('An error occurred during file upload.'); } }; fileInput.click(); }