diff --git a/src/pages/admin/events/index.tsx b/src/pages/admin/events/index.tsx index 922d0c8..d20cb54 100644 --- a/src/pages/admin/events/index.tsx +++ b/src/pages/admin/events/index.tsx @@ -9,7 +9,7 @@ import { Button, Link } from "@components/index"; import AddLink from "@components/AddLink"; import Event from "@models/Event"; import EventApi from "@api/eventApi"; -import { fetcher, APIPath } from "@api/backend"; +import { fetcher, APIPath, API } from "@api/backend"; const URL = "/admin/events"; @@ -33,8 +33,19 @@ const confirmDelete = async (event: Event) => { } }; -const renderData = (events: Event[]) => { - if (!events || events.length === 0) { +const Renderer: React.FC = () => { + const api: API = { path: APIPath.EVENTS, authenticated: true }; + const { data: events, error } = useSWR(api, fetcher); + + if (error) { + console.error(error); + return ( +
+ Failed loading events +
+ ); + } + if (!events?.length) { return
No events.
; } @@ -65,15 +76,12 @@ const renderData = (events: Event[]) => { ); }; -const AdminEventPage: NextPage = () => { - const { data: events } = useSWR([APIPath.EVENTS, { auth: true }], fetcher); - return ( - -

Events

- - {renderData(events)} -
- ); -}; +const AdminEventPage: NextPage = () => ( + +

Events

+ + +
+); export default AdminEventPage; diff --git a/src/pages/admin/feed/index.tsx b/src/pages/admin/feed/index.tsx index 09d9d25..07bfc8d 100644 --- a/src/pages/admin/feed/index.tsx +++ b/src/pages/admin/feed/index.tsx @@ -9,7 +9,7 @@ import { Button, Link } from "@components/index"; import AddLink from "@components/AddLink"; import Post from "@models/Feed"; import PostApi from "@api/feedApi"; -import { fetcher, APIPath } from "@api/backend"; +import { fetcher, APIPath, API } from "@api/backend"; const URL = "/admin/feed"; @@ -33,9 +33,22 @@ const confirmDelete = async (post: Post) => { } }; -const renderData = (feed: Post[]) => { - if (!feed || feed.length === 0) { - return
No posts.
; +const Renderer: React.FC = () => { + const api: API = { path: APIPath.FEED, authenticated: true }; + const { data: feed, error } = useSWR(api, fetcher); + + if (error) { + console.error(error); + return ( +
+ Failed loading feed +
+ ); + } + if (!feed?.length) { + return ( +
No posts.
+ ); } return ( @@ -65,15 +78,12 @@ const renderData = (feed: Post[]) => { ); }; -const AdminFeedPage: NextPage = () => { - const { data: feed } = useSWR([APIPath.FEED, { auth: true }], fetcher); - return ( - -

Feed

- - {renderData(feed)} -
- ); -}; +const AdminFeedPage: NextPage = () => ( + +

Feed

+ + +
+); export default AdminFeedPage; diff --git a/src/pages/admin/jobads/index.tsx b/src/pages/admin/jobads/index.tsx index e5cb9f7..6672c4c 100644 --- a/src/pages/admin/jobads/index.tsx +++ b/src/pages/admin/jobads/index.tsx @@ -9,7 +9,7 @@ import { Button, Link } from "@components/index"; import AddLink from "@components/AddLink"; import JobAd from "@models/JobAd"; import JobAdApi from "@api/jobAdApi"; -import { fetcher, APIPath } from "@api/backend"; +import { fetcher, APIPath, API } from "@api/backend"; const URL = "/admin/jobads"; @@ -33,8 +33,18 @@ const confirmDelete = async (jobad: JobAd) => { } }; -const renderData = (jobAds: JobAd[]) => { - if (!jobAds || jobAds.length === 0) { +const Renderer: React.FC = () => { + const api: API = { path: APIPath.JOBADS, authenticated: true }; + const { data: jobAds, error } = useSWR(api, fetcher); + if (error) { + console.error(error); + return ( +
+ Failed loading jobads +
+ ); + } + if (!jobAds?.length) { return
No advertisements.
; } @@ -69,15 +79,12 @@ const renderData = (jobAds: JobAd[]) => { ); }; -const AdminJobAdPage: NextPage = () => { - const { data: jobAds } = useSWR([APIPath.JOBADS, { auth: true }], fetcher); - return ( - -

Job advertisements

- - {renderData(jobAds)} -
- ); -}; +const AdminJobAdPage: NextPage = () => ( + +

Job advertisements

+ + +
+); export default AdminJobAdPage; diff --git a/src/pages/in_english.tsx b/src/pages/in_english.tsx index 121a9af..393f8fd 100644 --- a/src/pages/in_english.tsx +++ b/src/pages/in_english.tsx @@ -3,19 +3,23 @@ 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 Post from "@models/Feed"; -import FeedApi from "@api/feedApi"; import InEnglishPageView from "@views/InEnglishPage/InEnglishPageView"; import PageWrapper from "@views/common/PageWrapper"; -import { fetcher, APIPath } from "@api/backend"; +import { fetcher, APIPath, API } from "@api/backend"; -const eventOptions = { - limit: 4, +const eventApi: API = { + path: APIPath.EVENTS, + queryParams: { + limit: 4, + }, }; -const feedOptions = { - limit: 4, +const feedApi: API = { + path: APIPath.FEED, + queryParams: { + limit: 4, + }, }; interface InitialProps { @@ -24,8 +28,8 @@ interface InitialProps { } const InEnglishPage: NextPage = ({ initialEvents, initialFeed }) => { - const { data: events } = useSWR([APIPath.EVENTS, eventOptions], fetcher, { fallbackData: initialEvents }); - const { data: feed } = useSWR([APIPath.FEED, feedOptions], fetcher, { fallbackData: initialFeed }); + const { data: events } = useSWR(eventApi, fetcher, { fallbackData: initialEvents }); + const { data: feed } = useSWR(feedApi, fetcher, { fallbackData: initialFeed }); return ( <> @@ -40,8 +44,8 @@ const InEnglishPage: NextPage = ({ initialEvents, initialFeed }) = }; export const getStaticProps: GetStaticProps = async () => { - const initialEvents = await EventApi.getEvents(eventOptions); - const initialFeed = await FeedApi.getFeed(feedOptions); + const initialEvents = await fetcher(eventApi); + const initialFeed = await fetcher(feedApi); return { props: { initialEvents, diff --git a/src/pages/kilta/toiminta.tsx b/src/pages/kilta/toiminta.tsx index d773e26..b4d4577 100644 --- a/src/pages/kilta/toiminta.tsx +++ b/src/pages/kilta/toiminta.tsx @@ -8,16 +8,24 @@ import Post from "@models/Feed"; import FeedApi from "@api/feedApi"; import ActualPageView from "@views/ActualPage/ActualPageView"; import PageWrapper from "@views/common/PageWrapper"; -import { fetcher, APIPath } from "@api/backend"; +import { fetcher, APIPath, API } from "@api/backend"; interface InitialProps { initialEvents: Event[]; initialFeed: Post[]; } +const eventApi: API = { + path: APIPath.EVENTS, +}; + +const feedApi: API = { + path: APIPath.FEED, +}; + const ActualPage: NextPage = ({ initialEvents, initialFeed }) => { - const { data: events } = useSWR([APIPath.EVENTS, {}], fetcher, { fallbackData: initialEvents }); - const { data: feed } = useSWR([APIPath.FEED, {}], fetcher, { fallbackData: initialFeed }); + const { data: events } = useSWR(eventApi, fetcher, { fallbackData: initialEvents }); + const { data: feed } = useSWR(feedApi, fetcher, { fallbackData: initialFeed }); return ( <> diff --git a/src/pages/yritysyhteistyo.tsx b/src/pages/yritysyhteistyo.tsx index 5cb31a7..6ae3c24 100644 --- a/src/pages/yritysyhteistyo.tsx +++ b/src/pages/yritysyhteistyo.tsx @@ -3,17 +3,20 @@ 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 CorporatePageView from "@views/CorporatePage/CorporatePageView"; import PageWrapper from "@views/common/PageWrapper"; -import { APIPath, fetcher } from "@api/backend"; +import { API, APIPath, fetcher } from "@api/backend"; interface InitialProps { initialJobAds: JobAd[]; } +const jobAdApi: API = { + path: APIPath.JOBADS, +}; + const CorporatePage: NextPage = ({ initialJobAds }) => { - const { data: jobAds } = useSWR([APIPath.JOBADS, {}], fetcher, { fallbackData: initialJobAds }); + const { data: jobAds } = useSWR(jobAdApi, fetcher, { fallbackData: initialJobAds }); return ( <> @@ -27,7 +30,7 @@ const CorporatePage: NextPage = ({ initialJobAds }) => { }; export const getStaticProps: GetStaticProps = async () => { - const initialJobAds = await JobAdApi.getJobAds(); + const initialJobAds = await fetcher(jobAdApi); return { props: { initialJobAds,