making sure currentUser is in the list of authors for article
This commit is contained in:
@@ -10,15 +10,25 @@ export const ArticleProvider: React.FC<{ children: React.ReactNode }> = ({ child
|
|||||||
const [articles, setArticles] = useState<ArticleModel[]>([]);
|
const [articles, setArticles] = useState<ArticleModel[]>([]);
|
||||||
const [loading, setLoading] = useState<boolean>(true);
|
const [loading, setLoading] = useState<boolean>(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const { token } = useAuth();
|
const { token, currentUser } = useAuth();
|
||||||
|
|
||||||
/** 🔹 Author IDs must be strings for API, so we normalize here */
|
/** 🔹 Author IDs must be strings for API, so we normalize here */
|
||||||
const normalizeArticleForApi = (article: Partial<ArticleModel>) => ({
|
const normalizeArticleForApi = (article: Partial<ArticleModel>) => {
|
||||||
...article,
|
// Extract existing authors as a list of IDs (string[])
|
||||||
authors: (article.authors ?? []).map(a =>
|
const existingIds = (article.authors ?? []).map(a =>
|
||||||
a._id
|
typeof a === "string" ? a : a._id
|
||||||
),
|
);
|
||||||
});
|
|
||||||
|
// Inject currentUser if missing
|
||||||
|
const allAuthorIds = currentUser?._id
|
||||||
|
? Array.from(new Set([...existingIds, currentUser._id])) // dedupe
|
||||||
|
: existingIds;
|
||||||
|
|
||||||
|
return {
|
||||||
|
...article,
|
||||||
|
authors: allAuthorIds,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
/** 🔹 Fetch articles (JWT automatically attached by api.ts interceptor) */
|
/** 🔹 Fetch articles (JWT automatically attached by api.ts interceptor) */
|
||||||
const fetchArticles = async () => {
|
const fetchArticles = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user