feature/improved-article-content-loading #179
@ -1,6 +1,5 @@
|
||||
import type { ArticleStore } from "../domain/articleStore";
|
||||
import { useCallback, useEffect } from "react";
|
||||
import { Article } from "article/domain/articleEntity";
|
||||
import { GetArticleUseCase } from "article/useCases/getArticleUseCase";
|
||||
import { FetchArticleUseCase } from "article/useCases/fetchArticleUseCase";
|
||||
import { useParams } from "react-router-dom";
|
||||
@ -12,15 +11,17 @@ function useArticleViewModel(
|
||||
) {
|
||||
const { id } = useParams();
|
||||
|
||||
const getArticle = useCallback(
|
||||
() => {
|
||||
getArticleUseCase.call(id ?? '').catch((_) => fetchArticleUseCase.call(id ?? ''));
|
||||
console.log(id);
|
||||
},
|
||||
[id]
|
||||
);
|
||||
// const getArticle = useCallback(
|
||||
// () => {
|
||||
// getArticleUseCase.call(id ?? '').catch((_) => fetchArticleUseCase.call(id ?? ''));
|
||||
// console.log(id);
|
||||
// },
|
||||
// [id]
|
||||
// );
|
||||
|
||||
useEffect(getArticle, []);
|
||||
useEffect(() => {
|
||||
getArticleUseCase.call(id ?? '').catch((_) => fetchArticleUseCase.call(id ?? ''));
|
||||
}, []);
|
||||
|
||||
return {
|
||||
article: store.currentArticle,
|
||||
|
@ -17,7 +17,7 @@ const articleReducer = (
|
||||
): ArticleStoreState => {
|
||||
switch (action.type) {
|
||||
case actionTypes.SET_ARTICLE:
|
||||
return { ...state, articles: action.updatedList, currentArticle: action.article };
|
||||
return { ...state, articles: action.updatedList, currentArticle: action.article, hasError: typeof action.article === undefined };
|
||||
case actionTypes.GET_ARTICLE:
|
||||
return { ...state, isLoading: true };
|
||||
case actionTypes.GET_ARTICLE_SUCCESS:
|
||||
|
@ -26,17 +26,4 @@ class FetchArticleUseCase {
|
||||
/* -------------------------------------------------------------------------- */
|
||||
}
|
||||
|
||||
|
||||
const fetchArticleUseCase = async (
|
||||
fetchArticleCallback: (id: string) => Promise<Article | null>,
|
||||
setArticle: ArticleStore["setArticle"],
|
||||
id: string,
|
||||
): Promise<Article | null> => {
|
||||
const article = await fetchArticleCallback(id);
|
||||
if (article) {
|
||||
await setArticle(article);
|
||||
}
|
||||
return article;
|
||||
};
|
||||
export { FetchArticleUseCase };
|
||||
export { fetchArticleUseCase };
|
||||
|
@ -22,18 +22,4 @@ class GetArticleUseCase {
|
||||
/* -------------------------------------------------------------------------- */
|
||||
}
|
||||
|
||||
|
||||
const getArticleUseCase = async (
|
||||
getArticle: ArticleStore["getArticle"],
|
||||
setArticle: ArticleStore["setArticle"],
|
||||
id: Article["id"]
|
||||
): Promise<Article | null> => {
|
||||
const article = await getArticle(id);
|
||||
if (article) {
|
||||
await setArticle(article);
|
||||
}
|
||||
return article;
|
||||
};
|
||||
|
||||
export { GetArticleUseCase };
|
||||
export { getArticleUseCase };
|
||||
|
Loading…
x
Reference in New Issue
Block a user