diff --git a/src/blog/providers/Article.tsx b/src/blog/providers/Article.tsx index 1f7dce5..30f7e17 100644 --- a/src/blog/providers/Article.tsx +++ b/src/blog/providers/Article.tsx @@ -12,6 +12,19 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child const [error, setError] = useState(null); const { token, currentUser } = useAuth(); + const upsertArticleInList = (updated: ArticleModel) => { + setArticles(prev => { + const exists = prev.some(a => a._id === updated._id); + if (exists) { + // UPDATE → replace item + return prev.map(a => (a._id === updated._id ? updated : a)); + } else { + // CREATE → append to top + return [updated, ...prev]; + } + }); + }; + /** 🔹 Author IDs must be strings for API, so we normalize here */ const normalizeArticleForApi = (article: Partial) => { // Extract existing authors as a list of IDs (string[]) @@ -60,6 +73,7 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child setError(null); const res = await api.put(`/articles/${articleData._id}`, normalizedArticleData); + upsertArticleInList(res.data); return res.data; } catch (err: any) { console.error('Article update failed:', err); @@ -82,6 +96,7 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child setError(null); const res = await api.post(`/articles`, normalizedArticleData); + upsertArticleInList(res.data); return res.data; } catch (err: any) { console.error('Article create failed:', err);