diff --git a/packages/next-common/services/serverSide/fellowship/common.js b/packages/next-common/services/serverSide/fellowship/common.js new file mode 100644 index 0000000000..42f04fe3f7 --- /dev/null +++ b/packages/next-common/services/serverSide/fellowship/common.js @@ -0,0 +1,39 @@ +import { withCommonProps } from "next-common/lib"; +import { fetchOpenGovTracksProps } from "next-common/services/serverSide"; +import nextApi from "next-common/services/nextApi"; +import { + fellowshipMembersApiUri, + fellowshipParamsApi, + fellowshipSalaryClaimantsApi, +} from "next-common/services/url"; +import { merge, noop } from "lodash-es"; + +export function withFellowshipSalaryCommonProps(fn = noop) { + return withCommonProps(async (context) => { + const [ + tracksProps, + { result: fellowshipMembers }, + { result: fellowshipParams = {} }, + { result: fellowshipSalaryClaimants }, + ] = await Promise.all([ + fetchOpenGovTracksProps(), + nextApi.fetch(fellowshipMembersApiUri), + nextApi.fetch(fellowshipParamsApi), + nextApi.fetch(fellowshipSalaryClaimantsApi), + ]); + + const res = await fn?.(context); + + return merge( + { + props: { + ...tracksProps, + fellowshipMembers, + fellowshipParams, + fellowshipSalaryClaimants, + }, + }, + res, + ); + }); +} diff --git a/packages/next/pages/fellowship/salary/claimants/index.jsx b/packages/next/pages/fellowship/salary/claimants/index.jsx index aacb70b873..6c4a2d7637 100644 --- a/packages/next/pages/fellowship/salary/claimants/index.jsx +++ b/packages/next/pages/fellowship/salary/claimants/index.jsx @@ -1,13 +1,6 @@ import FellowshipSalaryCommon from "next-common/components/fellowship/salary/common"; import FellowshipSalaryClaimantsContainer from "next-common/components/fellowship/salary/claimants/container"; -import { withCommonProps } from "next-common/lib"; -import { fetchOpenGovTracksProps } from "next-common/services/serverSide"; -import nextApi from "next-common/services/nextApi"; -import { - fellowshipMembersApiUri, - fellowshipParamsApi, - fellowshipSalaryClaimantsApi, -} from "next-common/services/url"; +import { withFellowshipSalaryCommonProps } from "next-common/services/serverSide/fellowship/common"; export default function FellowshipSalaryClaimantsPage() { return ( @@ -17,25 +10,4 @@ export default function FellowshipSalaryClaimantsPage() { ); } -export const getServerSideProps = withCommonProps(async () => { - const [ - tracksProps, - { result: fellowshipMembers }, - { result: fellowshipParams = {} }, - { result: fellowshipSalaryClaimants }, - ] = await Promise.all([ - fetchOpenGovTracksProps(), - nextApi.fetch(fellowshipMembersApiUri), - nextApi.fetch(fellowshipParamsApi), - nextApi.fetch(fellowshipSalaryClaimantsApi), - ]); - - return { - props: { - ...tracksProps, - fellowshipMembers, - fellowshipParams, - fellowshipSalaryClaimants, - }, - }; -}); +export const getServerSideProps = withFellowshipSalaryCommonProps(); diff --git a/packages/next/pages/fellowship/salary/cycles/[...params].jsx b/packages/next/pages/fellowship/salary/cycles/[...params].jsx index a0a4e31437..d4f581ad99 100644 --- a/packages/next/pages/fellowship/salary/cycles/[...params].jsx +++ b/packages/next/pages/fellowship/salary/cycles/[...params].jsx @@ -1,5 +1,3 @@ -import { withCommonProps } from "next-common/lib"; -import { fetchOpenGovTracksProps } from "next-common/services/serverSide"; import FellowshipSalaryCycleLayout from "next-common/components/fellowship/salary/cycles/layout"; import FellowshipSalaryCycleDetailTabsList from "next-common/components/fellowship/salary/cycles/tabsList"; import FellowshipSalaryCycleDetailInfo from "next-common/components/fellowship/salary/cycles/info"; @@ -12,6 +10,7 @@ import { fellowshipSalaryCycleRegisteredPaymentsApi, fellowshipSalaryCycleUnregisteredPaymentsApi, } from "next-common/services/url"; +import { withFellowshipSalaryCommonProps } from "next-common/services/serverSide/fellowship/common"; export default function FellowshipSalaryCyclePage({ cycle }) { return ( @@ -28,56 +27,54 @@ export default function FellowshipSalaryCyclePage({ cycle }) { ); } -export const getServerSideProps = withCommonProps(async (context) => { - const { - params: [id], - page, - } = context.query; +export const getServerSideProps = withFellowshipSalaryCommonProps( + async (context) => { + const { + params: [id], + page, + } = context.query; - const [tracksProps, { result: cycle }] = await Promise.all([ - fetchOpenGovTracksProps(), - nextApi.fetch(fellowshipSalaryCycleApi(id)), - ]); + const { result: cycle } = await nextApi.fetch(fellowshipSalaryCycleApi(id)); - let registrations; - let registeredPayments; - let unRegisteredPayments; - let feeds; + let registrations; + let registeredPayments; + let unRegisteredPayments; + let feeds; - if (cycle) { - const [ - { result: registrationsResult }, - { result: registeredPaymentsResult }, - { result: unRegisteredPaymentsResult }, - { result: feedsResult }, - ] = await Promise.all([ - nextApi.fetch(fellowshipSalaryCycleRegistrationsApi(id), { page }), - nextApi.fetch(fellowshipSalaryCycleRegisteredPaymentsApi(id), { - page, - }), - nextApi.fetch(fellowshipSalaryCycleUnregisteredPaymentsApi(id), { - page, - }), - nextApi.fetch(fellowshipSalaryCycleFeedsApi(id), { - page, - }), - ]); + if (cycle) { + const [ + { result: registrationsResult }, + { result: registeredPaymentsResult }, + { result: unRegisteredPaymentsResult }, + { result: feedsResult }, + ] = await Promise.all([ + nextApi.fetch(fellowshipSalaryCycleRegistrationsApi(id), { page }), + nextApi.fetch(fellowshipSalaryCycleRegisteredPaymentsApi(id), { + page, + }), + nextApi.fetch(fellowshipSalaryCycleUnregisteredPaymentsApi(id), { + page, + }), + nextApi.fetch(fellowshipSalaryCycleFeedsApi(id), { + page, + }), + ]); - registrations = registrationsResult; - registeredPayments = registeredPaymentsResult; - unRegisteredPayments = unRegisteredPaymentsResult; - feeds = feedsResult; - } + registrations = registrationsResult; + registeredPayments = registeredPaymentsResult; + unRegisteredPayments = unRegisteredPaymentsResult; + feeds = feedsResult; + } - return { - props: { - ...tracksProps, - id, - cycle: cycle || null, - registrations: registrations || {}, - registeredPayments: registeredPayments || {}, - unRegisteredPayments: unRegisteredPayments || {}, - feeds: feeds || {}, - }, - }; -}); + return { + props: { + id, + cycle: cycle || null, + registrations: registrations || {}, + registeredPayments: registeredPayments || {}, + unRegisteredPayments: unRegisteredPayments || {}, + feeds: feeds || {}, + }, + }; + }, +); diff --git a/packages/next/pages/fellowship/salary/feeds.js b/packages/next/pages/fellowship/salary/feeds.js index 735f90ccc6..89bd5f0ec5 100644 --- a/packages/next/pages/fellowship/salary/feeds.js +++ b/packages/next/pages/fellowship/salary/feeds.js @@ -1,9 +1,8 @@ -import { withCommonProps } from "next-common/lib"; -import { fetchOpenGovTracksProps } from "next-common/services/serverSide"; import nextApi from "next-common/services/nextApi"; import { defaultPageSize } from "next-common/utils/constants"; import FellowshipSalaryCommon from "next-common/components/fellowship/salary/common"; import FellowshipSalaryFeedsContainer from "next-common/components/fellowship/salary/feeds/container"; +import { withFellowshipSalaryCommonProps } from "next-common/services/serverSide/fellowship/common"; export default function FellowshipSalaryFeedsPage({ fellowshipSalaryFeeds }) { return ( @@ -13,21 +12,22 @@ export default function FellowshipSalaryFeedsPage({ fellowshipSalaryFeeds }) { ); } -export const getServerSideProps = withCommonProps(async (context) => { - const { page = 0 } = context.query; +export const getServerSideProps = withFellowshipSalaryCommonProps( + async (context) => { + const { page = 0 } = context.query; - const [tracksProps, { result: fellowshipSalaryFeeds }] = await Promise.all([ - fetchOpenGovTracksProps(), - nextApi.fetch("fellowship/salary/feeds", { - page, - page_size: defaultPageSize, - }), - ]); + const { result: fellowshipSalaryFeeds } = await nextApi.fetch( + "fellowship/salary/feeds", + { + page, + page_size: defaultPageSize, + }, + ); - return { - props: { - ...tracksProps, - fellowshipSalaryFeeds, - }, - }; -}); + return { + props: { + fellowshipSalaryFeeds: fellowshipSalaryFeeds || {}, + }, + }; + }, +); diff --git a/packages/next/pages/fellowship/salary/index.js b/packages/next/pages/fellowship/salary/index.js index 8dde90fdb2..0cb564d1fa 100644 --- a/packages/next/pages/fellowship/salary/index.js +++ b/packages/next/pages/fellowship/salary/index.js @@ -1,9 +1,8 @@ -import { withCommonProps } from "next-common/lib"; -import { fetchOpenGovTracksProps } from "next-common/services/serverSide"; import nextApi from "next-common/services/nextApi"; import FellowshipSalaryCommon from "next-common/components/fellowship/salary/common"; import FellowshipHistoryCyclesSection from "next-common/components/fellowship/salary/cycles/section"; import FellowshipSalaryActiveCycle from "next-common/components/fellowship/salary/cycles/current"; +import { withFellowshipSalaryCommonProps } from "next-common/services/serverSide/fellowship/common"; export default function FellowshipSalaryPage() { return ( @@ -14,15 +13,13 @@ export default function FellowshipSalaryPage() { ); } -export const getServerSideProps = withCommonProps(async () => { - const [tracksProps, { result: historyCycles = {} }] = await Promise.all([ - fetchOpenGovTracksProps(), - nextApi.fetch("fellowship/salary/history_cycles"), - ]); +export const getServerSideProps = withFellowshipSalaryCommonProps(async () => { + const { result: historyCycles = {} } = nextApi.fetch( + "fellowship/salary/history_cycles", + ); return { props: { - ...tracksProps, historyCycles, }, };