From cfb442f146b001fe74fcbdc27d646bdb1a663bc8 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Fri, 4 Mar 2022 14:38:15 +0530 Subject: [PATCH 1/9] feat: add ky to fetch data --- package.json | 1 + pages/index.tsx | 27 ++++++++++++++++----------- src/ky/instance.ts | 10 ++++++++++ src/types/main.d.ts | 4 ++++ utils/fetcher.ts | 15 +++++++++++---- yarn.lock | 5 +++++ 6 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/ky/instance.ts diff --git a/package.json b/package.json index a7af255c..47597c00 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@sentry/webpack-plugin": "^1.18.8", "babel-plugin-macros": "^3.1.0", "dayjs": "^1.10.8", + "ky": "^0.30.0", "next": "12.1.0", "nprogress": "^0.2.0", "react": "17.0.2", diff --git a/pages/index.tsx b/pages/index.tsx index be031396..22c3ffa4 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -54,31 +54,36 @@ export default function Index(props: Props): JSX.Element { export async function getStaticProps() { const date = new Date().toISOString() - const eventUrl = `https://api.eventyay.com/v1/events?cache=true - &filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${date}"}]}] + + const [event, eventErr] = await fetcher( + 'events', + `&filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${date}"}]}] &page[size]=6 &public=true &sort=starts-at` - const upcomingEventUrl = `https://api.eventyay.com/v1/events/upcoming?cache=true&page[size]=3&public=true&upcoming=true` - const groupsUrl = `https://api.eventyay.com/v1/groups?cache=true&filter=[{"name":"is-promoted","op":"eq","val":true}]&include=user,follower&page[size]=3&public=true` - - const [event, eventErr] = await fetcher(eventUrl) + ) if (eventErr) { console.error(eventErr) } - const events = toCamelCase(event.data) + const events = toCamelCase(event?.data) - const [upcomingEvent, upcomingEventErr] = await fetcher(upcomingEventUrl) + const [upcomingEvent, upcomingEventErr] = await fetcher( + 'events/upcoming', + 'page[size]=3&public=true&upcoming=true' + ) if (upcomingEventErr) { console.error(upcomingEventErr) } - const upcomingEvents = toCamelCase(upcomingEvent.data) + const upcomingEvents = toCamelCase(upcomingEvent?.data) - const [group, groupErr] = await fetcher(groupsUrl) + const [group, groupErr] = await fetcher( + 'groups', + 'filter=[{"name":"is-promoted","op":"eq","val":true}]&include=user,follower&page[size]=3&public=true' + ) if (groupErr) { console.error(groupErr) } - const groups = toCamelCase(group.data) + const groups = toCamelCase(group?.data) return { props: { events, upcomingEvents, groups }, diff --git a/src/ky/instance.ts b/src/ky/instance.ts new file mode 100644 index 00000000..450f32ed --- /dev/null +++ b/src/ky/instance.ts @@ -0,0 +1,10 @@ +import ky from 'ky' + +const BASE_URL = 'https://api.eventyay.com/v1' + +export const instance = ky.create({ + prefixUrl: BASE_URL, + headers: { + Accept: 'application/vnd.api+json', + }, +}) diff --git a/src/types/main.d.ts b/src/types/main.d.ts index 9c4f9bc2..45cc7446 100644 --- a/src/types/main.d.ts +++ b/src/types/main.d.ts @@ -1,3 +1,7 @@ +interface ServerProp { + data: ServerData[] +} + interface ServerData { type: string attributes: any diff --git a/utils/fetcher.ts b/utils/fetcher.ts index e53bd5b5..25e0e5d2 100644 --- a/utils/fetcher.ts +++ b/utils/fetcher.ts @@ -1,8 +1,15 @@ -export default async function fetcher(url: string) { +import { instance } from '../src/ky/instance' + +export default async function fetcher( + url: string, + searchParams: string +): Promise<[data: ServerProp | null, err: Error | null]> { try { - const data = await fetch(url) - const res = await data.json() - return [res, null] + const data: ServerProp = await instance(url, { + searchParams: searchParams, + }).json() + + return [data, null] } catch (err) { return [null, err] } diff --git a/yarn.lock b/yarn.lock index f93ad06d..a9a4adff 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3823,6 +3823,11 @@ kleur@^3.0.3: resolved "https://registry.yarnpkg.com/kleur/-/kleur-3.0.3.tgz#a79c9ecc86ee1ce3fa6206d1216c501f147fc07e" integrity sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w== +ky@^0.30.0: + version "0.30.0" + resolved "https://registry.yarnpkg.com/ky/-/ky-0.30.0.tgz#a3d293e4f6c4604a9a4694eceb6ce30e73d27d64" + integrity sha512-X/u76z4JtDVq10u1JA5UQfatPxgPaVDMYTrgHyiTpGN2z4TMEJkIHsoSBBSg9SWZEIXTKsi9kHgiQ9o3Y/4yog== + language-subtag-registry@~0.3.2: version "0.3.21" resolved "https://registry.yarnpkg.com/language-subtag-registry/-/language-subtag-registry-0.3.21.tgz#04ac218bea46f04cb039084602c6da9e788dd45a" From 633f88ff34710dcf2a043a232068483af61c4fc2 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Fri, 4 Mar 2022 16:43:35 +0530 Subject: [PATCH 2/9] f --- utils/fetcher.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fetcher.ts b/utils/fetcher.ts index 25e0e5d2..da6b2a77 100644 --- a/utils/fetcher.ts +++ b/utils/fetcher.ts @@ -3,7 +3,7 @@ import { instance } from '../src/ky/instance' export default async function fetcher( url: string, searchParams: string -): Promise<[data: ServerProp | null, err: Error | null]> { +): Promise<[data: ServerProp | null, err: any]> { try { const data: ServerProp = await instance(url, { searchParams: searchParams, From c2cf0f34c19c6b8ee33accb3708ac6512132dce4 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Tue, 29 Mar 2022 12:57:49 +0530 Subject: [PATCH 3/9] complete pr --- package.json | 2 +- pages/index.tsx | 36 ++++++++++++++++++++---------------- utils/fetcher.ts | 15 ++++++++++----- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index d384211d..1d10be6e 100644 --- a/package.json +++ b/package.json @@ -117,4 +117,4 @@ ], "sourceLocale": "en" } -} \ No newline at end of file +} diff --git a/pages/index.tsx b/pages/index.tsx index 22c3ffa4..aee0a392 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -11,6 +11,7 @@ import { Trans } from '@lingui/macro' import FrontPage from '../src/components/templates/FrontPage' import fetcher from '../utils/fetcher' import toCamelCase from '../utils/camelCase' +import { PrivacyTipTwoTone } from '@mui/icons-material' interface Props { events: ServerData[] @@ -53,33 +54,36 @@ export default function Index(props: Props): JSX.Element { } export async function getStaticProps() { - const date = new Date().toISOString() - - const [event, eventErr] = await fetcher( - 'events', - `&filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${date}"}]}] - &page[size]=6 + const [event, eventErr] = await fetcher({ + url: `events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${new Date()}"}]}] + &page[size]=6 &public=true - &sort=starts-at` - ) + &sort=ends-at`, + }) if (eventErr) { console.error(eventErr) } const events = toCamelCase(event?.data) - const [upcomingEvent, upcomingEventErr] = await fetcher( - 'events/upcoming', - 'page[size]=3&public=true&upcoming=true' - ) + const [upcomingEvent, upcomingEventErr] = await fetcher({ + url: 'events/upcoming?page[size]=3&public=true&upcoming=true', + }) if (upcomingEventErr) { console.error(upcomingEventErr) } const upcomingEvents = toCamelCase(upcomingEvent?.data) - const [group, groupErr] = await fetcher( - 'groups', - 'filter=[{"name":"is-promoted","op":"eq","val":true}]&include=user,follower&page[size]=3&public=true' - ) + const grpFilter = JSON.stringify([ + { + name: 'is-promoted', + op: 'eq', + val: 'true', + }, + ]) + + const [group, groupErr] = await fetcher({ + url: `groups?filter=${grpFilter}&include=user,follower&page[size]=3&public=true`, + }) if (groupErr) { console.error(groupErr) } diff --git a/utils/fetcher.ts b/utils/fetcher.ts index da6b2a77..fd1fc137 100644 --- a/utils/fetcher.ts +++ b/utils/fetcher.ts @@ -1,12 +1,17 @@ import { instance } from '../src/ky/instance' -export default async function fetcher( - url: string, - searchParams: string -): Promise<[data: ServerProp | null, err: any]> { +interface Props { + url: string + method?: string +} + +export default async function fetcher({ + url, + method = 'GET', +}: Props): Promise<[data: ServerProp | null, err: any]> { try { const data: ServerProp = await instance(url, { - searchParams: searchParams, + method: method, }).json() return [data, null] From 08c585f662a11a603d2b19745572b653ab0229c6 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Tue, 29 Mar 2022 13:16:30 +0530 Subject: [PATCH 4/9] fix --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 1d10be6e..a7ddaabf 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@sentry/webpack-plugin": "^1.18.8", "babel-plugin-macros": "^3.1.0", "dayjs": "^1.11.0", + "ky": "^0.30.0", "next": "12.1.1", "nprogress": "^0.2.0", "react": "17.0.2", From 5929e322fa7ce347614d42195565e06237846474 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Tue, 29 Mar 2022 15:11:03 +0530 Subject: [PATCH 5/9] fix build --- pages/index.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index aee0a392..9031acd1 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -11,7 +11,6 @@ import { Trans } from '@lingui/macro' import FrontPage from '../src/components/templates/FrontPage' import fetcher from '../utils/fetcher' import toCamelCase from '../utils/camelCase' -import { PrivacyTipTwoTone } from '@mui/icons-material' interface Props { events: ServerData[] @@ -55,10 +54,7 @@ export default function Index(props: Props): JSX.Element { export async function getStaticProps() { const [event, eventErr] = await fetcher({ - url: `events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${new Date()}"}]}] - &page[size]=6 - &public=true - &sort=ends-at`, + url: `events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${new Date()}"}]}]&page[size]=6&public=true&sort=ends-at`, }) if (eventErr) { console.error(eventErr) From 21515797b933692d251a480302b7a15824c8f296 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Tue, 29 Mar 2022 15:18:19 +0530 Subject: [PATCH 6/9] change date to ISO string --- pages/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/index.tsx b/pages/index.tsx index 9031acd1..fc848fb1 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -54,7 +54,7 @@ export default function Index(props: Props): JSX.Element { export async function getStaticProps() { const [event, eventErr] = await fetcher({ - url: `events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${new Date()}"}]}]&page[size]=6&public=true&sort=ends-at`, + url: `events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${new Date().toISOString()}"}]}]&page[size]=6&public=true&sort=ends-at`, }) if (eventErr) { console.error(eventErr) From 5501f7daa1bd843f9312808d56906b51da966672 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Wed, 30 Mar 2022 12:27:36 +0530 Subject: [PATCH 7/9] fix again --- package.json | 3 ++- utils/fetcher.ts | 6 +----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 16f19059..5c1c6ecb 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@sentry/webpack-plugin": "^1.18.8", "babel-plugin-macros": "^3.1.0", "dayjs": "^1.11.0", + "ky": "^0.30.0", "next": "12.1.2", "nprogress": "^0.2.0", "react": "17.0.2", @@ -117,4 +118,4 @@ ], "sourceLocale": "en" } -} \ No newline at end of file +} diff --git a/utils/fetcher.ts b/utils/fetcher.ts index 521aac01..396770c4 100644 --- a/utils/fetcher.ts +++ b/utils/fetcher.ts @@ -2,17 +2,13 @@ import { instance } from '../src/ky/instance' interface Props { url: string - method?: string } export default async function fetcher({ url, - method = 'GET', }: Props): Promise<[data: ServerProp | null, err: any]> { try { - const data: ServerProp = await instance(url, { - method: method, - }).then((res) => res.json()) + const data: ServerProp = await instance.get(url).json() return [data, null] } catch (err) { From 4e16cc320bab4b4c7976811a1b367406c78ef338 Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Wed, 30 Mar 2022 22:45:19 +0530 Subject: [PATCH 8/9] fixed build --- pages/index.tsx | 14 ++++++++------ utils/fetcher.ts | 6 +++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pages/index.tsx b/pages/index.tsx index 0ab6e3dd..f97b358f 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -54,12 +54,14 @@ export default function Index(props: Props): JSX.Element { export async function getStaticProps() { const date = new Date().toISOString() - const [event, eventErr] = await fetcher({ - url: `events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${date}"}]}]&page[size]=6&public=true&sort=starts-at`, - }) - if (eventErr) { - console.error(eventErr) - } + const event = await fetch( + `https://api.eventyay.com/v1/events?filter=[{"and":[{"name":"state","op":"eq","val":"published"},{"name":"privacy","op":"eq","val":"public"},{"name":"is-featured","op":"eq","val":true}]},{"or":[{"name":"ends-at","op":"ge","val":"${date}"}]}]&page[size]=6&public=true&sort=starts-at` + ) + .then((res) => { + return res.json() + }) + .catch((err) => console.error(err)) + const events = toCamelCase(event?.data) const [upcomingEvent, upcomingEventErr] = await fetcher({ diff --git a/utils/fetcher.ts b/utils/fetcher.ts index 396770c4..fd1fc137 100644 --- a/utils/fetcher.ts +++ b/utils/fetcher.ts @@ -2,13 +2,17 @@ import { instance } from '../src/ky/instance' interface Props { url: string + method?: string } export default async function fetcher({ url, + method = 'GET', }: Props): Promise<[data: ServerProp | null, err: any]> { try { - const data: ServerProp = await instance.get(url).json() + const data: ServerProp = await instance(url, { + method: method, + }).json() return [data, null] } catch (err) { From 6b01f40d74cd9db2c7a2f725e58457be454f700a Mon Sep 17 00:00:00 2001 From: Shubham Upreti Date: Wed, 30 Mar 2022 23:05:24 +0530 Subject: [PATCH 9/9] f --- .vscode/settings.json | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 0967ef42..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1 +0,0 @@ -{}