From 6007f44eb40ca92549ec3bf443ea566c0ac817a3 Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Thu, 8 Apr 2021 23:07:04 +0300 Subject: [PATCH] add limit query parameter for FeedApi, check other APIs for missing options --- src/api/eventApi.ts | 6 +++++- src/api/feedApi.ts | 12 ++++++++++-- src/api/jobAdApi.ts | 6 +++++- src/api/signupApi.ts | 8 +++++--- src/api/tagApi.ts | 7 ++++--- src/hooks/useFetchEvents.ts | 5 ++++- src/hooks/useFetchFeed.ts | 6 +++++- src/hooks/useFetchJobAds.ts | 9 ++++++++- src/pages/index.tsx | 8 ++++++-- 9 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/api/eventApi.ts b/src/api/eventApi.ts index 86fdaa4..83cdc55 100644 --- a/src/api/eventApi.ts +++ b/src/api/eventApi.ts @@ -8,6 +8,7 @@ export const URL = `${process.env.NEXT_PUBLIC_API_URL}/events/`; export interface Options { onlyNonPast?: boolean; limit?: number; + offset?: number; auth?: boolean; } @@ -26,11 +27,14 @@ class EventApi { } static async getEvents(options: Options = {}): Promise { - const { onlyNonPast, limit, auth } = options; + const { + onlyNonPast, limit, offset, auth, + } = options; try { const params = { since: onlyNonPast ? (new Date()).toISOString() : undefined, limit, + offset, }; const headers = auth ? { Authorization: getAuthHeader() } : null; const resp = await axios.get(`${URL}`, { diff --git a/src/api/feedApi.ts b/src/api/feedApi.ts index 759462e..2f00aa2 100644 --- a/src/api/feedApi.ts +++ b/src/api/feedApi.ts @@ -6,15 +6,23 @@ import { getAuthHeader } from "@utils/auth"; export const URL = `${process.env.NEXT_PUBLIC_API_URL}/feed/`; export interface Options { + limit?: number; + offset?: number; auth?: boolean; } class FeedApi { static async getFeed(options: Options = {}): Promise { - const { auth } = options; + const { + limit, offset, auth, + } = options; + const params = { + limit, + offset, + }; const headers = auth ? { Authorization: getAuthHeader() } : null; try { - const resp = await axios.get(URL, { headers }); + const resp = await axios.get(URL, { params, headers }); return resp.data.results; } catch (err) { console.error(err); diff --git a/src/api/jobAdApi.ts b/src/api/jobAdApi.ts index a26c28d..eb36d25 100644 --- a/src/api/jobAdApi.ts +++ b/src/api/jobAdApi.ts @@ -8,16 +8,20 @@ export const URL = `${process.env.NEXT_PUBLIC_API_URL}/jobads/`; export interface Options { onlyNonPast?: boolean; limit?: number; + offset?: number; auth?: boolean; } class JobAdApi { static async getJobAds(options: Options = {}): Promise { - const { onlyNonPast, limit, auth } = options; + const { + onlyNonPast, limit, offset, auth, + } = options; try { const params = { since: onlyNonPast ? (new Date()).toISOString() : undefined, limit, + offset, }; const headers = auth ? { Authorization: getAuthHeader() } : null; const resp = await axios.get(`${URL}`, { diff --git a/src/api/signupApi.ts b/src/api/signupApi.ts index fa57f44..6b36509 100644 --- a/src/api/signupApi.ts +++ b/src/api/signupApi.ts @@ -6,10 +6,12 @@ import { getAuthHeader } from "@utils/auth"; export const URL = `${process.env.NEXT_PUBLIC_API_URL}/signup/`; export const FORM_URL = `${process.env.NEXT_PUBLIC_API_URL}/signupForm/`; +// eslint-disable-next-line @typescript-eslint/no-empty-interface export interface Options { - onlyNonPast?: boolean; - limit?: number; - auth?: boolean; + // onlyNonPast?: boolean; + // limit?: number; + // offset?: number; + // auth?: boolean; } class SignupApi { diff --git a/src/api/tagApi.ts b/src/api/tagApi.ts index 8ec4f4c..df41d10 100644 --- a/src/api/tagApi.ts +++ b/src/api/tagApi.ts @@ -4,10 +4,11 @@ import Tag from "@models/Tag"; export const URL = `${process.env.NEXT_PUBLIC_API_URL}/tags/`; +// eslint-disable-next-line @typescript-eslint/no-empty-interface export interface Options { - onlyNonPast?: boolean; - limit?: number; - auth?: boolean; + // limit?: number; + // offset?: number; + // auth?: boolean; } class TagApi { diff --git a/src/hooks/useFetchEvents.ts b/src/hooks/useFetchEvents.ts index 5c32d00..eb8e2db 100644 --- a/src/hooks/useFetchEvents.ts +++ b/src/hooks/useFetchEvents.ts @@ -10,7 +10,9 @@ const fetcher = (url: string, config: AxiosRequestConfig) => axios.get(url, conf const generateFetchParams = (id = "", options: Options = {}) => { const url = `${URL}${id}`; - const { auth, onlyNonPast, limit } = options; + const { + auth, onlyNonPast, limit, offset, + } = options; return { url, @@ -18,6 +20,7 @@ const generateFetchParams = (id = "", options: Options = {}) => { params: { since: onlyNonPast ? (new Date()).toISOString() : undefined, limit, + offset, }, headers: auth ? { Authorization: getAuthHeader() } : null, }, diff --git a/src/hooks/useFetchFeed.ts b/src/hooks/useFetchFeed.ts index 1124a46..e17d063 100644 --- a/src/hooks/useFetchFeed.ts +++ b/src/hooks/useFetchFeed.ts @@ -10,11 +10,15 @@ const feedFetcher = (url: string, config?: AxiosRequestConfig) => axios.get(url, const generateFetchParams = (id = "", options: Options = {}) => { const url = `${URL}${id}`; - const { auth } = options; + const { auth, limit, offset } = options; return { url, config: { + params: { + limit, + offset, + }, headers: auth ? { Authorization: getAuthHeader() } : null, }, }; diff --git a/src/hooks/useFetchJobAds.ts b/src/hooks/useFetchJobAds.ts index 100363a..6353816 100644 --- a/src/hooks/useFetchJobAds.ts +++ b/src/hooks/useFetchJobAds.ts @@ -10,11 +10,18 @@ const jobAdFetcher = (url: string, config?: AxiosRequestConfig) => axios.get(url const generateFetchParams = (id = "", options: Options = {}) => { const url = `${URL}${id}`; - const { auth } = options; + const { + onlyNonPast, limit, offset, auth, + } = options; return { url, config: { + params: { + since: onlyNonPast ? (new Date()).toISOString() : undefined, + limit, + offset, + }, headers: auth ? { Authorization: getAuthHeader() } : null, }, }; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 23483b9..37bb458 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -15,6 +15,10 @@ const eventOptions = { limit: 4, }; +const feedOptions = { + limit: 4, +}; + interface InitialProps { initialEvents: Event[]; initialFeed: Post[]; @@ -22,7 +26,7 @@ interface InitialProps { const FrontPage: NextPage = ({ initialEvents, initialFeed }) => { const eventResult = useFetchEvents({ initialData: initialEvents, options: eventOptions }); - const feedResult = useFetchFeed({ initialData: initialFeed }); + const feedResult = useFetchFeed({ initialData: initialFeed, options: feedOptions }); return ( <> @@ -38,7 +42,7 @@ const FrontPage: NextPage = ({ initialEvents, initialFeed }) => { export const getStaticProps: GetStaticProps = async () => { const initialEvents = await EventApi.getEvents(eventOptions); - const initialFeed = await FeedApi.getFeed(); + const initialFeed = await FeedApi.getFeed(feedOptions); return { props: { initialEvents,