2025-01-15 14:18:48 +01:00
|
|
|
import { defineStore, storeToRefs } from "pinia";
|
2025-01-15 16:24:34 +01:00
|
|
|
import { useUserStore } from "./user";
|
2024-07-10 18:43:46 +02:00
|
|
|
|
2024-09-10 08:49:09 +02:00
|
|
|
export const useApiStore = defineStore("api", () => {
|
2025-01-15 16:24:34 +01:00
|
|
|
const userStore = useUserStore();
|
|
|
|
|
|
2024-10-28 15:33:52 +01:00
|
|
|
async function fetchData(path = window.location.pathname) {
|
2024-09-04 11:28:12 +02:00
|
|
|
const isHomePage = path === "/";
|
2024-09-11 06:09:34 +02:00
|
|
|
path = path === "/" ? "/home" : path;
|
2024-09-10 08:13:20 +02:00
|
|
|
path = path.startsWith("/") ? path : "/" + path;
|
2024-09-11 06:09:34 +02:00
|
|
|
path = path.endsWith("/") ? path.substring(0, path.length - 1) : path;
|
2024-09-10 08:13:20 +02:00
|
|
|
|
|
|
|
|
const url = `${window.location.origin}${path}.json`;
|
2024-07-11 12:42:29 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(url);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
2024-09-04 11:28:12 +02:00
|
|
|
console.log("Données récupérées du chemin " + path, data);
|
2024-07-11 12:42:29 +02:00
|
|
|
return data;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(
|
2024-09-04 11:28:12 +02:00
|
|
|
"Une erreur s'est produite lors de la récupération des données pour l'URL :",
|
|
|
|
|
url,
|
2024-07-11 12:42:29 +02:00
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-11 07:32:34 +02:00
|
|
|
async function fetchRoute(path, method, data) {
|
|
|
|
|
const config = {
|
|
|
|
|
method: method,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
|
config.body = JSON.stringify(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(path, config);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
console.log("La route " + path + " a fonctionné.");
|
|
|
|
|
return data;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("La route " + path + " n'a pas fonctionné.", error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 17:50:40 +01:00
|
|
|
async function addComment(comment) {
|
|
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(comment),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2024-10-29 11:12:57 +01:00
|
|
|
const response = await fetch("/create-comment.json", headers);
|
2024-10-28 17:50:40 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const newFile = await response.json();
|
|
|
|
|
return newFile;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(
|
|
|
|
|
"Une erreur s'est produite lors de l'ajout du commentaire :",
|
|
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 14:39:52 +01:00
|
|
|
async function post(data, route) {
|
2025-01-23 15:31:15 +01:00
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2025-01-27 14:39:52 +01:00
|
|
|
const response = await fetch(route, headers);
|
2025-01-23 15:31:15 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
2025-01-27 14:39:52 +01:00
|
|
|
return data;
|
2025-01-23 15:31:15 +01:00
|
|
|
} catch (error) {
|
2025-01-27 14:39:52 +01:00
|
|
|
console.error("Une erreur s'est produite :", error);
|
2025-01-23 15:31:15 +01:00
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 15:19:50 +01:00
|
|
|
async function updateComment(comment) {
|
|
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(comment),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch("/update-comment.json", headers);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const newFile = await response.json();
|
|
|
|
|
return newFile;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(
|
|
|
|
|
"Une erreur s'est produite lors de la mise à jour du commentaire :",
|
|
|
|
|
comment,
|
|
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 16:32:13 +01:00
|
|
|
async function deleteComment(comment) {
|
|
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(comment),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch("/delete-comment.json", headers);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const newFile = await response.json();
|
|
|
|
|
return newFile;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(
|
|
|
|
|
"Une erreur s'est produite lors de la suppression du commentaire :",
|
|
|
|
|
comment,
|
|
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 16:51:31 +01:00
|
|
|
async function replyComment(comment) {
|
|
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify(comment),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch("/reply-comment.json", headers);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const newFile = await response.json();
|
|
|
|
|
return newFile;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(
|
|
|
|
|
"Une erreur s'est produite lors de l'ajout de la réponse' :",
|
|
|
|
|
commentaire,
|
|
|
|
|
error
|
|
|
|
|
);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 15:36:43 +01:00
|
|
|
async function readNotification(notificationId, projectId) {
|
2024-10-28 17:50:40 +01:00
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify({
|
2025-01-15 14:18:48 +01:00
|
|
|
projectId,
|
2025-01-15 15:36:43 +01:00
|
|
|
notificationId,
|
2024-10-28 17:50:40 +01:00
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
try {
|
2024-12-16 18:14:14 +01:00
|
|
|
const response = await fetch("/read-notification.json", headers);
|
2024-10-28 17:50:40 +01:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
2024-11-18 09:36:15 +01:00
|
|
|
const data = await response.json();
|
|
|
|
|
if (data.error) {
|
|
|
|
|
throw new Error(data);
|
|
|
|
|
} else {
|
2025-01-15 16:24:34 +01:00
|
|
|
if (!projectId.startsWith("/")) {
|
|
|
|
|
projectId = "/" + projectId;
|
|
|
|
|
}
|
|
|
|
|
userStore.readNotification(notificationId, projectId);
|
2024-11-18 09:36:15 +01:00
|
|
|
return data;
|
|
|
|
|
}
|
2024-10-28 17:50:40 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 09:20:38 +01:00
|
|
|
async function validateBrief(briefUri, dialogUri = null) {
|
2024-11-21 19:43:49 +01:00
|
|
|
const headers = {
|
|
|
|
|
method: "POST",
|
|
|
|
|
body: JSON.stringify({
|
2024-11-22 09:20:38 +01:00
|
|
|
briefUri: briefUri,
|
|
|
|
|
dialogUri,
|
2024-11-21 19:43:49 +01:00
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch("/validate-brief.json", headers);
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
if (data.error) {
|
|
|
|
|
throw new Error(data);
|
|
|
|
|
} else {
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2024-11-22 09:20:38 +01:00
|
|
|
console.log(error);
|
2024-11-21 19:43:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-18 13:45:40 +01:00
|
|
|
async function readAllNotifications() {
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch("/read-all-notifications.json");
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
console.log(response);
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
if (data.error) {
|
|
|
|
|
throw new Error(data);
|
|
|
|
|
} else {
|
2025-01-15 16:24:34 +01:00
|
|
|
console.log(data);
|
|
|
|
|
userStore.readAllNotifications();
|
2024-11-18 13:45:40 +01:00
|
|
|
console.log("All notifications read.");
|
2025-01-15 16:24:34 +01:00
|
|
|
return data;
|
2024-11-18 13:45:40 +01:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-28 17:50:40 +01:00
|
|
|
return {
|
|
|
|
|
fetchData,
|
|
|
|
|
fetchRoute,
|
|
|
|
|
addComment,
|
2025-01-07 15:19:50 +01:00
|
|
|
updateComment,
|
2024-10-30 16:32:13 +01:00
|
|
|
deleteComment,
|
2024-10-29 16:51:31 +01:00
|
|
|
replyComment,
|
2024-10-28 17:50:40 +01:00
|
|
|
readNotification,
|
2024-11-18 13:45:40 +01:00
|
|
|
readAllNotifications,
|
2024-11-21 19:43:49 +01:00
|
|
|
validateBrief,
|
2025-01-27 14:39:52 +01:00
|
|
|
post,
|
2024-10-28 17:50:40 +01:00
|
|
|
};
|
2024-07-10 18:43:46 +02:00
|
|
|
});
|