Fix typos in data fetching

This commit is contained in:
Aarni Halinen
2022-07-27 00:03:50 +03:00
parent 6f7ef76af4
commit eaab0f4e72
6 changed files with 98 additions and 58 deletions
+21 -13
View File
@@ -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<Event[]>(api, fetcher);
if (error) {
console.error(error);
return (
<div>
Failed loading events
</div>
);
}
if (!events?.length) {
return <div>No events.</div>;
}
@@ -65,15 +76,12 @@ const renderData = (events: Event[]) => {
);
};
const AdminEventPage: NextPage = () => {
const { data: events } = useSWR<Event[]>([APIPath.EVENTS, { auth: true }], fetcher);
return (
<AdminListCommon>
<h1>Events</h1>
<AddLink text="Create event" to={`${URL}/create`} data-e2e="create-event" />
{renderData(events)}
</AdminListCommon>
);
};
const AdminEventPage: NextPage = () => (
<AdminListCommon>
<h1>Events</h1>
<AddLink text="Create event" to={`${URL}/create`} data-e2e="create-event" />
<Renderer />
</AdminListCommon>
);
export default AdminEventPage;
+24 -14
View File
@@ -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 <div>No posts.</div>;
const Renderer: React.FC = () => {
const api: API = { path: APIPath.FEED, authenticated: true };
const { data: feed, error } = useSWR<Post[]>(api, fetcher);
if (error) {
console.error(error);
return (
<div>
Failed loading feed
</div>
);
}
if (!feed?.length) {
return (
<div>No posts.</div>
);
}
return (
@@ -65,15 +78,12 @@ const renderData = (feed: Post[]) => {
);
};
const AdminFeedPage: NextPage = () => {
const { data: feed } = useSWR<Post[]>([APIPath.FEED, { auth: true }], fetcher);
return (
<AdminListCommon>
<h1>Feed</h1>
<AddLink text="Create news post" to={`${URL}/create`} />
{renderData(feed)}
</AdminListCommon>
);
};
const AdminFeedPage: NextPage = () => (
<AdminListCommon>
<h1>Feed</h1>
<AddLink text="Create news post" to={`${URL}/create`} />
<Renderer />
</AdminListCommon>
);
export default AdminFeedPage;
+20 -13
View File
@@ -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<JobAd[]>(api, fetcher);
if (error) {
console.error(error);
return (
<div>
Failed loading jobads
</div>
);
}
if (!jobAds?.length) {
return <div>No advertisements.</div>;
}
@@ -69,15 +79,12 @@ const renderData = (jobAds: JobAd[]) => {
);
};
const AdminJobAdPage: NextPage = () => {
const { data: jobAds } = useSWR<JobAd[]>([APIPath.JOBADS, { auth: true }], fetcher);
return (
<AdminListCommon>
<h1>Job advertisements</h1>
<AddLink text="Create job ad" to={`${URL}/create`} />
{renderData(jobAds)}
</AdminListCommon>
);
};
const AdminJobAdPage: NextPage = () => (
<AdminListCommon>
<h1>Job advertisements</h1>
<AddLink text="Create job ad" to={`${URL}/create`} />
<Renderer />
</AdminListCommon>
);
export default AdminJobAdPage;
+15 -11
View File
@@ -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<InitialProps> = ({ initialEvents, initialFeed }) => {
const { data: events } = useSWR<Event[]>([APIPath.EVENTS, eventOptions], fetcher, { fallbackData: initialEvents });
const { data: feed } = useSWR<Post[]>([APIPath.FEED, feedOptions], fetcher, { fallbackData: initialFeed });
const { data: events } = useSWR<Event[]>(eventApi, fetcher, { fallbackData: initialEvents });
const { data: feed } = useSWR<Post[]>(feedApi, fetcher, { fallbackData: initialFeed });
return (
<>
@@ -40,8 +44,8 @@ const InEnglishPage: NextPage<InitialProps> = ({ initialEvents, initialFeed }) =
};
export const getStaticProps: GetStaticProps<InitialProps> = async () => {
const initialEvents = await EventApi.getEvents(eventOptions);
const initialFeed = await FeedApi.getFeed(feedOptions);
const initialEvents = await fetcher<Event[]>(eventApi);
const initialFeed = await fetcher<Post[]>(feedApi);
return {
props: {
initialEvents,
+11 -3
View File
@@ -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<InitialProps> = ({ initialEvents, initialFeed }) => {
const { data: events } = useSWR<Event[]>([APIPath.EVENTS, {}], fetcher, { fallbackData: initialEvents });
const { data: feed } = useSWR<Post[]>([APIPath.FEED, {}], fetcher, { fallbackData: initialFeed });
const { data: events } = useSWR<Event[]>(eventApi, fetcher, { fallbackData: initialEvents });
const { data: feed } = useSWR<Post[]>(feedApi, fetcher, { fallbackData: initialFeed });
return (
<>
+7 -4
View File
@@ -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<InitialProps> = ({ initialJobAds }) => {
const { data: jobAds } = useSWR<JobAd[]>([APIPath.JOBADS, {}], fetcher, { fallbackData: initialJobAds });
const { data: jobAds } = useSWR<JobAd[]>(jobAdApi, fetcher, { fallbackData: initialJobAds });
return (
<>
<Head>
@@ -27,7 +30,7 @@ const CorporatePage: NextPage<InitialProps> = ({ initialJobAds }) => {
};
export const getStaticProps: GetStaticProps<InitialProps> = async () => {
const initialJobAds = await JobAdApi.getJobAds();
const initialJobAds = await fetcher<JobAd[]>(jobAdApi);
return {
props: {
initialJobAds,