From 02df6bb9eb87491dfae730dc94f94c6ca238012a Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Tue, 17 May 2022 22:07:12 +0300 Subject: [PATCH] Remove useFetchBackend --- src/api/backend.ts | 2 ++ src/hooks/useFetchBackend.ts | 21 --------------------- src/pages/admin/events/index.tsx | 6 +++--- src/pages/admin/feed/index.tsx | 6 +++--- src/pages/admin/jobads/index.tsx | 6 +++--- src/pages/in_english.tsx | 8 ++++---- src/pages/index.tsx | 8 ++++---- src/pages/kilta/toiminta.tsx | 8 ++++---- src/pages/yritysyhteistyo.tsx | 7 +++---- 9 files changed, 26 insertions(+), 46 deletions(-) delete mode 100644 src/hooks/useFetchBackend.ts diff --git a/src/api/backend.ts b/src/api/backend.ts index 96d94a2..4e988d4 100644 --- a/src/api/backend.ts +++ b/src/api/backend.ts @@ -122,3 +122,5 @@ export const deleteBackendAPI = async ({ const headers = getHeaders(authenticated); return callBackendAPI(path, urlParams, queryParams, "DELETE", headers, undefined); }; + +export const fetcher = (path: APIPath, options: { limit?: number, auth?: boolean }) => getBackendAPI({ path, queryParams: { limit: options.limit }, authenticated: options.auth }); diff --git a/src/hooks/useFetchBackend.ts b/src/hooks/useFetchBackend.ts deleted file mode 100644 index 3cc0d49..0000000 --- a/src/hooks/useFetchBackend.ts +++ /dev/null @@ -1,21 +0,0 @@ -import useSWR, { SWRResponse } from "swr"; -import { APIPath, getBackendAPI } from "@api/backend"; - -const useFetchBackend = ({ - apiPath: path, - fallbackData, - options, -}: { - apiPath: APIPath, - fallbackData?: DataType, - options?: { - limit?: number; - auth?: boolean; - } -}): SWRResponse => { - const fetcher = (limit: number, authenticated: boolean) => getBackendAPI({ path, queryParams: { limit }, authenticated }); - const result = useSWR([path, options?.limit, options?.auth], fetcher, { fallbackData }); - return result; -}; - -export default useFetchBackend; diff --git a/src/pages/admin/events/index.tsx b/src/pages/admin/events/index.tsx index 6cdddd0..632b886 100644 --- a/src/pages/admin/events/index.tsx +++ b/src/pages/admin/events/index.tsx @@ -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({ apiPath: APIPath.EVENTS, options: { auth: true } }); + const { data } = useSWR([APIPath.EVENTS, { auth: true }], fetcher); return (

Events

diff --git a/src/pages/admin/feed/index.tsx b/src/pages/admin/feed/index.tsx index 30092e5..a61e235 100644 --- a/src/pages/admin/feed/index.tsx +++ b/src/pages/admin/feed/index.tsx @@ -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({ apiPath: APIPath.FEED, options: { auth: true } }); + const { data } = useSWR([APIPath.FEED, { auth: true }], fetcher); return (

Feed

diff --git a/src/pages/admin/jobads/index.tsx b/src/pages/admin/jobads/index.tsx index 3de3931..67c9dfe 100644 --- a/src/pages/admin/jobads/index.tsx +++ b/src/pages/admin/jobads/index.tsx @@ -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({ apiPath: APIPath.JOBADS, options: { auth: true } }); + const { data } = useSWR([APIPath.JOBADS, { auth: true }], fetcher); return (

Job advertisements

diff --git a/src/pages/in_english.tsx b/src/pages/in_english.tsx index db62e76..b60a8fc 100644 --- a/src/pages/in_english.tsx +++ b/src/pages/in_english.tsx @@ -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 = ({ initialEvents, initialFeed }) => { - const eventResult = useFetchBackend({ apiPath: APIPath.EVENTS, fallbackData: initialEvents, options: eventOptions }); - const feedResult = useFetchBackend({ apiPath: APIPath.FEED, fallbackData: initialFeed, options: feedOptions }); + const eventResult = useSWR([APIPath.EVENTS, eventOptions], fetcher, { fallbackData: initialEvents }); + const feedResult = useSWR([APIPath.FEED, feedOptions], fetcher, { fallbackData: initialFeed }); return ( <> diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 309ee90..3846a7d 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -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 = ({ initialEvents, initialFeed }) => { - const eventResult = useFetchBackend({ apiPath: APIPath.EVENTS, fallbackData: initialEvents, options: eventOptions }); - const feedResult = useFetchBackend({ apiPath: APIPath.FEED, fallbackData: initialFeed, options: feedOptions }); + const eventResult = useSWR([APIPath.EVENTS, eventOptions], fetcher, { fallbackData: initialEvents }); + const feedResult = useSWR([APIPath.FEED, feedOptions], fetcher, { fallbackData: initialFeed }); return ( <> diff --git a/src/pages/kilta/toiminta.tsx b/src/pages/kilta/toiminta.tsx index 3a35b82..08cdd23 100644 --- a/src/pages/kilta/toiminta.tsx +++ b/src/pages/kilta/toiminta.tsx @@ -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 = ({ initialEvents, initialFeed }) => { - const eventResult = useFetchBackend({ apiPath: APIPath.EVENTS, fallbackData: initialEvents }); - const feedResult = useFetchBackend({ apiPath: APIPath.FEED, fallbackData: initialFeed }); + const eventResult = useSWR([APIPath.EVENTS, {}], fetcher, { fallbackData: initialEvents }); + const feedResult = useSWR([APIPath.FEED, {}], fetcher, { fallbackData: initialFeed }); return ( <> diff --git a/src/pages/yritysyhteistyo.tsx b/src/pages/yritysyhteistyo.tsx index b81c759..7a057a1 100644 --- a/src/pages/yritysyhteistyo.tsx +++ b/src/pages/yritysyhteistyo.tsx @@ -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 = ({ initialJobAds }) => { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { data, error } = useFetchBackend({ apiPath: APIPath.JOBADS, fallbackData: initialJobAds }); + const { data } = useSWR([APIPath.JOBADS, {}], fetcher, { fallbackData: initialJobAds }); return ( <>