74 lines
2.7 KiB
JavaScript
74 lines
2.7 KiB
JavaScript
|
|
/**
|
|
* Fetch and update articles data with likes and dislikes.
|
|
* @param {Ref<Array>} articles - Reactive array of articles.
|
|
*/
|
|
export const fetchArticlesData = async (articles) => {
|
|
try {
|
|
const response = await $fetch(`/api/articles`, { method: 'GET' });
|
|
|
|
if (response.success && response.data) {
|
|
articles.value = articles.value.map((article) => {
|
|
const updatedArticle = response.data.find((data) => data.id === article.id);
|
|
return updatedArticle
|
|
? { ...article, likes: updatedArticle.likes, dislikes: updatedArticle.dislikes }
|
|
: article;
|
|
});
|
|
} else {
|
|
console.error('Failed to fetch articles:', response.message || 'Unknown error');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching articles data:', error);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Fetch and attach comments to their respective articles.
|
|
* @param {Ref<Array>} articles - Reactive array of articles.
|
|
*/
|
|
export const fetchCommentsData = async (articles) => {
|
|
try {
|
|
const response = await $fetch(`/api/comments`, { method: 'GET' });
|
|
|
|
if (response.success && response.data) {
|
|
articles.value = articles.value.map((article) => {
|
|
const relatedComments = response.data.filter((data) => data.articleId === article.id);
|
|
return { ...article, comments: relatedComments };
|
|
});
|
|
} else {
|
|
console.error('Failed to fetch comments:', response.message || 'Unknown error');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching comments data:', error);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Fetch and format signatures data.
|
|
* @param {Ref<Array>} signatures - Reactive array of signatures.
|
|
*/
|
|
export const fetchSignatures = async (signatures) => {
|
|
try {
|
|
const response = await $fetch(`/api/charte`, { method: 'GET' });
|
|
|
|
if (response.success && Array.isArray(response.data)) {
|
|
signatures.value = response.data.map((signature) => ({
|
|
...signature,
|
|
createdAt: new Intl.DateTimeFormat('en-US', {
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
}).format(new Date(signature.createdAt)),
|
|
}));
|
|
} else if (response.success) {
|
|
console.error('Unexpected data format:', response.data);
|
|
} else {
|
|
console.error('Failed to fetch signatures:', response.message || 'Unknown error');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching signatures data:', error);
|
|
}
|
|
};
|