Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor API helper function param structure #2347

Merged
merged 11 commits into from
Jan 9, 2025
Merged
37 changes: 22 additions & 15 deletions pages/sites/[slug]/[locale]/all.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,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 @@ -66,10 +66,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 @@ -78,18 +78,17 @@ export default function Home({ pageProps }: Props) {
loadTenantScore();
}, []);


const [treesDonated, setTreesDonated] = React.useState<TreesDonated | null>(
null
);

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 All @@ -103,12 +102,20 @@ export default function Home({ pageProps }: Props) {
switch (pageProps.tenantConfig.config.slug) {
case 'planet':
AllPage = (
<LeaderBoard leaderboard={leaderboard} tenantScore={tenantScore} treesDonated={treesDonated} />
<LeaderBoard
leaderboard={leaderboard}
tenantScore={tenantScore}
treesDonated={treesDonated}
/>
);
return AllPage;
case 'ttc':
AllPage = (
<LeaderBoard leaderboard={leaderboard} tenantScore={tenantScore} treesDonated={treesDonated}/>
<LeaderBoard
leaderboard={leaderboard}
tenantScore={tenantScore}
treesDonated={treesDonated}
/>
);
return AllPage;
default:
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 @@ -83,13 +83,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 @@ -52,10 +52,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 @@ -67,10 +67,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 @@ -57,26 +57,25 @@
const { token, user, logoutUser, contextLoaded } = useUserProps();

// Checks context and sets project, bulk method if not already set within context
const checkContext = useCallback(async () => {
if (planetCashAccount && token && contextLoaded && projectList) {
if (!project) {
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(

Check notice on line 78 in pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx

View check run for this annotation

codefactor.io / CodeFactor

pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx#L60-L78

Complex Method
(project) => project.guid === paymentOptions.id
);
if (!retrievedProject) {
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 @@ -65,16 +65,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,
}
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
);
setPaymentHistory({
...paymentHistory,
Expand All @@ -91,12 +93,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 @@ -108,16 +110,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 @@ -68,12 +68,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 @@ -52,12 +52,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 @@ -78,13 +78,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,
});
mohitb35 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -93,15 +93,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 @@ -113,17 +113,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 @@ -38,10 +38,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 @@ -43,10 +43,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 @@ -24,10 +24,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 @@ -100,7 +100,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 @@ -172,10 +172,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
Loading