Remove useFetchBackend

This commit is contained in:
Aarni Halinen
2022-05-17 22:07:12 +03:00
parent b55a04f0f3
commit 02df6bb9eb
9 changed files with 26 additions and 46 deletions
+2
View File
@@ -122,3 +122,5 @@ export const deleteBackendAPI = async <ResponseType>({
const headers = getHeaders(authenticated);
return callBackendAPI<undefined, ResponseType>(path, urlParams, queryParams, "DELETE", headers, undefined);
};
export const fetcher = <ResponseType>(path: APIPath, options: { limit?: number, auth?: boolean }) => getBackendAPI<ResponseType>({ path, queryParams: { limit: options.limit }, authenticated: options.auth });
-21
View File
@@ -1,21 +0,0 @@
import useSWR, { SWRResponse } from "swr";
import { APIPath, getBackendAPI } from "@api/backend";
const useFetchBackend = <DataType>({
apiPath: path,
fallbackData,
options,
}: {
apiPath: APIPath,
fallbackData?: DataType,
options?: {
limit?: number;
auth?: boolean;
}
}): SWRResponse<DataType> => {
const fetcher = (limit: number, authenticated: boolean) => getBackendAPI<DataType>({ path, queryParams: { limit }, authenticated });
const result = useSWR<DataType>([path, options?.limit, options?.auth], fetcher, { fallbackData });
return result;
};
export default useFetchBackend;
+3 -3
View File
@@ -1,5 +1,6 @@
import React from "react";
import { NextPage } from "next";
import useSWR from "swr";
import { formatRelative } from "date-fns";
import { toast } from "react-toastify";
import styled from "styled-components";
@@ -8,8 +9,7 @@ import { Button, Link } from "@components/index";
import AddLink from "@components/AddLink";
import Event from "@models/Event";
import EventApi from "@api/eventApi";
import useFetchBackend from "@hooks/useFetchBackend";
import { APIPath } from "@api/backend";
import { fetcher, APIPath } from "@api/backend";
const URL = "/admin/events";
@@ -66,7 +66,7 @@ const renderData = (events: Event[]) => {
};
const AdminEventPage: NextPage = () => {
const { data } = useFetchBackend<Event[]>({ apiPath: APIPath.EVENTS, options: { auth: true } });
const { data } = useSWR<Event[]>([APIPath.EVENTS, { auth: true }], fetcher);
return (
<AdminListCommon>
<h1>Events</h1>
+3 -3
View File
@@ -1,5 +1,6 @@
import React from "react";
import { NextPage } from "next";
import useSWR from "swr";
import { formatRelative } from "date-fns";
import { toast } from "react-toastify";
import styled from "styled-components";
@@ -8,8 +9,7 @@ import { Button, Link } from "@components/index";
import AddLink from "@components/AddLink";
import Post from "@models/Feed";
import PostApi from "@api/feedApi";
import useFetchBackend from "@hooks/useFetchBackend";
import { APIPath } from "@api/backend";
import { fetcher, APIPath } from "@api/backend";
const URL = "/admin/feed";
@@ -66,7 +66,7 @@ const renderData = (feed: Post[]) => {
};
const AdminFeedPage: NextPage = () => {
const { data } = useFetchBackend<Post[]>({ apiPath: APIPath.FEED, options: { auth: true } });
const { data } = useSWR<Post[]>([APIPath.FEED, { auth: true }], fetcher);
return (
<AdminListCommon>
<h1>Feed</h1>
+3 -3
View File
@@ -1,5 +1,6 @@
import React from "react";
import { NextPage } from "next";
import useSWR from "swr";
import { formatRelative } from "date-fns";
import { toast } from "react-toastify";
import styled from "styled-components";
@@ -7,9 +8,8 @@ import AdminListCommon from "@views/admin/AdminListCommon";
import { Button, Link } from "@components/index";
import AddLink from "@components/AddLink";
import JobAd from "@models/JobAd";
import useFetchBackend from "@hooks/useFetchBackend";
import JobAdApi from "@api/jobAdApi";
import { APIPath } from "@api/backend";
import { fetcher, APIPath } from "@api/backend";
const URL = "/admin/jobads";
@@ -70,7 +70,7 @@ const renderData = (jobAds: JobAd[]) => {
};
const AdminJobAdPage: NextPage = () => {
const { data } = useFetchBackend<JobAd[]>({ apiPath: APIPath.JOBADS, options: { auth: true } });
const { data } = useSWR<JobAd[]>([APIPath.JOBADS, { auth: true }], fetcher);
return (
<AdminListCommon>
<h1>Job advertisements</h1>
+4 -4
View File
@@ -1,14 +1,14 @@
import React from "react";
import { NextPage, GetStaticProps } from "next";
import Head from "next/head";
import useSWR from "swr";
import Event from "@models/Event";
import EventApi from "@api/eventApi";
import useFetchBackend from "@hooks/useFetchBackend";
import Post from "@models/Feed";
import FeedApi from "@api/feedApi";
import InEnglishPageView from "@views/InEnglishPage/InEnglishPageView";
import PageWrapper from "@views/common/PageWrapper";
import { APIPath } from "@api/backend";
import { fetcher, APIPath } from "@api/backend";
const eventOptions = {
limit: 4,
@@ -24,8 +24,8 @@ interface InitialProps {
}
const InEnglishPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => {
const eventResult = useFetchBackend<Event[]>({ apiPath: APIPath.EVENTS, fallbackData: initialEvents, options: eventOptions });
const feedResult = useFetchBackend<Post[]>({ apiPath: APIPath.FEED, fallbackData: initialFeed, options: feedOptions });
const eventResult = useSWR<Event[]>([APIPath.EVENTS, eventOptions], fetcher, { fallbackData: initialEvents });
const feedResult = useSWR<Post[]>([APIPath.FEED, feedOptions], fetcher, { fallbackData: initialFeed });
return (
<>
+4 -4
View File
@@ -1,14 +1,14 @@
import React from "react";
import { NextPage, GetStaticProps } from "next";
import Head from "next/head";
import useSWR from "swr";
import Event from "@models/Event";
import EventApi from "@api/eventApi";
import useFetchBackend from "@hooks/useFetchBackend";
import Post from "@models/Feed";
import FeedApi from "@api/feedApi";
import FrontPageView from "@views/FrontPage/FrontPageView";
import PageWrapper from "@views/common/PageWrapper";
import { APIPath } from "@api/backend";
import { fetcher, APIPath } from "@api/backend";
const eventOptions = {
limit: 4,
@@ -24,8 +24,8 @@ interface InitialProps {
}
const FrontPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => {
const eventResult = useFetchBackend<Event[]>({ apiPath: APIPath.EVENTS, fallbackData: initialEvents, options: eventOptions });
const feedResult = useFetchBackend<Post[]>({ apiPath: APIPath.FEED, fallbackData: initialFeed, options: feedOptions });
const eventResult = useSWR<Event[]>([APIPath.EVENTS, eventOptions], fetcher, { fallbackData: initialEvents });
const feedResult = useSWR<Post[]>([APIPath.FEED, feedOptions], fetcher, { fallbackData: initialFeed });
return (
<>
+4 -4
View File
@@ -1,14 +1,14 @@
import React from "react";
import { NextPage, GetStaticProps } from "next";
import Head from "next/head";
import useSWR from "swr";
import Event from "@models/Event";
import EventApi from "@api/eventApi";
import useFetchBackend from "@hooks/useFetchBackend";
import Post from "@models/Feed";
import FeedApi from "@api/feedApi";
import ActualPageView from "@views/ActualPage/ActualPageView";
import PageWrapper from "@views/common/PageWrapper";
import { APIPath } from "@api/backend";
import { fetcher, APIPath } from "@api/backend";
interface InitialProps {
initialEvents: Event[];
@@ -16,8 +16,8 @@ interface InitialProps {
}
const ActualPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) => {
const eventResult = useFetchBackend<Event[]>({ apiPath: APIPath.EVENTS, fallbackData: initialEvents });
const feedResult = useFetchBackend<Post[]>({ apiPath: APIPath.FEED, fallbackData: initialFeed });
const eventResult = useSWR<Event[]>([APIPath.EVENTS, {}], fetcher, { fallbackData: initialEvents });
const feedResult = useSWR<Post[]>([APIPath.FEED, {}], fetcher, { fallbackData: initialFeed });
return (
<>
+3 -4
View File
@@ -1,20 +1,19 @@
import React from "react";
import { NextPage, GetStaticProps } from "next";
import Head from "next/head";
import useSWR from "swr";
import JobAd from "@models/JobAd";
import JobAdApi from "@api/jobAdApi";
import useFetchBackend from "@hooks/useFetchBackend";
import CorporatePageView from "@views/CorporatePage/CorporatePageView";
import PageWrapper from "@views/common/PageWrapper";
import { APIPath } from "@api/backend";
import { APIPath, fetcher } from "@api/backend";
interface InitialProps {
initialJobAds: JobAd[];
}
const CorporatePage: NextPage<InitialProps> = ({ initialJobAds }) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { data, error } = useFetchBackend<JobAd[]>({ apiPath: APIPath.JOBADS, fallbackData: initialJobAds });
const { data } = useSWR<JobAd[]>([APIPath.JOBADS, {}], fetcher, { fallbackData: initialJobAds });
return (
<>
<Head>