Commit c77db6c5 authored by Daniel Puchala's avatar Daniel Puchala
Browse files

feat: made swr fetcher global

parent 7008f93a
......@@ -5,7 +5,7 @@ import fetcher from '../models/fetcher';
import { MovieCardMode } from '../models/types';
export const LandingPage = () => {
const { data, error } = useSWR('movies', fetcher);
const { data, error } = useSWR('movies');
if (error) return <div>failed to load</div>;
if (!data) return <div>loading...</div>;
......
......@@ -6,10 +6,8 @@ import { useForm } from 'react-hook-form';
import { useParams } from 'react-router-dom';
import instance from '../models/axios';
import useSWR from 'swr';
import fetcher from '../models/fetcher';
import { getToken, getUser } from '../services/auth';
import formatDuration from '../services/formatDuration';
import review from './Review';
import Review from './Review';
function getAverageRating(review: any) {
......@@ -37,13 +35,14 @@ export const Movie = () => {
};
console.log(reviewData)
await instance.post('reviews', reviewData, { headers });
await mutateReviews();
alert('Review created');
};
const { id } = useParams();
const { data: movie, error: movieError } = useSWR(`movies/${id}`, fetcher)
const { data: reviews, error: reviewError, mutate: mutateReviews } = useSWR(`reviews/search/${id}`, fetcher)
const { data: recommended, error: recommendedError } = useSWR(`movies/${id}/recommended`, fetcher)
const { data: movie, error: movieError } = useSWR(`movies/${id}`)
const { data: reviews, error: reviewError, mutate: mutateReviews } = useSWR(`reviews/search/${id}`)
const { data: recommended, error: recommendedError } = useSWR(`movies/${id}/recommended`)
if (movieError) return <div>failed to load</div>;
if (!movie) return <div>loading...</div>;
......
......@@ -3,16 +3,20 @@ import LandingPage from './LandingPage';
import Movie from './Movie';
import PageNotFound from './PageNotFound';
import { Login } from './Login';
import { SWRConfig } from 'swr';
import fetcher from '../models/fetcher';
export const Pages = () => (
<BrowserRouter>
<Routes>
<Route path="pa165" element={<LandingPage/>}/>
<Route path="pa165/movie/:id" element={<Movie/>}/>
<Route path="pa165/login" element={<Login/>}/>
<Route path="*" element={<PageNotFound />}/>
</Routes>
</BrowserRouter>
<SWRConfig value={{ fetcher }}>
<BrowserRouter>
<Routes>
<Route path="pa165" element={<LandingPage/>}/>
<Route path="pa165/movie/:id" element={<Movie/>}/>
<Route path="pa165/login" element={<Login/>}/>
<Route path="*" element={<PageNotFound />}/>
</Routes>
</BrowserRouter>
</SWRConfig>
);
export default Pages;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment