dynamize projects
This commit is contained in:
parent
3f9c8bbebf
commit
edd9e66efb
10 changed files with 417 additions and 291 deletions
|
|
@ -1,11 +1,40 @@
|
|||
import { defineStore } from "pinia";
|
||||
|
||||
export const useApiStore = defineStore("counter", () => {
|
||||
async function fetchPageData() {
|
||||
const isHomePage = window.location.pathname === "/";
|
||||
/**
|
||||
* Asynchronously fetches JSON data corresponding to a given path.
|
||||
*
|
||||
* @param {string} [path=window.location.pathname] - The path for which to fetch data.
|
||||
* - If no path is provided, the function will use the current page's path.
|
||||
* - If the path is "/", it is assumed to be the homepage, and "home.json" is fetched.
|
||||
* - For other paths, the function will append ".json" to the path and fetch that URL.
|
||||
*
|
||||
* @returns {Promise<Object>} A promise that resolves to the JSON data if the fetch is successful.
|
||||
*
|
||||
* @throws {Error} Will throw an error if the HTTP request fails (e.g., non-200 status code).
|
||||
* - The error will include the HTTP status code and a message indicating the fetch failed.
|
||||
*
|
||||
* @example
|
||||
* // Fetch data for the current page
|
||||
* fetchPageData().then(data => {
|
||||
* console.log(data);
|
||||
* }).catch(error => {
|
||||
* console.error('Error fetching data:', error);
|
||||
* });
|
||||
*
|
||||
* @example
|
||||
* // Fetch data for a specific path
|
||||
* fetchPageData('/about').then(data => {
|
||||
* console.log(data);
|
||||
* }).catch(error => {
|
||||
* console.error('Error fetching data:', error);
|
||||
* });
|
||||
*/
|
||||
async function fetchPageData(path = window.location.pathname) {
|
||||
const isHomePage = path === "/";
|
||||
const url = isHomePage
|
||||
? `${window.location.href}home.json`
|
||||
: `${window.location.href}.json`;
|
||||
? `${window.location.origin}/home.json`
|
||||
: `${window.location.origin}/${path}.json`;
|
||||
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
|
|
@ -13,10 +42,12 @@ export const useApiStore = defineStore("counter", () => {
|
|||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
console.log("Données récupérées du chemin " + path, data);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(
|
||||
"Une erreur s'est produite lors de la récupération des données :",
|
||||
"Une erreur s'est produite lors de la récupération des données pour l'URL :",
|
||||
url,
|
||||
error
|
||||
);
|
||||
throw error;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue