Skip to content
Snippets Groups Projects
Commit cff622e9 authored by Chif Gergő's avatar Chif Gergő
Browse files

Add hook to query news list, use it in news container

parent 0a6e502b
No related branches found
No related tags found
1 merge request!19fix type imports
import React from 'react'; import React from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';
import { BrowserRouter as Router } from 'react-router-dom'; import { BrowserRouter as Router } from 'react-router-dom';
import { ClientContextProvider } from './context'; import { ClientContextProvider } from './context';
import ThemeProvider from './context/ThemeProvider'; import ThemeProvider from './context/ThemeProvider';
...@@ -6,17 +7,21 @@ import { UserStateProvider } from './context/UserContext'; ...@@ -6,17 +7,21 @@ import { UserStateProvider } from './context/UserContext';
import Routes from './Routes'; import Routes from './Routes';
import { darkTheme } from './theme'; import { darkTheme } from './theme';
const queryClient = new QueryClient();
function App(): React.ReactElement { function App(): React.ReactElement {
return ( return (
<ClientContextProvider> <QueryClientProvider client={queryClient}>
<UserStateProvider> <ClientContextProvider>
<ThemeProvider theme={darkTheme}> <UserStateProvider>
<Router> <ThemeProvider theme={darkTheme}>
<Routes /> <Router>
</Router> <Routes />
</ThemeProvider> </Router>
</UserStateProvider> </ThemeProvider>
</ClientContextProvider> </UserStateProvider>
</ClientContextProvider>
</QueryClientProvider>
); );
} }
......
import { AxiosRequestConfig } from 'axios'; import { AxiosRequestConfig } from 'axios';
import { auth, AuthClient } from './auth'; import { auth, AuthClient } from './auth';
import { news, NewsClient } from './news';
const DEFAULT_BASE_URL = ''; const DEFAULT_BASE_URL = '';
...@@ -14,10 +15,12 @@ export function createClient( ...@@ -14,10 +15,12 @@ export function createClient(
}; };
const client = { const client = {
auth: auth(config), auth: auth(config),
news: news(config),
}; };
return client; return client;
} }
export type Client = { export type Client = {
auth: AuthClient; auth: AuthClient;
news: NewsClient;
}; };
...@@ -4,18 +4,19 @@ import { INews } from '../types/News'; ...@@ -4,18 +4,19 @@ import { INews } from '../types/News';
type NewsQuery = () => Promise<INews[]>; type NewsQuery = () => Promise<INews[]>;
export type NewsClient = { export type NewsClient = {
news: NewsQuery; newsList: NewsQuery;
}; };
const authToken = 'q98g7o6353wl7usv1a36xv6dieso6219'; // TODO: Remove when auth flow complete
const authToken = process.env.AUTH_TOKEN;
export function users(config: AxiosRequestConfig): NewsClient { export function news(config: AxiosRequestConfig): NewsClient {
const axios = Axios.create({ const axios = Axios.create({
...config, ...config,
baseURL: `${config.baseURL}/api/v1/news`, baseURL: `${config.baseURL}/api/v1/news`,
}); });
const news: NewsQuery = async (): Promise<INews[]> => { const newsList: NewsQuery = async (): Promise<INews[]> => {
const { data: response } = await axios.get<INews[]>('/', { const { data: response } = await axios.get<INews[]>('/', {
headers: { Authorization: `Bearer ${authToken}` }, headers: { Authorization: `Bearer ${authToken}` },
}); });
...@@ -23,6 +24,6 @@ export function users(config: AxiosRequestConfig): NewsClient { ...@@ -23,6 +24,6 @@ export function users(config: AxiosRequestConfig): NewsClient {
}; };
return { return {
news, newsList,
}; };
} }
import { Box, CircularProgress, Grid, Typography } from '@material-ui/core'; import { Box, CircularProgress, Grid, Typography } from '@material-ui/core';
import React from 'react'; import React from 'react';
import useGetNews from '../hooks/getNews';
import News from './News'; import News from './News';
interface Props {} interface Props {}
const NewsContainer: React.FC = () => { const NewsContainer: React.FC = () => {
const isLoading = false; const { isFetching, data } = useGetNews();
return isLoading ? ( return isFetching ? (
<CircularProgress /> <CircularProgress />
) : ( ) : (
<Box paddingY={2}> <Box paddingY={2}>
...@@ -24,21 +25,18 @@ const NewsContainer: React.FC = () => { ...@@ -24,21 +25,18 @@ const NewsContainer: React.FC = () => {
</Grid> </Grid>
<Grid item xs={12} sm={6}> <Grid item xs={12} sm={6}>
<Grid container direction="column"> <Grid container direction="column">
<Grid item xs={12}> {data?.map((item) => (
<Box p={2}> <Grid item xs={12}>
<News title="ASd" author="Anonymus" content="ASDADDD" createDate="2021.01.01" /> <Box p={2}>
</Box> <News
</Grid> title={item.title}
<Grid item xs={12}> author={item.publishedBy}
<Box p={2}> content={item.text}
<News title="ASd" author="Anonymus" content="ASDADDD" createDate="2021.01.01" /> createDate={item.publishedAt.toString()}
</Box> />
</Grid> </Box>
<Grid item xs={12}> </Grid>
<Box p={2}> ))}
<News title="ASd" author="Anonymus" content="ASDADDD" createDate="2021.01.01" />
</Box>
</Grid>
</Grid> </Grid>
</Grid> </Grid>
<Grid container item xs={12} sm={3} justify="flex-end"> <Grid container item xs={12} sm={3} justify="flex-end">
......
import { useQuery, UseQueryResult } from 'react-query';
import { INews } from '../types/News';
import useClientContext from './useClientContext';
const useGetNews = (): UseQueryResult<INews[], Error> => {
const { client } = useClientContext();
return useQuery<INews[], Error>('news', async () => client.news.newsList());
};
export default useGetNews;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment