139 lines
4.5 KiB
TypeScript
139 lines
4.5 KiB
TypeScript
interface Project {
|
|
id: string,
|
|
name: string;
|
|
description: string;
|
|
object: JSON;
|
|
preview_url: string;
|
|
}
|
|
|
|
export const getProjects = async () => {
|
|
try {
|
|
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/projects/`, {
|
|
method: 'GET',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
const data = await response.json();
|
|
if (data?.token) {
|
|
localStorage.setItem('canvas_token', data.token);
|
|
// Remove the token from the response data
|
|
const { token, ...restData } = data;
|
|
return restData; // Return modified data without token
|
|
}
|
|
else if (!data?.token) {
|
|
localStorage.removeItem("canvas_token");
|
|
return data;
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to get projects:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const getProjectById = async (projectId: string) => {
|
|
try {
|
|
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/projects/each/${projectId}`, {
|
|
method: 'GET',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
const data = await response.json();
|
|
if (data?.token) {
|
|
localStorage.setItem('canvas_token', data.token);
|
|
// Remove the token from the response data
|
|
const { token, ...restData } = data;
|
|
return restData; // Return modified data without token
|
|
}
|
|
else if (!data?.token) {
|
|
localStorage.removeItem("canvas_token");
|
|
return data;
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to get project by ID:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const createProject = async () => {
|
|
try {
|
|
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/projects/create`, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
|
|
const data = await response.json();
|
|
if (data?.token) {
|
|
localStorage.setItem('canvas_token', data.token);
|
|
// Remove the token from the response data
|
|
const { token, ...restData } = data;
|
|
return restData; // Return modified data without token
|
|
}
|
|
else if (!data?.token) {
|
|
localStorage.removeItem("canvas_token");
|
|
return data;
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to create project:', error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const updateProject = async (body: Project) => {
|
|
try {
|
|
const { id, name, description, object, preview_url } = body;
|
|
|
|
const project = { name, description, object, preview_url };
|
|
|
|
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/projects/update/${id}`, {
|
|
method: 'PUT',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(project),
|
|
})
|
|
const data = await response.json();
|
|
if (data?.token) {
|
|
localStorage.setItem('canvas_token', data.token);
|
|
// Remove the token from the response data
|
|
const { token, ...restData } = data;
|
|
return restData; // Return modified data without token
|
|
}
|
|
else if (!data?.token) {
|
|
localStorage.removeItem("canvas_token");
|
|
return data;
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to update project:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
export const deleteProject = async (projectId: string) => {
|
|
try {
|
|
const response = await fetch(`${import.meta.env.VITE_SERVER_URL}/projects/delete/${projectId}`, {
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
const data = await response.json();
|
|
if (data?.token) {
|
|
localStorage.setItem('canvas_token', data.token);
|
|
// Remove the token from the response data
|
|
const { token, ...restData } = data;
|
|
return restData; // Return modified data without token
|
|
}
|
|
else if (!data?.token) {
|
|
localStorage.removeItem("canvas_token");
|
|
return data;
|
|
}
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Failed to delete project:', error);
|
|
throw error;
|
|
}
|
|
}
|
|
|