Skip to content

Commit

Permalink
Merge pull request #2347 from Plant-for-the-Planet-org/feature/object…
Browse files Browse the repository at this point in the history
…-param-api-helper-function

Refactor API helper function param structure
  • Loading branch information
mohitb35 authored Jan 9, 2025
2 parents b5d2798 + b578413 commit 4734f75
Show file tree
Hide file tree
Showing 73 changed files with 644 additions and 591 deletions.
24 changes: 12 additions & 12 deletions pages/sites/[slug]/[locale]/all.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export default function Home({ pageProps }: Props) {
React.useEffect(() => {
async function loadLeaderboard() {
try {
const newLeaderboard = await getRequest<LeaderBoardList>(
pageProps.tenantConfig.id,
`/app/leaderboard/${pageProps.tenantConfig.id}`
);
const newLeaderboard = await getRequest<LeaderBoardList>({
tenant: pageProps.tenantConfig.id,
url: `/app/leaderboard/${pageProps.tenantConfig.id}`,
});
setLeaderboard(newLeaderboard);
} catch (err) {
setErrors(handleError(err as APIError));
Expand All @@ -69,10 +69,10 @@ export default function Home({ pageProps }: Props) {
React.useEffect(() => {
async function loadTenantScore() {
try {
const newTenantScore = await getRequest<TenantScore>(
pageProps.tenantConfig.id,
`/app/tenantScore/${pageProps.tenantConfig.id}`
);
const newTenantScore = await getRequest<TenantScore>({
tenant: pageProps.tenantConfig.id,
url: `/app/tenantScore/${pageProps.tenantConfig.id}`,
});
setTenantScore(newTenantScore);
} catch (err) {
setErrors(handleError(err as APIError));
Expand All @@ -88,10 +88,10 @@ export default function Home({ pageProps }: Props) {
React.useEffect(() => {
async function loadTreesDonated() {
try {
const newTreesDonated = await getRequest<TreesDonated>(
pageProps.tenantConfig.id,
`${process.env.WEBHOOK_URL}/platform/total-tree-count`
);
const newTreesDonated = await getRequest<TreesDonated>({
tenant: pageProps.tenantConfig.id,
url: `${process.env.WEBHOOK_URL}/platform/total-tree-count`,
});
setTreesDonated(newTreesDonated);
} catch (err) {
setErrors(handleError(err as APIError));
Expand Down
12 changes: 6 additions & 6 deletions pages/sites/[slug]/[locale]/claim/[type]/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ function ClaimDonation({ pageProps }: Props): ReactElement {
};
if (contextLoaded && user) {
try {
const res = await postAuthenticatedRequest<RedeemedCodeData>(
pageProps.tenantConfig.id,
`/app/redeem`,
submitData,
const res = await postAuthenticatedRequest<RedeemedCodeData>({
tenant: pageProps.tenantConfig.id,
url: `/app/redeem`,
data: submitData,
token,
logoutUser
);
logoutUser,
});
setRedeemedCodeData(res);
} catch (err) {
const serializedErrors = handleError(err as APIError);
Expand Down
16 changes: 8 additions & 8 deletions pages/sites/[slug]/[locale]/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export default function Home({ pageProps }: Props) {
React.useEffect(() => {
async function loadTenantScore() {
try {
const newTenantScore = await getRequest<TenantScore>(
pageProps.tenantConfig.id,
`/app/tenantScore`
);
const newTenantScore = await getRequest<TenantScore>({
tenant: pageProps.tenantConfig.id,
url: `/app/tenantScore`,
});
setTenantScore(newTenantScore);
} catch (err) {
setErrors(handleError(err as APIError));
Expand All @@ -70,10 +70,10 @@ export default function Home({ pageProps }: Props) {
React.useEffect(() => {
async function loadLeaderboard() {
try {
const newLeaderBoard = await getRequest<LeaderBoardList>(
pageProps.tenantConfig.id,
`/app/leaderboard`
);
const newLeaderBoard = await getRequest<LeaderBoardList>({
tenant: pageProps.tenantConfig.id,
url: `/app/leaderboard`,
});
setLeaderboard(newLeaderBoard);
} catch (err) {
setErrors(handleError(err as APIError));
Expand Down
15 changes: 7 additions & 8 deletions pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,16 @@ export default function BulkCodeIssueCodesPage({
if (router.isReady) {
try {
const paymentOptions =
await getAuthenticatedRequest<PaymentOptions>(
pageProps.tenantConfig.id,
`/app/paymentOptions/${router.query.id}`,
await getAuthenticatedRequest<PaymentOptions>({
tenant: pageProps.tenantConfig.id,
url: `/app/paymentOptions/${router.query.id}`,
token,
logoutUser,
undefined,
{
country: planetCashAccount.country,
queryParams: {
country: planetCashAccount?.country ?? '',
...(user !== null && { legacyPriceFor: user.id }),
}
);
},
});

if (paymentOptions) {
const retrievedProject = projectList.find(
Expand Down
42 changes: 22 additions & 20 deletions pages/sites/[slug]/[locale]/profile/history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ function AccountHistory({ pageProps }: Props): ReactElement {
if (next && paymentHistory?._links?.next) {
try {
const newPaymentHistory = await getAuthenticatedRequest<PaymentHistory>(
tenantConfig?.id,
`${
filter && accountingFilters
? accountingFilters[filter] +
'&' +
paymentHistory?._links?.next.split('?').pop()
: paymentHistory?._links?.next
}`,
token,
logoutUser
{
tenant: tenantConfig.id,
url: `${
filter && accountingFilters
? accountingFilters[filter] +
'&' +
paymentHistory?._links?.next.split('?').pop()
: paymentHistory?._links?.next
}`,
token,
logoutUser,
}
);
setPaymentHistory({
...paymentHistory,
Expand All @@ -92,12 +94,12 @@ function AccountHistory({ pageProps }: Props): ReactElement {
} else {
if (filter === null) {
try {
const paymentHistory = await getAuthenticatedRequest<PaymentHistory>(
tenantConfig?.id,
'/app/paymentHistory?limit=15',
const paymentHistory = await getAuthenticatedRequest<PaymentHistory>({
tenant: tenantConfig?.id,
url: '/app/paymentHistory?limit=15',
token,
logoutUser
);
logoutUser,
});
setPaymentHistory(paymentHistory);
setProgress(100);
setIsDataLoading(false);
Expand All @@ -109,16 +111,16 @@ function AccountHistory({ pageProps }: Props): ReactElement {
}
} else {
try {
const paymentHistory = await getAuthenticatedRequest<PaymentHistory>(
tenantConfig?.id,
`${
const paymentHistory = await getAuthenticatedRequest<PaymentHistory>({
tenant: tenantConfig?.id,
url: `${
filter && accountingFilters
? accountingFilters[filter] + '&limit=15'
: '/app/paymentHistory?limit=15'
}`,
token,
logoutUser
);
logoutUser,
});
setPaymentHistory(paymentHistory);
setProgress(100);
setIsDataLoading(false);
Expand Down
10 changes: 5 additions & 5 deletions pages/sites/[slug]/[locale]/profile/projects/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ function ManageSingleProject({
try {
const result = await getAuthenticatedRequest<
ProfileProjectTrees | ProfileProjectConservation
>(
tenantConfig.id,
`/app/profile/projects/${projectGUID}`,
>({
tenant: tenantConfig.id,
url: `/app/profile/projects/${projectGUID}`,
token,
logoutUser
);
logoutUser,
});
setProject(result);
setSetupAccess(true);
} catch (err) {
Expand Down
10 changes: 5 additions & 5 deletions pages/sites/[slug]/[locale]/profile/recurrency.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function RecurrentDonations({
setIsDataLoading(true);
setProgress(70);
try {
const recurrencies = await getAuthenticatedRequest<Subscription[]>(
tenantConfig.id,
'/app/subscriptions',
const recurrencies = await getAuthenticatedRequest<Subscription[]>({
tenant: tenantConfig.id,
url: '/app/subscriptions',
token,
logoutUser
);
logoutUser,
});
if (recurrencies && Array.isArray(recurrencies)) {
const activeRecurrencies = recurrencies?.filter(
(obj) => obj.status == 'active' || obj.status == 'trialing'
Expand Down
12 changes: 6 additions & 6 deletions pages/sites/[slug]/[locale]/profile/redeem/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ const RedeemCode = ({ pageProps: { tenantConfig } }: Props) => {

if (contextLoaded && user) {
try {
const res = await postAuthenticatedRequest<RedeemedCodeData>(
tenantConfig?.id,
`/app/redeem`,
submitData,
const res = await postAuthenticatedRequest<RedeemedCodeData>({
tenant: tenantConfig?.id,
url: `/app/redeem`,
data: submitData,
token,
logoutUser
);
logoutUser,
});
setRedeemedCodeData(res);
} catch (err) {
const serializedErrors = handleError(err as APIError);
Expand Down
12 changes: 6 additions & 6 deletions pages/sites/[slug]/[locale]/projects-archive/[p].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ export default function Donate({
setCurrencyCode(currency);
try {
const { p } = router.query;
const project = await getRequest<ProjectExtended>(
pageProps.tenantConfig.id,
encodeURI(`/app/projects/${p}`),
{
const project = await getRequest<ProjectExtended>({
tenant: pageProps.tenantConfig.id,
url: encodeURI(`/app/projects/${p}`),
queryParams: {
_scope: 'extended',
currency: currency || '',
locale: locale,
}
);
},
});
if (
project.purpose === 'conservation' ||
project.purpose === 'trees'
Expand Down
12 changes: 6 additions & 6 deletions pages/sites/[slug]/[locale]/projects-archive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,17 @@ export default function Donate({
setCurrencyCode(currency);
setInternalLanguage(locale);
try {
const projects = await getRequest<MapProject[]>(
pageProps.tenantConfig.id,
`/app/projects`,
{
const projects = await getRequest<MapProject[]>({
tenant: pageProps.tenantConfig.id,
url: `/app/projects`,
queryParams: {
_scope: 'map',
currency: currency,
tenant: pageProps.tenantConfig.id,
'filter[purpose]': 'trees,conservation',
locale: locale,
}
);
},
});
setProjects(projects);
setProject(null);
setShowSingleProject(false);
Expand Down
8 changes: 4 additions & 4 deletions pages/sites/[slug]/[locale]/s/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export default function DirectGift({

async function loadPublicUserData() {
try {
const newProfile = await getRequest<UserPublicProfile>(
tenantConfig.id,
`/app/profiles/${router.query.id}`
);
const newProfile = await getRequest<UserPublicProfile>({
tenant: tenantConfig.id,
url: `/app/profiles/${router.query.id}`,
});
if (newProfile.type !== 'tpo') {
localStorage.setItem(
'directGift',
Expand Down
8 changes: 4 additions & 4 deletions pages/sites/[slug]/[locale]/t/[profile].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const PublicProfilePage = ({ pageProps: { tenantConfig } }: Props) => {

async function loadPublicProfile(slug: string) {
try {
const profileData = await getRequest<UserPublicProfile>(
tenantConfig.id,
`/app/profiles/${slug}`
);
const profileData = await getRequest<UserPublicProfile>({
tenant: tenantConfig.id,
url: `/app/profiles/${slug}`,
});
setProfile(profileData);
} catch (err) {
setErrors(handleError(err as APIError));
Expand Down
7 changes: 3 additions & 4 deletions src/features/common/Layout/CurrencyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ export const CurrencyProvider: FC = ({ children }) => {

const fetchCurrencies = async () => {
try {
const currencyData = await getRequest<CurrencyList>(
undefined,
'/app/currencies'
);
const currencyData = await getRequest<CurrencyList>({
url: '/app/currencies',
});
setFetchCount(fetchCount + 1);
setSupportedCurrencies(
new Set(Object.keys(currencyData) as CurrencyCode[])
Expand Down
2 changes: 1 addition & 1 deletion src/features/common/Layout/UserPropsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const UserPropsProvider: FC = ({ children }) => {
try {
// TODO: Add error handling after figuring out the nature of getAccountInfo function call with impersonatedEmail

const res = await getAccountInfo(tenantConfig?.id, token);
const res = await getAccountInfo({ tenant: tenantConfig?.id, token });
if (res.status === 200) {
const resJson = await res.json();
setUser(resJson as User);
Expand Down
8 changes: 4 additions & 4 deletions src/features/projects/screens/Projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@ function ProjectsList({
React.useEffect(() => {
async function setListOrder() {
try {
const res = await getRequest<Tenant>(
tenantConfig.id,
`/app/tenants/${tenantConfig.id}`
);
const res = await getRequest<Tenant>({
tenant: tenantConfig.id,
url: `/app/tenants/${tenantConfig.id}`,
});
setShouldSortProjectList(res.topProjectsOnly);
} catch (err) {
setErrors(handleError(err as APIError));
Expand Down
Loading

0 comments on commit 4734f75

Please sign in to comment.