Files
mtdb_movie/resources/client/admin/news/requests/use-news-article.ts
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

33 lines
1.1 KiB
TypeScript
Executable File

import {useQuery} from '@tanstack/react-query';
import {apiClient} from '@common/http/query-client';
import {BackendResponse} from '@common/http/backend-response/backend-response';
import {NewsArticle} from '@app/titles/models/news-article';
import {useParams} from 'react-router-dom';
import {getBootstrapData} from '@common/core/bootstrap-data/use-backend-bootstrap-data';
export interface GetNewsArticleResponse extends BackendResponse {
article: NewsArticle;
related: NewsArticle[];
}
export function useNewsArticle(loader: 'newsArticlePage') {
const {articleId} = useParams();
return useQuery<GetNewsArticleResponse>({
queryKey: ['news-articles', `${articleId}`],
queryFn: () => fetchNewsArticle(articleId!),
initialData: () => {
const data = getBootstrapData().loaders?.[loader];
if (data?.article?.id == articleId) {
return data;
}
return undefined;
},
});
}
function fetchNewsArticle(articleId: string) {
return apiClient
.get<GetNewsArticleResponse>(`news/${articleId}`)
.then(response => response.data);
}