From bea66cf58b0c77ae96ae1aed800196ac2c73b915 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:01:17 +0530 Subject: [PATCH 01/14] feat: add GetStaticPaths type to enforce type safety in getStaticPaths return value --- pages/sites/[slug]/[locale]/[p].tsx | 22 +++++++------ pages/sites/[slug]/[locale]/all.tsx | 33 ++++++++++++------- .../[slug]/[locale]/claim/[type]/[code].tsx | 24 +++++++------- .../sites/[slug]/[locale]/complete-signup.tsx | 20 ++++++----- pages/sites/[slug]/[locale]/home.tsx | 20 ++++++----- pages/sites/[slug]/[locale]/index.tsx | 20 ++++++----- pages/sites/[slug]/[locale]/login.tsx | 20 ++++++----- .../[slug]/[locale]/mangrove-challenge.tsx | 3 +- pages/sites/[slug]/[locale]/mangroves.tsx | 3 +- .../sites/[slug]/[locale]/profile/api-key.tsx | 20 ++++++----- .../profile/bulk-codes/[method]/[id].tsx | 24 +++++++------- .../profile/bulk-codes/[method]/index.tsx | 22 +++++++------ .../[locale]/profile/bulk-codes/index.tsx | 20 ++++++----- .../[locale]/profile/delete-account.tsx | 20 ++++++----- .../[slug]/[locale]/profile/donation-link.tsx | 20 ++++++----- pages/sites/[slug]/[locale]/profile/edit.tsx | 20 ++++++----- .../[locale]/profile/giftfund/index.tsx | 20 ++++++----- .../sites/[slug]/[locale]/profile/history.tsx | 20 ++++++----- .../[locale]/profile/impersonate-user.tsx | 20 ++++++----- pages/sites/[slug]/[locale]/profile/index.tsx | 20 ++++++----- .../profile/payouts/add-bank-details.tsx | 20 ++++++----- .../payouts/edit-bank-details/[id].tsx | 22 +++++++------ .../[slug]/[locale]/profile/payouts/index.tsx | 20 ++++++----- .../[locale]/profile/payouts/schedule.tsx | 20 ++++++----- .../[locale]/profile/planetcash/index.tsx | 20 ++++++----- .../[locale]/profile/planetcash/new.tsx | 20 ++++++----- .../profile/planetcash/transactions.tsx | 20 ++++++----- .../[slug]/[locale]/profile/projects/[id].tsx | 22 +++++++------ .../[locale]/profile/projects/index.tsx | 20 ++++++----- .../[locale]/profile/projects/new-project.tsx | 20 ++++++----- .../[slug]/[locale]/profile/recurrency.tsx | 20 ++++++----- .../[slug]/[locale]/profile/redeem/[code].tsx | 22 +++++++------ .../[locale]/profile/register-trees.tsx | 20 ++++++----- .../profile/treemapper/data-explorer.tsx | 20 ++++++----- .../[locale]/profile/treemapper/import.tsx | 20 ++++++----- .../[locale]/profile/treemapper/index.tsx | 20 ++++++----- .../profile/treemapper/my-species.tsx | 20 ++++++----- .../sites/[slug]/[locale]/profile/widgets.tsx | 20 ++++++----- .../[slug]/[locale]/projects-archive/[p].tsx | 22 +++++++------ .../[locale]/projects-archive/index.tsx | 20 ++++++----- pages/sites/[slug]/[locale]/s/[id].tsx | 22 +++++++------ pages/sites/[slug]/[locale]/t/[profile].tsx | 22 +++++++------ pages/sites/[slug]/[locale]/verify-email.tsx | 20 ++++++----- .../[locale]/vto-fitness-challenge-2023.tsx | 3 +- .../[slug]/[locale]/vto-fitness-challenge.tsx | 3 +- 45 files changed, 481 insertions(+), 388 deletions(-) diff --git a/pages/sites/[slug]/[locale]/[p].tsx b/pages/sites/[slug]/[locale]/[p].tsx index 04ac3135c4..7a6a88deb9 100644 --- a/pages/sites/[slug]/[locale]/[p].tsx +++ b/pages/sites/[slug]/[locale]/[p].tsx @@ -1,4 +1,5 @@ import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -60,18 +61,19 @@ ProjectDetailsPage.getLayout = function getLayout( export default ProjectDetailsPage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - p: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + p: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/all.tsx b/pages/sites/[slug]/[locale]/all.tsx index 7d28be43ca..8f7375c4c5 100644 --- a/pages/sites/[slug]/[locale]/all.tsx +++ b/pages/sites/[slug]/[locale]/all.tsx @@ -17,6 +17,7 @@ import { getTenantConfig, } from '../../../../src/utils/multiTenancy/helpers'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -78,7 +79,6 @@ export default function Home({ pageProps }: Props) { loadTenantScore(); }, []); - const [treesDonated, setTreesDonated] = React.useState( null ); @@ -103,12 +103,20 @@ export default function Home({ pageProps }: Props) { switch (pageProps.tenantConfig.config.slug) { case 'planet': AllPage = ( - + ); return AllPage; case 'ttc': AllPage = ( - + ); return AllPage; default: @@ -127,17 +135,18 @@ export default function Home({ pageProps }: Props) { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx b/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx index 5ce505ae9b..abbe58c921 100644 --- a/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx +++ b/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx @@ -1,5 +1,6 @@ import type { ReactElement } from 'react'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -185,19 +186,20 @@ function ClaimDonation({ pageProps }: Props): ReactElement { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - type: v4(), - code: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + type: v4(), + code: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/complete-signup.tsx b/pages/sites/[slug]/[locale]/complete-signup.tsx index b7a67a0895..d7977d99f2 100644 --- a/pages/sites/[slug]/[locale]/complete-signup.tsx +++ b/pages/sites/[slug]/[locale]/complete-signup.tsx @@ -9,6 +9,7 @@ import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { useRouter } from 'next/router'; import { useTenant } from '../../../../src/features/common/Layout/TenantContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -43,17 +44,18 @@ export default function UserProfile({ pageProps }: Props) { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/home.tsx b/pages/sites/[slug]/[locale]/home.tsx index 57c52209cb..9ede8d8bb8 100644 --- a/pages/sites/[slug]/[locale]/home.tsx +++ b/pages/sites/[slug]/[locale]/home.tsx @@ -18,6 +18,7 @@ import { getTenantConfig, } from '../../../../src/utils/multiTenancy/helpers'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -129,17 +130,18 @@ export default function Home({ pageProps }: Props) { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/index.tsx b/pages/sites/[slug]/[locale]/index.tsx index 20dbd2dc70..15fa8c4475 100644 --- a/pages/sites/[slug]/[locale]/index.tsx +++ b/pages/sites/[slug]/[locale]/index.tsx @@ -1,4 +1,5 @@ import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -56,17 +57,18 @@ ProjectListPage.getLayout = function getLayout( export default ProjectListPage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/login.tsx b/pages/sites/[slug]/[locale]/login.tsx index 8a538c222e..fd11f49267 100644 --- a/pages/sites/[slug]/[locale]/login.tsx +++ b/pages/sites/[slug]/[locale]/login.tsx @@ -9,6 +9,7 @@ import { import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { useTenant } from '../../../../src/features/common/Layout/TenantContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -83,17 +84,18 @@ export default function Login({ pageProps }: Props): ReactElement { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/mangrove-challenge.tsx b/pages/sites/[slug]/[locale]/mangrove-challenge.tsx index 5d3221f986..48a63f4406 100644 --- a/pages/sites/[slug]/[locale]/mangrove-challenge.tsx +++ b/pages/sites/[slug]/[locale]/mangrove-challenge.tsx @@ -8,6 +8,7 @@ import { import { AbstractIntlMessages } from 'next-intl'; import { Tenant } from '@planet-sdk/common'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -98,7 +99,7 @@ export default function MangroveChallenge({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { return { paths: [{ params: { slug: 'salesforce', locale: 'en' } }], fallback: 'blocking', diff --git a/pages/sites/[slug]/[locale]/mangroves.tsx b/pages/sites/[slug]/[locale]/mangroves.tsx index 7c2c94a031..d00f3341c2 100644 --- a/pages/sites/[slug]/[locale]/mangroves.tsx +++ b/pages/sites/[slug]/[locale]/mangroves.tsx @@ -4,6 +4,7 @@ import GetHomeMeta from '../../../../src/utils/getMetaTags/GetHomeMeta'; import { AbstractIntlMessages } from 'next-intl'; import { Tenant } from '@planet-sdk/common'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,7 +51,7 @@ export default function MangrovesLandingPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { return { paths: [{ params: { slug: 'salesforce', locale: 'en' } }], fallback: 'blocking', diff --git a/pages/sites/[slug]/[locale]/profile/api-key.tsx b/pages/sites/[slug]/[locale]/profile/api-key.tsx index 7d7b60e8d1..cc3e722e1f 100644 --- a/pages/sites/[slug]/[locale]/profile/api-key.tsx +++ b/pages/sites/[slug]/[locale]/profile/api-key.tsx @@ -3,6 +3,7 @@ import React, { ReactElement } from 'react'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import ApiKey from '../../../../../src/features/user/Settings/ApiKey'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -47,17 +48,18 @@ function EditProfilePage({ pageProps: { tenantConfig } }: Props): ReactElement { export default EditProfilePage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx b/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx index 56371bdba7..b037aac15b 100644 --- a/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx +++ b/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx @@ -1,5 +1,6 @@ import type { ReactElement } from 'react'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -135,19 +136,20 @@ export default function BulkCodeIssueCodesPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - method: v4(), - id: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + method: v4(), + id: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/index.tsx b/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/index.tsx index 92dd59cbdb..f6660b7913 100644 --- a/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/index.tsx @@ -9,6 +9,7 @@ import { useBulkCode } from '../../../../../../../src/features/common/Layout/Bul import { BulkCodeMethods } from '../../../../../../../src/utils/constants/bulkCodeConstants'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -70,18 +71,19 @@ export default function BulkCodeSelectProjectPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - method: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + method: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/profile/bulk-codes/index.tsx b/pages/sites/[slug]/[locale]/profile/bulk-codes/index.tsx index 4ff4467fba..b50a9e6138 100644 --- a/pages/sites/[slug]/[locale]/profile/bulk-codes/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/bulk-codes/index.tsx @@ -6,6 +6,7 @@ import BulkCodes, { import Head from 'next/head'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -47,17 +48,18 @@ export default function BulkCodePage({ pageProps }: Props): ReactElement { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/delete-account.tsx b/pages/sites/[slug]/[locale]/profile/delete-account.tsx index 81592bd5a9..76a8078af4 100644 --- a/pages/sites/[slug]/[locale]/profile/delete-account.tsx +++ b/pages/sites/[slug]/[locale]/profile/delete-account.tsx @@ -4,6 +4,7 @@ import UserLayout from '../../../../../src/features/common/Layout/UserLayout/Use import { AbstractIntlMessages, useTranslations } from 'next-intl'; import DeleteProfile from '../../../../../src/features/user/Settings/DeleteProfile'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -49,17 +50,18 @@ function DeleteProfilePage({ export default DeleteProfilePage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/donation-link.tsx b/pages/sites/[slug]/[locale]/profile/donation-link.tsx index 67c129c838..cb0b798098 100644 --- a/pages/sites/[slug]/[locale]/profile/donation-link.tsx +++ b/pages/sites/[slug]/[locale]/profile/donation-link.tsx @@ -4,6 +4,7 @@ import DonationLink from '../../../../../src/features/user/Widget/DonationLink'; import Head from 'next/head'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -47,17 +48,18 @@ export default function DonationLinkPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/edit.tsx b/pages/sites/[slug]/[locale]/profile/edit.tsx index 0a171a832e..4f2e3b1091 100644 --- a/pages/sites/[slug]/[locale]/profile/edit.tsx +++ b/pages/sites/[slug]/[locale]/profile/edit.tsx @@ -1,5 +1,6 @@ import type { AbstractIntlMessages } from 'next-intl'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,17 +51,18 @@ function EditProfilePage({ pageProps: { tenantConfig } }: Props): ReactElement { export default EditProfilePage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/giftfund/index.tsx b/pages/sites/[slug]/[locale]/profile/giftfund/index.tsx index 44b786a4bc..858bf762c7 100644 --- a/pages/sites/[slug]/[locale]/profile/giftfund/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/giftfund/index.tsx @@ -4,6 +4,7 @@ import Head from 'next/head'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import GiftFunds from '../../../../../../src/features/user/GiftFunds'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -48,17 +49,18 @@ export default function Register({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/history.tsx b/pages/sites/[slug]/[locale]/profile/history.tsx index d761e2a5e7..b0c53a5f98 100644 --- a/pages/sites/[slug]/[locale]/profile/history.tsx +++ b/pages/sites/[slug]/[locale]/profile/history.tsx @@ -19,6 +19,7 @@ import { ErrorHandlingContext } from '../../../../../src/features/common/Layout/ import { handleError } from '@planet-sdk/common'; import DashboardView from '../../../../../src/features/common/Layout/DashboardView'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -177,17 +178,18 @@ function AccountHistory({ pageProps }: Props): ReactElement { export default AccountHistory; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/impersonate-user.tsx b/pages/sites/[slug]/[locale]/profile/impersonate-user.tsx index f30ff590c4..8905a65ac3 100644 --- a/pages/sites/[slug]/[locale]/profile/impersonate-user.tsx +++ b/pages/sites/[slug]/[locale]/profile/impersonate-user.tsx @@ -6,6 +6,7 @@ import { useUserProps } from '../../../../../src/features/common/Layout/UserProp import { ReactElement, useEffect } from 'react'; import AccessDeniedLoader from '../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -56,17 +57,18 @@ const ImpersonateUserPage = ({ export default ImpersonateUserPage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/index.tsx b/pages/sites/[slug]/[locale]/profile/index.tsx index f23fe00f83..ab715f604a 100644 --- a/pages/sites/[slug]/[locale]/profile/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/index.tsx @@ -1,6 +1,7 @@ import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { MyForestProvider } from '../../../../../src/features/common/Layout/MyForestContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -42,17 +43,18 @@ const MyForestPage = ({ pageProps: { tenantConfig } }: Props) => { export default MyForestPage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/payouts/add-bank-details.tsx b/pages/sites/[slug]/[locale]/profile/payouts/add-bank-details.tsx index ebdb249759..a52e06bd02 100644 --- a/pages/sites/[slug]/[locale]/profile/payouts/add-bank-details.tsx +++ b/pages/sites/[slug]/[locale]/profile/payouts/add-bank-details.tsx @@ -8,6 +8,7 @@ import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -56,17 +57,18 @@ export default function AddBankDetailsPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/payouts/edit-bank-details/[id].tsx b/pages/sites/[slug]/[locale]/profile/payouts/edit-bank-details/[id].tsx index 8e20b5f00c..37049cf1e1 100644 --- a/pages/sites/[slug]/[locale]/profile/payouts/edit-bank-details/[id].tsx +++ b/pages/sites/[slug]/[locale]/profile/payouts/edit-bank-details/[id].tsx @@ -6,6 +6,7 @@ import ManagePayouts, { } from '../../../../../../../src/features/user/ManagePayouts'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,18 +51,19 @@ export default function EditBankDetailsPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - id: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + id: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/profile/payouts/index.tsx b/pages/sites/[slug]/[locale]/profile/payouts/index.tsx index a4471eac26..2b629425dc 100644 --- a/pages/sites/[slug]/[locale]/profile/payouts/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/payouts/index.tsx @@ -9,6 +9,7 @@ import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -68,17 +69,18 @@ export default function OverviewPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/payouts/schedule.tsx b/pages/sites/[slug]/[locale]/profile/payouts/schedule.tsx index ea2df81f04..0fd8837357 100644 --- a/pages/sites/[slug]/[locale]/profile/payouts/schedule.tsx +++ b/pages/sites/[slug]/[locale]/profile/payouts/schedule.tsx @@ -8,6 +8,7 @@ import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -57,17 +58,18 @@ export default function PayoutSchedulePage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/planetcash/index.tsx b/pages/sites/[slug]/[locale]/profile/planetcash/index.tsx index dc33744dfd..068f2a9801 100644 --- a/pages/sites/[slug]/[locale]/profile/planetcash/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/planetcash/index.tsx @@ -7,6 +7,7 @@ import PlanetCash, { } from '../../../../../../src/features/user/PlanetCash'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -65,17 +66,18 @@ export default function PlanetCashPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/planetcash/new.tsx b/pages/sites/[slug]/[locale]/profile/planetcash/new.tsx index 9550ca2812..6304536ba2 100644 --- a/pages/sites/[slug]/[locale]/profile/planetcash/new.tsx +++ b/pages/sites/[slug]/[locale]/profile/planetcash/new.tsx @@ -7,6 +7,7 @@ import PlanetCash, { } from '../../../../../../src/features/user/PlanetCash'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -69,17 +70,18 @@ export default function PlanetCashCreatePage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/planetcash/transactions.tsx b/pages/sites/[slug]/[locale]/profile/planetcash/transactions.tsx index 53216e0870..09f848f4a1 100644 --- a/pages/sites/[slug]/[locale]/profile/planetcash/transactions.tsx +++ b/pages/sites/[slug]/[locale]/profile/planetcash/transactions.tsx @@ -7,6 +7,7 @@ import PlanetCash, { } from '../../../../../../src/features/user/PlanetCash'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -69,17 +70,18 @@ export default function PlanetCashTransactionsPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/projects/[id].tsx b/pages/sites/[slug]/[locale]/profile/projects/[id].tsx index f464cd340f..a0e9772086 100644 --- a/pages/sites/[slug]/[locale]/profile/projects/[id].tsx +++ b/pages/sites/[slug]/[locale]/profile/projects/[id].tsx @@ -11,6 +11,7 @@ import Head from 'next/head'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { ErrorHandlingContext } from '../../../../../../src/features/common/Layout/ErrorHandlingContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -124,18 +125,19 @@ function ManageSingleProject({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - id: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + id: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/profile/projects/index.tsx b/pages/sites/[slug]/[locale]/profile/projects/index.tsx index 17a84164f0..47ed378972 100644 --- a/pages/sites/[slug]/[locale]/profile/projects/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/projects/index.tsx @@ -6,6 +6,7 @@ import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,17 +51,18 @@ export default function Register({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/projects/new-project.tsx b/pages/sites/[slug]/[locale]/profile/projects/new-project.tsx index 86bd4af623..cff30a470f 100644 --- a/pages/sites/[slug]/[locale]/profile/projects/new-project.tsx +++ b/pages/sites/[slug]/[locale]/profile/projects/new-project.tsx @@ -7,6 +7,7 @@ import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoa import Footer from '../../../../../../src/features/common/Layout/Footer'; import Head from 'next/head'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -96,17 +97,18 @@ export default function AddProjectType({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/recurrency.tsx b/pages/sites/[slug]/[locale]/profile/recurrency.tsx index 417c5e1567..2dd3d7c822 100644 --- a/pages/sites/[slug]/[locale]/profile/recurrency.tsx +++ b/pages/sites/[slug]/[locale]/profile/recurrency.tsx @@ -10,6 +10,7 @@ import { handleError, APIError } from '@planet-sdk/common'; import { Subscription } from '../../../../../src/features/common/types/payments'; import RecurrentPayments from '../../../../../src/features/user/Account/RecurrentPayments'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -117,17 +118,18 @@ function RecurrentDonations({ export default RecurrentDonations; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx b/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx index 9855b64586..c256380fd2 100644 --- a/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx +++ b/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx @@ -1,4 +1,5 @@ import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -195,18 +196,19 @@ const RedeemCode = ({ pageProps: { tenantConfig } }: Props) => { ); }; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - code: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + code: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/profile/register-trees.tsx b/pages/sites/[slug]/[locale]/profile/register-trees.tsx index 987ce912c7..8d7c1d9325 100644 --- a/pages/sites/[slug]/[locale]/profile/register-trees.tsx +++ b/pages/sites/[slug]/[locale]/profile/register-trees.tsx @@ -4,6 +4,7 @@ import UserLayout from '../../../../../src/features/common/Layout/UserLayout/Use import Head from 'next/head'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,17 +51,18 @@ export default function Register({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/treemapper/data-explorer.tsx b/pages/sites/[slug]/[locale]/profile/treemapper/data-explorer.tsx index 169dd321c1..89762e1610 100644 --- a/pages/sites/[slug]/[locale]/profile/treemapper/data-explorer.tsx +++ b/pages/sites/[slug]/[locale]/profile/treemapper/data-explorer.tsx @@ -6,6 +6,7 @@ import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { useRouter } from 'next/router'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -62,17 +63,18 @@ function TreeMapperAnalytics({ export default TreeMapperAnalytics; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/treemapper/import.tsx b/pages/sites/[slug]/[locale]/profile/treemapper/import.tsx index 8461a5fd88..d65c6c9af6 100644 --- a/pages/sites/[slug]/[locale]/profile/treemapper/import.tsx +++ b/pages/sites/[slug]/[locale]/profile/treemapper/import.tsx @@ -6,6 +6,7 @@ import ImportData from '../../../../../../src/features/user/TreeMapper/Import'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,17 +51,18 @@ export default function Import({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/treemapper/index.tsx b/pages/sites/[slug]/[locale]/profile/treemapper/index.tsx index e76bfb8a24..2e494efcf5 100644 --- a/pages/sites/[slug]/[locale]/profile/treemapper/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/treemapper/index.tsx @@ -4,6 +4,7 @@ import UserLayout from '../../../../../../src/features/common/Layout/UserLayout/ import TreeMapper from '../../../../../../src/features/user/TreeMapper'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -51,17 +52,18 @@ function TreeMapperPage({ pageProps: { tenantConfig } }: Props): ReactElement { export default TreeMapperPage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/treemapper/my-species.tsx b/pages/sites/[slug]/[locale]/profile/treemapper/my-species.tsx index abe853a458..95d8e33d56 100644 --- a/pages/sites/[slug]/[locale]/profile/treemapper/my-species.tsx +++ b/pages/sites/[slug]/[locale]/profile/treemapper/my-species.tsx @@ -6,6 +6,7 @@ import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -50,17 +51,18 @@ export default function MySpeciesPage({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/profile/widgets.tsx b/pages/sites/[slug]/[locale]/profile/widgets.tsx index 88e88b72b1..7aaf72df1f 100644 --- a/pages/sites/[slug]/[locale]/profile/widgets.tsx +++ b/pages/sites/[slug]/[locale]/profile/widgets.tsx @@ -6,6 +6,7 @@ import styles from '../../../../../src/features/common/Layout/UserLayout/UserLay import Head from 'next/head'; import { AbstractIntlMessages, useTranslations } from 'next-intl'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -68,17 +69,18 @@ function ProfilePage({ pageProps: { tenantConfig } }: Props): ReactElement { export default ProfilePage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/projects-archive/[p].tsx b/pages/sites/[slug]/[locale]/projects-archive/[p].tsx index bd3d764eca..31d438756f 100644 --- a/pages/sites/[slug]/[locale]/projects-archive/[p].tsx +++ b/pages/sites/[slug]/[locale]/projects-archive/[p].tsx @@ -27,6 +27,7 @@ import { import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { v4 } from 'uuid'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -220,18 +221,19 @@ export default function Donate({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - p: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + p: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/projects-archive/index.tsx b/pages/sites/[slug]/[locale]/projects-archive/index.tsx index 3b939b50a7..bbc7621342 100644 --- a/pages/sites/[slug]/[locale]/projects-archive/index.tsx +++ b/pages/sites/[slug]/[locale]/projects-archive/index.tsx @@ -22,6 +22,7 @@ import { import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { useTenant } from '../../../../../src/features/common/Layout/TenantContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -174,17 +175,18 @@ export default function Donate({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/s/[id].tsx b/pages/sites/[slug]/[locale]/s/[id].tsx index 6ab7f124a1..724e18f381 100644 --- a/pages/sites/[slug]/[locale]/s/[id].tsx +++ b/pages/sites/[slug]/[locale]/s/[id].tsx @@ -3,6 +3,7 @@ import { useRouter } from 'next/router'; import { getRequest } from '../../../../../src/utils/apiRequests/api'; import { ErrorHandlingContext } from '../../../../../src/features/common/Layout/ErrorHandlingContext'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -69,18 +70,19 @@ export default function DirectGift({ return tenantConfig ?
: <>; } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - id: v4(), - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + id: v4(), + locale: 'en', + }, + }; + }) ?? []; return { paths: paths, diff --git a/pages/sites/[slug]/[locale]/t/[profile].tsx b/pages/sites/[slug]/[locale]/t/[profile].tsx index 6392cfbdcc..ab811f5bd6 100644 --- a/pages/sites/[slug]/[locale]/t/[profile].tsx +++ b/pages/sites/[slug]/[locale]/t/[profile].tsx @@ -1,6 +1,7 @@ // This page will be moved to a different place in the future, as it is not a part of the user dashboard import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -83,18 +84,19 @@ const PublicProfilePage = ({ pageProps: { tenantConfig } }: Props) => { export default PublicProfilePage; -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths?.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - profile: v4(), - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + profile: v4(), + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/verify-email.tsx b/pages/sites/[slug]/[locale]/verify-email.tsx index 96ee87c173..a38f075c56 100644 --- a/pages/sites/[slug]/[locale]/verify-email.tsx +++ b/pages/sites/[slug]/[locale]/verify-email.tsx @@ -10,6 +10,7 @@ import { useRouter } from 'next/router'; import { useTenant } from '../../../../src/features/common/Layout/TenantContext'; import { Tenant } from '@planet-sdk/common/build/types/tenant'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -44,17 +45,18 @@ export default function VerifyEmail({ pageProps }: Props): ReactElement { ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { const subDomainPaths = await constructPathsForTenantSlug(); - const paths = subDomainPaths.map((path) => { - return { - params: { - slug: path.params.slug, - locale: 'en', - }, - }; - }); + const paths = + subDomainPaths?.map((path) => { + return { + params: { + slug: path.params.slug, + locale: 'en', + }, + }; + }) ?? []; return { paths, diff --git a/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx b/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx index a733365a23..3c03926f19 100644 --- a/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx +++ b/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx @@ -8,6 +8,7 @@ import { import { AbstractIntlMessages } from 'next-intl'; import { Tenant } from '@planet-sdk/common'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -101,7 +102,7 @@ export default function VTOFitnessChallenge({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { return { paths: [{ params: { slug: 'salesforce', locale: 'en' } }], fallback: 'blocking', diff --git a/pages/sites/[slug]/[locale]/vto-fitness-challenge.tsx b/pages/sites/[slug]/[locale]/vto-fitness-challenge.tsx index 726a541945..953196f176 100644 --- a/pages/sites/[slug]/[locale]/vto-fitness-challenge.tsx +++ b/pages/sites/[slug]/[locale]/vto-fitness-challenge.tsx @@ -8,6 +8,7 @@ import { import { AbstractIntlMessages } from 'next-intl'; import { Tenant } from '@planet-sdk/common'; import { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, @@ -101,7 +102,7 @@ export default function VTOFitnessChallenge({ ); } -export const getStaticPaths = async () => { +export const getStaticPaths: GetStaticPaths = async () => { return { paths: [{ params: { slug: 'salesforce', locale: 'en' } }], fallback: 'blocking', From 698aa6ae00095cd130f7d30639b5a081b0ba0ba9 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Mon, 30 Dec 2024 11:22:37 +0530 Subject: [PATCH 02/14] refactor: update header text in the public profile sdg section. --- public/static/locales/en/profile.json | 3 ++- src/features/common/CarouselSlider/index.tsx | 10 +++++++--- .../user/Profile/InfoAndCTA/SDGCardList/index.tsx | 5 ++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/public/static/locales/en/profile.json b/public/static/locales/en/profile.json index a7033f0fa1..3eabd28583 100644 --- a/public/static/locales/en/profile.json +++ b/public/static/locales/en/profile.json @@ -112,7 +112,8 @@ "m2": "m²" }, "infoAndCtaContainer": { - "sdgCardsSectionHeading": "Restoring Ecosystems & Fighting the Climate Crisis for the SDGs", + "sdgSectionHeadingPrimary": "Restoring Ecosystems & Fighting the Climate Crisis.", + "sdgSectionHeadingSecondary": "Contributing to the Sustainable Development Goals.", "pfpDonorCircleMember": "Plant-for-the-Planet donor circle member", "publicProfileActions": { "becomeAMember": "Become a Member", diff --git a/src/features/common/CarouselSlider/index.tsx b/src/features/common/CarouselSlider/index.tsx index 1b896d145d..56291fb6c3 100644 --- a/src/features/common/CarouselSlider/index.tsx +++ b/src/features/common/CarouselSlider/index.tsx @@ -38,7 +38,10 @@ const CarouselArrow = (props: { }; interface CarouselSliderProps { - carouselTitle: string; + carouselTitles: { + primary: string; + secondary: string; + }; carouselData: ReactElement[]; settings: Settings; currentSlide: number; @@ -47,7 +50,7 @@ interface CarouselSliderProps { } const CarouselSlider = ({ - carouselTitle, + carouselTitles, carouselData, settings, currentSlide, @@ -105,7 +108,8 @@ const CarouselSlider = ({
-

{carouselTitle}

+

{carouselTitles.primary}

+

{carouselTitles.secondary}

{ return ( Date: Thu, 2 Jan 2025 10:40:42 +0530 Subject: [PATCH 03/14] refactor(auth): convert getAuthenticatedRequest to use object parameters --- .../profile/bulk-codes/[method]/[id].tsx | 15 ++++--- .../sites/[slug]/[locale]/profile/history.tsx | 42 ++++++++++--------- .../[slug]/[locale]/profile/projects/[id].tsx | 10 ++--- .../[slug]/[locale]/profile/recurrency.tsx | 10 ++--- .../BulkCodes/components/ProjectSelector.tsx | 14 +++---- src/features/user/ManagePayouts/index.tsx | 10 ++--- .../user/ManageProjects/ProjectsContainer.tsx | 10 ++--- .../components/ProjectCertificates.tsx | 10 ++--- .../components/ProjectMedia.tsx | 10 ++--- .../components/ProjectSites.tsx | 10 ++--- .../components/ProjectSpending.tsx | 10 ++--- src/features/user/ManageProjects/index.tsx | 10 ++--- src/features/user/PlanetCash/index.tsx | 10 ++--- .../user/PlanetCash/screens/Transactions.tsx | 10 ++--- .../RegisterTrees/RegisterTreesWidget.tsx | 10 ++--- .../user/Settings/ApiKey/ApiKeyForm.tsx | 10 ++--- .../EditProfile/AddressManagement/index.tsx | 10 ++--- .../user/TreeMapper/Analytics/index.tsx | 10 ++--- .../Import/components/PlantingLocation.tsx | 20 ++++----- src/features/user/TreeMapper/Import/index.tsx | 10 ++--- .../TreeMapper/MySpecies/MySpeciesForm.tsx | 10 ++--- src/features/user/TreeMapper/index.tsx | 25 +++++------ src/utils/apiRequests/api.ts | 29 ++++++++----- 23 files changed, 159 insertions(+), 156 deletions(-) diff --git a/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx b/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx index 56371bdba7..782b0adb6e 100644 --- a/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx +++ b/pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx @@ -63,17 +63,16 @@ export default function BulkCodeIssueCodesPage({ if (router.isReady) { try { const paymentOptions = - await getAuthenticatedRequest( - pageProps.tenantConfig.id, - `/app/paymentOptions/${router.query.id}`, + await getAuthenticatedRequest({ + 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( diff --git a/pages/sites/[slug]/[locale]/profile/history.tsx b/pages/sites/[slug]/[locale]/profile/history.tsx index d761e2a5e7..1191db344c 100644 --- a/pages/sites/[slug]/[locale]/profile/history.tsx +++ b/pages/sites/[slug]/[locale]/profile/history.tsx @@ -65,16 +65,18 @@ function AccountHistory({ pageProps }: Props): ReactElement { if (next && paymentHistory?._links?.next) { try { const newPaymentHistory = await getAuthenticatedRequest( - 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, @@ -91,12 +93,12 @@ function AccountHistory({ pageProps }: Props): ReactElement { } else { if (filter === null) { try { - const paymentHistory = await getAuthenticatedRequest( - tenantConfig?.id, - '/app/paymentHistory?limit=15', + const paymentHistory = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/paymentHistory?limit=15', token, - logoutUser - ); + logoutUser, + }); setPaymentHistory(paymentHistory); setProgress(100); setIsDataLoading(false); @@ -108,16 +110,16 @@ function AccountHistory({ pageProps }: Props): ReactElement { } } else { try { - const paymentHistory = await getAuthenticatedRequest( - tenantConfig?.id, - `${ + const paymentHistory = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `${ filter && accountingFilters ? accountingFilters[filter] + '&limit=15' : '/app/paymentHistory?limit=15' }`, token, - logoutUser - ); + logoutUser, + }); setPaymentHistory(paymentHistory); setProgress(100); setIsDataLoading(false); diff --git a/pages/sites/[slug]/[locale]/profile/projects/[id].tsx b/pages/sites/[slug]/[locale]/profile/projects/[id].tsx index f464cd340f..19276cbf9d 100644 --- a/pages/sites/[slug]/[locale]/profile/projects/[id].tsx +++ b/pages/sites/[slug]/[locale]/profile/projects/[id].tsx @@ -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) { diff --git a/pages/sites/[slug]/[locale]/profile/recurrency.tsx b/pages/sites/[slug]/[locale]/profile/recurrency.tsx index 417c5e1567..782f99a543 100644 --- a/pages/sites/[slug]/[locale]/profile/recurrency.tsx +++ b/pages/sites/[slug]/[locale]/profile/recurrency.tsx @@ -52,12 +52,12 @@ function RecurrentDonations({ setIsDataLoading(true); setProgress(70); try { - const recurrencies = await getAuthenticatedRequest( - tenantConfig.id, - '/app/subscriptions', + const recurrencies = await getAuthenticatedRequest({ + 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' diff --git a/src/features/user/BulkCodes/components/ProjectSelector.tsx b/src/features/user/BulkCodes/components/ProjectSelector.tsx index 994fee594c..83c6217b2c 100644 --- a/src/features/user/BulkCodes/components/ProjectSelector.tsx +++ b/src/features/user/BulkCodes/components/ProjectSelector.tsx @@ -19,7 +19,6 @@ interface ProjectSelectorProps { active?: boolean; planetCashAccount: PlanetCashAccount | null; } - const ProjectSelector = ({ projectList, project, @@ -32,17 +31,16 @@ const ProjectSelector = ({ const { user, token, logoutUser, contextLoaded } = useUserProps(); const fetchPaymentOptions = async (guid: string) => { - const paymentOptions = await getAuthenticatedRequest( - `${tenantConfig?.id}`, - `/app/paymentOptions/${guid}`, + const paymentOptions = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/paymentOptions/${guid}`, token, logoutUser, - undefined, - { + queryParams: { country: planetCashAccount?.country || '', ...(user !== null && { legacyPriceFor: user.id }), - } - ); + }, + }); return paymentOptions; }; diff --git a/src/features/user/ManagePayouts/index.tsx b/src/features/user/ManagePayouts/index.tsx index d39670d426..3f52c45789 100644 --- a/src/features/user/ManagePayouts/index.tsx +++ b/src/features/user/ManagePayouts/index.tsx @@ -76,12 +76,12 @@ export default function ManagePayouts({ setIsDataLoading(true); setProgress && setProgress(70); try { - const res = await getAuthenticatedRequest( - tenantConfig?.id, - `/app/accounts`, + const res = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/accounts`, token, - logoutUser - ); + logoutUser, + }); setAccounts(res); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/ManageProjects/ProjectsContainer.tsx b/src/features/user/ManageProjects/ProjectsContainer.tsx index e1cc8207d4..9e47e105eb 100644 --- a/src/features/user/ManageProjects/ProjectsContainer.tsx +++ b/src/features/user/ManageProjects/ProjectsContainer.tsx @@ -112,12 +112,12 @@ export default function ProjectsContainer() { async function loadProjects() { if (user) { try { - const projects = await getAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/projects?version=1.2', + const projects = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/projects?version=1.2', token, - logoutUser - ); + logoutUser, + }); setProjects(projects); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/ManageProjects/components/ProjectCertificates.tsx b/src/features/user/ManageProjects/components/ProjectCertificates.tsx index 6405ecd85f..586a17199e 100644 --- a/src/features/user/ManageProjects/components/ProjectCertificates.tsx +++ b/src/features/user/ManageProjects/components/ProjectCertificates.tsx @@ -133,12 +133,12 @@ function ProjectCertificates({ const fetchCertificates = async () => { try { - const result = await getAuthenticatedRequest( - tenantConfig?.id, - `/app/profile/projects/${projectGUID}?_scope=certificates`, + const result = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile/projects/${projectGUID}?_scope=certificates`, token, - logoutUser - ); + logoutUser, + }); setShowForm(false); setShowToggle(false); setUploadedFiles(result.certificates); diff --git a/src/features/user/ManageProjects/components/ProjectMedia.tsx b/src/features/user/ManageProjects/components/ProjectMedia.tsx index fa3e1bd50e..11fbf17df9 100644 --- a/src/features/user/ManageProjects/components/ProjectMedia.tsx +++ b/src/features/user/ManageProjects/components/ProjectMedia.tsx @@ -67,12 +67,12 @@ export default function ProjectMedia({ try { // Fetch images of the project if (projectGUID && token) { - const result = await getAuthenticatedRequest( - tenantConfig?.id, - `/app/profile/projects/${projectGUID}?_scope=images`, + const result = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile/projects/${projectGUID}?_scope=images`, token, - logoutUser - ); + logoutUser, + }); setUploadedImages(result.images); } } catch (err) { diff --git a/src/features/user/ManageProjects/components/ProjectSites.tsx b/src/features/user/ManageProjects/components/ProjectSites.tsx index 2f6d070306..206e6d98f3 100644 --- a/src/features/user/ManageProjects/components/ProjectSites.tsx +++ b/src/features/user/ManageProjects/components/ProjectSites.tsx @@ -335,12 +335,12 @@ export default function ProjectSites({ try { if (projectGUID) { // Fetch sites of the project - const result = await getAuthenticatedRequest( - tenantConfig?.id, - `/app/profile/projects/${projectGUID}?_scope=sites`, + const result = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile/projects/${projectGUID}?_scope=sites`, token, - logoutUser - ); + logoutUser, + }); const geoLocation = { geoLatitude: result.geoLatitude, geoLongitude: result.geoLongitude, diff --git a/src/features/user/ManageProjects/components/ProjectSpending.tsx b/src/features/user/ManageProjects/components/ProjectSpending.tsx index a9fcd0499b..b2a0fc4dae 100644 --- a/src/features/user/ManageProjects/components/ProjectSpending.tsx +++ b/src/features/user/ManageProjects/components/ProjectSpending.tsx @@ -167,12 +167,12 @@ export default function ProjectSpending({ try { // Fetch spending of the project if (projectGUID && token) { - const result = await getAuthenticatedRequest( - tenantConfig?.id, - `/app/profile/projects/${projectGUID}?_scope=expenses`, + const result = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile/projects/${projectGUID}?_scope=expenses`, token, - logoutUser - ); + logoutUser, + }); if (result?.expenses && result.expenses.length > 0) { setShowForm(false); } diff --git a/src/features/user/ManageProjects/index.tsx b/src/features/user/ManageProjects/index.tsx index ad5b4ff677..53ddac4bb4 100644 --- a/src/features/user/ManageProjects/index.tsx +++ b/src/features/user/ManageProjects/index.tsx @@ -150,12 +150,12 @@ export default function ManageProjects({ try { const res = await getAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >( - tenantConfig?.id, - `/app/profile/projects/${projectGUID}`, + >({ + tenant: tenantConfig?.id, + url: `/app/profile/projects/${projectGUID}`, token, - logoutUser - ); + logoutUser, + }); setProjectDetails(res); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/PlanetCash/index.tsx b/src/features/user/PlanetCash/index.tsx index 8b956b4b6f..6b3f1032e2 100644 --- a/src/features/user/PlanetCash/index.tsx +++ b/src/features/user/PlanetCash/index.tsx @@ -86,12 +86,12 @@ export default function PlanetCash({ try { setIsDataLoading(true); setProgress && setProgress(70); - const accounts = await getAuthenticatedRequest( - tenantConfig?.id, - `/app/planetCash`, + const accounts = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/planetCash`, token, - logoutUser - ); + logoutUser, + }); redirectIfNeeded(accounts); const sortedAccounts = sortAccountsByActive(accounts); setIsPlanetCashActive(accounts.some((account) => account.isActive)); diff --git a/src/features/user/PlanetCash/screens/Transactions.tsx b/src/features/user/PlanetCash/screens/Transactions.tsx index a76bb17f56..ebf56e870c 100644 --- a/src/features/user/PlanetCash/screens/Transactions.tsx +++ b/src/features/user/PlanetCash/screens/Transactions.tsx @@ -63,12 +63,12 @@ const Transactions = ({ : `/app/paymentHistory?filter=planet-cash&limit=15`; const newTransactionHistory = - await getAuthenticatedRequest( - tenantConfig?.id, - apiUrl, + await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: apiUrl, token, - logoutUser - ); + logoutUser, + }); if (transactionHistory) { setTransactionHistory({ diff --git a/src/features/user/RegisterTrees/RegisterTreesWidget.tsx b/src/features/user/RegisterTrees/RegisterTreesWidget.tsx index 453deb118b..dececc1e26 100644 --- a/src/features/user/RegisterTrees/RegisterTreesWidget.tsx +++ b/src/features/user/RegisterTrees/RegisterTreesWidget.tsx @@ -216,12 +216,12 @@ function RegisterTreesForm({ }; async function loadProjects() { try { - const projects = await getAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/projects', + const projects = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/projects', token, - logoutUser - ); + logoutUser, + }); setProjects(projects); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/Settings/ApiKey/ApiKeyForm.tsx b/src/features/user/Settings/ApiKey/ApiKeyForm.tsx index 8886884903..5df65ccfec 100644 --- a/src/features/user/Settings/ApiKey/ApiKeyForm.tsx +++ b/src/features/user/Settings/ApiKey/ApiKeyForm.tsx @@ -48,12 +48,12 @@ export default function ApiKey() { const getApiKey = async () => { setIsUploadingData(true); try { - const res = await getAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/apiKey', + const res = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/apiKey', token, - logoutUser - ); + logoutUser, + }); if (res) { setApiKey(res.apiKey || ''); } diff --git a/src/features/user/Settings/EditProfile/AddressManagement/index.tsx b/src/features/user/Settings/EditProfile/AddressManagement/index.tsx index 6cbc44f0be..d767d4b9cc 100644 --- a/src/features/user/Settings/EditProfile/AddressManagement/index.tsx +++ b/src/features/user/Settings/EditProfile/AddressManagement/index.tsx @@ -52,12 +52,12 @@ const AddressManagement = () => { const updateUserAddresses = useCallback(async () => { if (!user || !token || !contextLoaded) return; try { - const res = await getAuthenticatedRequest( - tenantConfig.id, - '/app/addresses', + const res = await getAuthenticatedRequest({ + tenant: tenantConfig.id, + url: '/app/addresses', token, - logoutUser - ); + logoutUser, + }); if (res) setUserAddresses(res); } catch (error) { setErrors(handleError(error as APIError)); diff --git a/src/features/user/TreeMapper/Analytics/index.tsx b/src/features/user/TreeMapper/Analytics/index.tsx index aea8d0cf2c..23bbace1f1 100644 --- a/src/features/user/TreeMapper/Analytics/index.tsx +++ b/src/features/user/TreeMapper/Analytics/index.tsx @@ -23,12 +23,12 @@ const Analytics = () => { const fetchProjects = async () => { try { // TODO - update project type, this does not match completely - const res = await getAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/projects?scope=map', + const res = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/projects?scope=map', token, - logoutUser - ); + logoutUser, + }); const projects: Project[] = []; res.forEach((_proj) => { diff --git a/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx b/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx index 283103cba7..c066420306 100644 --- a/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx +++ b/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx @@ -213,12 +213,12 @@ export default function PlantingLocation({ const loadProjects = async () => { try { - const projects = await getAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/projects', + const projects = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/projects', token, - logoutUser - ); + logoutUser, + }); setProjects(projects); } catch (err) { setErrors(handleError(err as APIError)); @@ -227,12 +227,12 @@ export default function PlantingLocation({ const loadMySpecies = async () => { try { - const species = await getAuthenticatedRequest( - tenantConfig?.id, - '/treemapper/species', + const species = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/treemapper/species', token, - logoutUser - ); + logoutUser, + }); setMySpecies(species); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/TreeMapper/Import/index.tsx b/src/features/user/TreeMapper/Import/index.tsx index 5673981169..eb08b55d08 100644 --- a/src/features/user/TreeMapper/Import/index.tsx +++ b/src/features/user/TreeMapper/Import/index.tsx @@ -65,12 +65,12 @@ export default function ImportData(): ReactElement { const fetchPlantLocation = async (id: string) => { try { - const result = await getAuthenticatedRequest( - tenantConfig?.id, - `/treemapper/interventions/${id}?_scope=extended`, + const result = await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/treemapper/interventions/${id}?_scope=extended`, token, - logoutUser - ); + logoutUser, + }); setPlantLocation(result); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx b/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx index 742022b191..afd57b6033 100644 --- a/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx +++ b/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx @@ -51,12 +51,12 @@ export default function MySpeciesForm() { const fetchMySpecies = async () => { try { - const result = await getAuthenticatedRequest( - tenantConfig.id, - '/treemapper/species', + const result = await getAuthenticatedRequest({ + tenant: tenantConfig.id, + url: '/treemapper/species', token, - logoutUser - ); + logoutUser, + }); setSpecies(result); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/TreeMapper/index.tsx b/src/features/user/TreeMapper/index.tsx index df5f5592cd..74e0e66e8f 100644 --- a/src/features/user/TreeMapper/index.tsx +++ b/src/features/user/TreeMapper/index.tsx @@ -46,15 +46,13 @@ function TreeMapper(): ReactElement { if (next && links?.next) { try { const response = - await getAuthenticatedRequest( - tenantConfig?.id, - links.next, + await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: links.next, token, logoutUser, - {}, - undefined, - '1.0.4' - ); + version: '1.0.4', + }); if (response?.items) { const newPlantLocations = response.items; for (const itr in newPlantLocations) { @@ -92,16 +90,13 @@ function TreeMapper(): ReactElement { } else { try { const response = - await getAuthenticatedRequest( - tenantConfig?.id, - '/treemapper/interventions?_scope=extended&limit=15', + await getAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/treemapper/interventions?_scope=extended&limit=15', token, logoutUser, - - {}, - undefined, - '1.0.4' - ); + version: '1.0.4', + }); if (response?.items) { const plantLocations = response.items; if (plantLocations?.length === 0) { diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 13c9b04be6..9e137ece82 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -7,6 +7,16 @@ import { setHeaderForImpersonation } from './setHeader'; const INVALID_TOKEN_STATUS_CODE = 498; +export interface GetAuthRequestOptions { + tenant: string | undefined; + url: string; + token: string | null; + logoutUser: (value?: string | undefined) => void; + header?: Record | null; + queryParams?: { [key: string]: string }; + version?: string; +} + // API call to private /profile endpoint export async function getAccountInfo( tenant: string | undefined, @@ -75,16 +85,15 @@ export function getRequest( })(); }); } - -export function getAuthenticatedRequest( - tenant: string | undefined, - url: string, - token: string | null, - logoutUser: (value?: string | undefined) => void, - header: Record | null = null, - queryParams?: { [key: string]: string }, - version?: string -) { +export function getAuthenticatedRequest({ + tenant, + url, + token, + logoutUser, + header = null, + queryParams = {}, + version = '1.0.3', +}: GetAuthRequestOptions): Promise { const lang = localStorage.getItem('language') || 'en'; const query = { ...queryParams }; const queryString = getQueryString(query); From 9efad1549ed92f64de6dd74164f5c39cde3ab7ee Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:04:50 +0530 Subject: [PATCH 04/14] refactor: refactor getRequest to use object parameters --- pages/sites/[slug]/[locale]/all.tsx | 37 +++++++++++-------- pages/sites/[slug]/[locale]/home.tsx | 16 ++++---- .../[slug]/[locale]/projects-archive/[p].tsx | 12 +++--- .../[locale]/projects-archive/index.tsx | 12 +++--- pages/sites/[slug]/[locale]/s/[id].tsx | 8 ++-- pages/sites/[slug]/[locale]/t/[profile].tsx | 8 ++-- .../common/Layout/CurrencyContext.tsx | 7 ++-- src/features/projects/screens/Projects.tsx | 8 ++-- .../projectsV2/ProjectDetails/index.tsx | 24 ++++++------ src/features/projectsV2/ProjectsContext.tsx | 12 +++--- .../user/Account/components/DownloadCodes.tsx | 2 +- src/features/user/BulkCodes/index.tsx | 8 ++-- src/features/user/ManagePayouts/index.tsx | 8 ++-- .../user/Profile/TpoProjects/index.tsx | 12 +++--- .../user/Widget/DonationLink/index.tsx | 12 +++--- .../Mangroves/components/ProjectGrid.tsx | 12 +++--- .../components/ContentSection.tsx | 10 +++-- .../VTOCampaign/components/ProjectGrid.tsx | 12 ++++-- .../components/ProjectGrid.tsx | 12 ++++-- src/utils/apiRequests/api.ts | 20 ++++++---- src/utils/maps/plantLocations.ts | 10 ++--- 21 files changed, 142 insertions(+), 120 deletions(-) diff --git a/pages/sites/[slug]/[locale]/all.tsx b/pages/sites/[slug]/[locale]/all.tsx index 7d28be43ca..216931c958 100644 --- a/pages/sites/[slug]/[locale]/all.tsx +++ b/pages/sites/[slug]/[locale]/all.tsx @@ -47,10 +47,10 @@ export default function Home({ pageProps }: Props) { React.useEffect(() => { async function loadLeaderboard() { try { - const newLeaderboard = await getRequest( - pageProps.tenantConfig.id, - `/app/leaderboard/${pageProps.tenantConfig.id}` - ); + const newLeaderboard = await getRequest({ + tenant: pageProps.tenantConfig.id, + url: `/app/leaderboard/${pageProps.tenantConfig.id}`, + }); setLeaderboard(newLeaderboard); } catch (err) { setErrors(handleError(err as APIError)); @@ -66,10 +66,10 @@ export default function Home({ pageProps }: Props) { React.useEffect(() => { async function loadTenantScore() { try { - const newTenantScore = await getRequest( - pageProps.tenantConfig.id, - `/app/tenantScore/${pageProps.tenantConfig.id}` - ); + const newTenantScore = await getRequest({ + tenant: pageProps.tenantConfig.id, + url: `/app/tenantScore/${pageProps.tenantConfig.id}`, + }); setTenantScore(newTenantScore); } catch (err) { setErrors(handleError(err as APIError)); @@ -78,7 +78,6 @@ export default function Home({ pageProps }: Props) { loadTenantScore(); }, []); - const [treesDonated, setTreesDonated] = React.useState( null ); @@ -86,10 +85,10 @@ export default function Home({ pageProps }: Props) { React.useEffect(() => { async function loadTreesDonated() { try { - const newTreesDonated = await getRequest( - pageProps.tenantConfig.id, - `${process.env.WEBHOOK_URL}/platform/total-tree-count` - ); + const newTreesDonated = await getRequest({ + tenant: pageProps.tenantConfig.id, + url: `${process.env.WEBHOOK_URL}/platform/total-tree-count`, + }); setTreesDonated(newTreesDonated); } catch (err) { setErrors(handleError(err as APIError)); @@ -103,12 +102,20 @@ export default function Home({ pageProps }: Props) { switch (pageProps.tenantConfig.config.slug) { case 'planet': AllPage = ( - + ); return AllPage; case 'ttc': AllPage = ( - + ); return AllPage; default: diff --git a/pages/sites/[slug]/[locale]/home.tsx b/pages/sites/[slug]/[locale]/home.tsx index 57c52209cb..dcc8fe1368 100644 --- a/pages/sites/[slug]/[locale]/home.tsx +++ b/pages/sites/[slug]/[locale]/home.tsx @@ -52,10 +52,10 @@ export default function Home({ pageProps }: Props) { React.useEffect(() => { async function loadTenantScore() { try { - const newTenantScore = await getRequest( - pageProps.tenantConfig.id, - `/app/tenantScore` - ); + const newTenantScore = await getRequest({ + tenant: pageProps.tenantConfig.id, + url: `/app/tenantScore`, + }); setTenantScore(newTenantScore); } catch (err) { setErrors(handleError(err as APIError)); @@ -67,10 +67,10 @@ export default function Home({ pageProps }: Props) { React.useEffect(() => { async function loadLeaderboard() { try { - const newLeaderBoard = await getRequest( - pageProps.tenantConfig.id, - `/app/leaderboard` - ); + const newLeaderBoard = await getRequest({ + tenant: pageProps.tenantConfig.id, + url: `/app/leaderboard`, + }); setLeaderboard(newLeaderBoard); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/pages/sites/[slug]/[locale]/projects-archive/[p].tsx b/pages/sites/[slug]/[locale]/projects-archive/[p].tsx index bd3d764eca..1f13da6958 100644 --- a/pages/sites/[slug]/[locale]/projects-archive/[p].tsx +++ b/pages/sites/[slug]/[locale]/projects-archive/[p].tsx @@ -93,15 +93,15 @@ export default function Donate({ setCurrencyCode(currency); try { const { p } = router.query; - const project = await getRequest( - pageProps.tenantConfig.id, - encodeURI(`/app/projects/${p}`), - { + const project = await getRequest({ + tenant: pageProps.tenantConfig.id, + url: encodeURI(`/app/projects/${p}`), + queryParams: { _scope: 'extended', currency: currency || '', locale: locale, - } - ); + }, + }); if ( project.purpose === 'conservation' || project.purpose === 'trees' diff --git a/pages/sites/[slug]/[locale]/projects-archive/index.tsx b/pages/sites/[slug]/[locale]/projects-archive/index.tsx index 3b939b50a7..6f904f2969 100644 --- a/pages/sites/[slug]/[locale]/projects-archive/index.tsx +++ b/pages/sites/[slug]/[locale]/projects-archive/index.tsx @@ -113,17 +113,17 @@ export default function Donate({ setCurrencyCode(currency); setInternalLanguage(locale); try { - const projects = await getRequest( - pageProps.tenantConfig.id, - `/app/projects`, - { + const projects = await getRequest({ + 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); diff --git a/pages/sites/[slug]/[locale]/s/[id].tsx b/pages/sites/[slug]/[locale]/s/[id].tsx index 6ab7f124a1..2888b9d1ec 100644 --- a/pages/sites/[slug]/[locale]/s/[id].tsx +++ b/pages/sites/[slug]/[locale]/s/[id].tsx @@ -38,10 +38,10 @@ export default function DirectGift({ async function loadPublicUserData() { try { - const newProfile = await getRequest( - tenantConfig.id, - `/app/profiles/${router.query.id}` - ); + const newProfile = await getRequest({ + tenant: tenantConfig.id, + url: `/app/profiles/${router.query.id}`, + }); if (newProfile.type !== 'tpo') { localStorage.setItem( 'directGift', diff --git a/pages/sites/[slug]/[locale]/t/[profile].tsx b/pages/sites/[slug]/[locale]/t/[profile].tsx index 6392cfbdcc..a52a061ae9 100644 --- a/pages/sites/[slug]/[locale]/t/[profile].tsx +++ b/pages/sites/[slug]/[locale]/t/[profile].tsx @@ -43,10 +43,10 @@ const PublicProfilePage = ({ pageProps: { tenantConfig } }: Props) => { async function loadPublicProfile(slug: string) { try { - const profileData = await getRequest( - tenantConfig.id, - `/app/profiles/${slug}` - ); + const profileData = await getRequest({ + tenant: tenantConfig.id, + url: `/app/profiles/${slug}`, + }); setProfile(profileData); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/common/Layout/CurrencyContext.tsx b/src/features/common/Layout/CurrencyContext.tsx index 055952a07c..b203d1b2fd 100644 --- a/src/features/common/Layout/CurrencyContext.tsx +++ b/src/features/common/Layout/CurrencyContext.tsx @@ -24,10 +24,9 @@ export const CurrencyProvider: FC = ({ children }) => { const fetchCurrencies = async () => { try { - const currencyData = await getRequest( - undefined, - '/app/currencies' - ); + const currencyData = await getRequest({ + url: '/app/currencies', + }); setFetchCount(fetchCount + 1); setSupportedCurrencies( new Set(Object.keys(currencyData) as CurrencyCode[]) diff --git a/src/features/projects/screens/Projects.tsx b/src/features/projects/screens/Projects.tsx index dfa2d0aff4..65b1a5f004 100644 --- a/src/features/projects/screens/Projects.tsx +++ b/src/features/projects/screens/Projects.tsx @@ -172,10 +172,10 @@ function ProjectsList({ React.useEffect(() => { async function setListOrder() { try { - const res = await getRequest( - tenantConfig.id, - `/app/tenants/${tenantConfig.id}` - ); + const res = await getRequest({ + tenant: tenantConfig.id, + url: `/app/tenants/${tenantConfig.id}`, + }); setShouldSortProjectList(res.topProjectsOnly); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/projectsV2/ProjectDetails/index.tsx b/src/features/projectsV2/ProjectDetails/index.tsx index d373a71d62..ba77f307cf 100644 --- a/src/features/projectsV2/ProjectDetails/index.tsx +++ b/src/features/projectsV2/ProjectDetails/index.tsx @@ -60,15 +60,15 @@ const ProjectDetails = ({ setIsLoading(true); setIsError(false); try { - const fetchedProject = await getRequest( - tenantConfig.id, - `/app/projects/${projectSlug}`, - { + const fetchedProject = await getRequest({ + tenant: tenantConfig.id, + url: `/app/projects/${projectSlug}`, + queryParams: { _scope: 'extended', currency: currency, locale: locale, - } - ); + }, + }); const { purpose } = fetchedProject; if (purpose === 'conservation' || purpose === 'trees') { setSingleProject(fetchedProject); @@ -95,14 +95,14 @@ const ProjectDetails = ({ async function loadPlantLocations() { setIsLoading(true); try { - const result = await getRequest( - tenantConfig.id, - `/app/plantLocations/${singleProject?.id}`, - { + const result = await getRequest({ + tenant: tenantConfig.id, + url: `/app/plantLocations/${singleProject?.id}`, + queryParams: { _scope: 'extended', }, - '1.0.4' - ); + version: '1.0.4', + }); setPlantLocations(result); } catch (err) { setErrors(handleError(err as APIError | ClientError)); diff --git a/src/features/projectsV2/ProjectsContext.tsx b/src/features/projectsV2/ProjectsContext.tsx index 3bdcc675ae..73936c827b 100644 --- a/src/features/projectsV2/ProjectsContext.tsx +++ b/src/features/projectsV2/ProjectsContext.tsx @@ -195,17 +195,17 @@ export const ProjectsProvider: FC = ({ setIsLoading(true); setIsError(false); try { - const fetchedProjects = await getRequest( - tenantConfig.id, - `/app/projects`, - { + const fetchedProjects = await getRequest({ + tenant: tenantConfig.id, + url: `/app/projects`, + queryParams: { _scope: 'map', currency: currencyCode, tenant: tenantConfig.id, 'filter[purpose]': 'trees,conservation', locale: locale, - } - ); + }, + }); setProjects(fetchedProjects); setProjectsLocale(locale); } catch (err) { diff --git a/src/features/user/Account/components/DownloadCodes.tsx b/src/features/user/Account/components/DownloadCodes.tsx index b269834bdc..c424c65bcf 100644 --- a/src/features/user/Account/components/DownloadCodes.tsx +++ b/src/features/user/Account/components/DownloadCodes.tsx @@ -38,7 +38,7 @@ const DownloadCodes = ({ codesUrl }: DownloadCodesProps): ReactElement => { type: string; numberOfItems: number; items: []; - }>(tenantConfig?.id, codesUrl); + }>({ tenant: tenantConfig?.id, url: codesUrl }); if (response) { if (response.items.length) { downloadCSV(response.items, 'codes.csv'); diff --git a/src/features/user/BulkCodes/index.tsx b/src/features/user/BulkCodes/index.tsx index 9f6cddba66..3e675f4c67 100644 --- a/src/features/user/BulkCodes/index.tsx +++ b/src/features/user/BulkCodes/index.tsx @@ -72,10 +72,10 @@ export default function BulkCodes({ const fetchProjectList = useCallback(async () => { if (planetCashAccount && !projectList) { try { - const fetchedProjects = await getRequest( - `${tenantConfig?.id}`, - `/app/countryProjects/${planetCashAccount.country}` - ); + const fetchedProjects = await getRequest({ + tenant: tenantConfig?.id, + url: `/app/countryProjects/${planetCashAccount.country}`, + }); // map fetchedProjects to desired form and setProject if ( diff --git a/src/features/user/ManagePayouts/index.tsx b/src/features/user/ManagePayouts/index.tsx index 3f52c45789..0b027c8185 100644 --- a/src/features/user/ManagePayouts/index.tsx +++ b/src/features/user/ManagePayouts/index.tsx @@ -56,10 +56,10 @@ export default function ManagePayouts({ const fetchPayoutMinAmounts = useCallback(async () => { if (!payoutMinAmounts) { try { - const res = await getRequest( - tenantConfig?.id, - '/app/payoutMinAmounts' - ); + const res = await getRequest({ + tenant: tenantConfig?.id, + url: '/app/payoutMinAmounts', + }); setPayoutMinAmounts(res); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/Profile/TpoProjects/index.tsx b/src/features/user/Profile/TpoProjects/index.tsx index 05774bccd2..1f101c63e4 100644 --- a/src/features/user/Profile/TpoProjects/index.tsx +++ b/src/features/user/Profile/TpoProjects/index.tsx @@ -31,13 +31,13 @@ export default function ProjectsContainer({ profile }: Props) { async function loadProjects() { try { - const projects = await getRequest( - `${tenantConfig?.id}`, - `/app/profiles/${profile.id}/projects`, - { + const projects = await getRequest({ + tenant: tenantConfig?.id, + url: `/app/profiles/${profile.id}/projects`, + queryParams: { locale: locale, - } - ); + }, + }); setProjects(projects); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/features/user/Widget/DonationLink/index.tsx b/src/features/user/Widget/DonationLink/index.tsx index b77979f08b..586ada5d2a 100644 --- a/src/features/user/Widget/DonationLink/index.tsx +++ b/src/features/user/Widget/DonationLink/index.tsx @@ -20,16 +20,16 @@ export default function DonationLink(): ReactElement | null { async function fetchProjectList() { try { - const projectsList = await getRequest( - tenantConfig?.id, - `/app/projects`, - { + const projectsList = await getRequest({ + tenant: tenantConfig?.id, + url: `/app/projects`, + queryParams: { _scope: 'map', 'filter[purpose]': 'trees,restoration', tenant: tenantConfig?.id, locale: locale, - } - ); + }, + }); if ( projectsList && Array.isArray(projectsList) && diff --git a/src/tenants/salesforce/Mangroves/components/ProjectGrid.tsx b/src/tenants/salesforce/Mangroves/components/ProjectGrid.tsx index 84a91a9ba3..acf528e641 100644 --- a/src/tenants/salesforce/Mangroves/components/ProjectGrid.tsx +++ b/src/tenants/salesforce/Mangroves/components/ProjectGrid.tsx @@ -33,16 +33,16 @@ export default function ProjectGrid() { async function loadProjects() { const currencyCode = getStoredCurrency(); try { - const projects = await getRequest( - tenantConfig.id, - `/app/projects`, - { + const projects = await getRequest({ + tenant: tenantConfig.id, + url: `/app/projects`, + queryParams: { _scope: 'map', currency: currencyCode, tenant: tenantConfig.id, 'filter[purpose]': 'trees,conservation', - } - ); + }, + }); setProjects(projects); setIsLoaded(true); } catch (err) { diff --git a/src/tenants/salesforce/OceanforceCampaign/components/ContentSection.tsx b/src/tenants/salesforce/OceanforceCampaign/components/ContentSection.tsx index e3f14a04d7..018f184a27 100644 --- a/src/tenants/salesforce/OceanforceCampaign/components/ContentSection.tsx +++ b/src/tenants/salesforce/OceanforceCampaign/components/ContentSection.tsx @@ -27,9 +27,13 @@ export default function ContentSection() { try { const project = await getRequest< TreeProjectExtended | ConservationProjectExtended - >(tenantConfig.id, `/app/projects/${projectSlug}`, { - _scope: 'extended', - currency: currencyCode, + >({ + tenant: tenantConfig.id, + url: `/app/projects/${projectSlug}`, + queryParams: { + _scope: 'extended', + currency: currencyCode, + }, }); setProject(project); } catch (err) { diff --git a/src/tenants/salesforce/VTOCampaign/components/ProjectGrid.tsx b/src/tenants/salesforce/VTOCampaign/components/ProjectGrid.tsx index 1e50844bbc..0bc43c22f7 100644 --- a/src/tenants/salesforce/VTOCampaign/components/ProjectGrid.tsx +++ b/src/tenants/salesforce/VTOCampaign/components/ProjectGrid.tsx @@ -19,11 +19,15 @@ export default function ProjectGrid() { async function loadProjects() { const currencyCode = getStoredCurrency(); try { - const projects = await getRequest(tenantConfig.id, `/app/projects`, { - _scope: 'map', - currency: currencyCode, + const projects = await getRequest({ tenant: tenantConfig.id, - 'filter[purpose]': 'trees,conservation', + url: `/app/projects`, + queryParams: { + _scope: 'map', + currency: currencyCode, + tenant: tenantConfig.id, + 'filter[purpose]': 'trees,conservation', + }, }); setProjects(projects as MapProject[]); } catch (err) { diff --git a/src/tenants/salesforce/VTOCampaign2023/components/ProjectGrid.tsx b/src/tenants/salesforce/VTOCampaign2023/components/ProjectGrid.tsx index b2c384671f..1e93c5a642 100644 --- a/src/tenants/salesforce/VTOCampaign2023/components/ProjectGrid.tsx +++ b/src/tenants/salesforce/VTOCampaign2023/components/ProjectGrid.tsx @@ -19,11 +19,15 @@ export default function ProjectGrid() { async function loadProjects() { const currencyCode = getStoredCurrency(); try { - const projects = await getRequest(tenantConfig.id, `/app/projects`, { - _scope: 'map', - currency: currencyCode, + const projects = await getRequest({ tenant: tenantConfig.id, - 'filter[purpose]': 'trees,conservation', + url: `/app/projects`, + queryParams: { + _scope: 'map', + currency: currencyCode, + tenant: tenantConfig.id, + 'filter[purpose]': 'trees,conservation', + }, }); setProjects(projects as MapProject[]); } catch (err) { diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 9e137ece82..205a498474 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -7,8 +7,8 @@ import { setHeaderForImpersonation } from './setHeader'; const INVALID_TOKEN_STATUS_CODE = 498; -export interface GetAuthRequestOptions { - tenant: string | undefined; +interface GetAuthRequestOptions { + tenant?: string | undefined; url: string; token: string | null; logoutUser: (value?: string | undefined) => void; @@ -17,6 +17,10 @@ export interface GetAuthRequestOptions { version?: string; } +type GetRequestOptions = Omit< + GetAuthRequestOptions, + 'header' | 'logoutUser' | 'token' +>; // API call to private /profile endpoint export async function getAccountInfo( tenant: string | undefined, @@ -45,12 +49,12 @@ function isAbsoluteUrl(url: string) { return pattern.test(url); } -export function getRequest( - tenant: string | undefined, - url: string, - queryParams?: { [key: string]: string }, - version?: string -) { +export function getRequest({ + tenant, + url, + queryParams = {}, + version, +}: GetRequestOptions): Promise { const lang = localStorage.getItem('language') || 'en'; const query = { ...queryParams }; const queryString = getQueryString(query); diff --git a/src/utils/maps/plantLocations.ts b/src/utils/maps/plantLocations.ts index 50d28f639a..a1f4538d16 100644 --- a/src/utils/maps/plantLocations.ts +++ b/src/utils/maps/plantLocations.ts @@ -68,14 +68,14 @@ export async function getAllPlantLocations( redirect: (url: string) => void ): Promise { try { - const result = await getRequest( + const result = await getRequest({ tenant, - `/app/plantLocations/${project}`, - { + url: `/app/plantLocations/${project}`, + queryParams: { _scope: 'extended', }, - '1.0.4' - ); + version: '1.0.4', + }); if (result) { return result; } else { From c937405ef276bd142fddaeb7a6557d9e39ab9cea Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:18:11 +0530 Subject: [PATCH 05/14] refactor: refactor getAccountInfo to use object param --- .../common/Layout/UserPropsContext.tsx | 2 +- .../ImpersonateUser/ImpersonateUserForm.tsx | 6 +++++- src/utils/apiRequests/api.ts | 19 +++++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/features/common/Layout/UserPropsContext.tsx b/src/features/common/Layout/UserPropsContext.tsx index 8615bb7578..815d1ca656 100644 --- a/src/features/common/Layout/UserPropsContext.tsx +++ b/src/features/common/Layout/UserPropsContext.tsx @@ -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); diff --git a/src/features/user/Settings/ImpersonateUser/ImpersonateUserForm.tsx b/src/features/user/Settings/ImpersonateUser/ImpersonateUserForm.tsx index 2357bca024..47d7eb79b0 100644 --- a/src/features/user/Settings/ImpersonateUser/ImpersonateUserForm.tsx +++ b/src/features/user/Settings/ImpersonateUser/ImpersonateUserForm.tsx @@ -72,7 +72,11 @@ const ImpersonateUserForm = (): ReactElement => { if (data.targetEmail && data.supportPin) { setIsProcessing(true); try { - const res = await getAccountInfo(tenantConfig?.id, token, data); + const res = await getAccountInfo({ + tenant: tenantConfig?.id, + token, + impersonationData: data, + }); const resJson = await res.json(); if (res.status === 200) { setIsInvalidEmail(false); diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 205a498474..311f3e273b 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -1,12 +1,19 @@ +import type { ImpersonationData } from '../../features/user/Settings/ImpersonateUser/ImpersonateUserForm'; + import { getQueryString } from './getQueryString'; import getsessionId from './getSessionId'; import { APIError, ClientError } from '@planet-sdk/common'; import { validateToken } from './validateToken'; -import { ImpersonationData } from '../../features/user/Settings/ImpersonateUser/ImpersonateUserForm'; import { setHeaderForImpersonation } from './setHeader'; const INVALID_TOKEN_STATUS_CODE = 498; +interface GetAccountInfo { + tenant: string | undefined; + token: string | null; + impersonationData?: ImpersonationData; +} + interface GetAuthRequestOptions { tenant?: string | undefined; url: string; @@ -22,11 +29,11 @@ type GetRequestOptions = Omit< 'header' | 'logoutUser' | 'token' >; // API call to private /profile endpoint -export async function getAccountInfo( - tenant: string | undefined, - token: string | null, - impersonationData?: ImpersonationData -): Promise { +export async function getAccountInfo({ + tenant, + token, + impersonationData, +}: GetAccountInfo): Promise { const lang = localStorage.getItem('language') || 'en'; const header = { 'tenant-key': `${tenant}`, From b5798a11fe793678064cf050a82ca50bcf127516 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:02:38 +0530 Subject: [PATCH 06/14] refactor: refactor postAuthenticatedRequest to use object parameter --- .../[slug]/[locale]/claim/[type]/[code].tsx | 12 ++++----- .../[slug]/[locale]/profile/redeem/[code].tsx | 12 ++++----- .../user/BulkCodes/forms/IssueCodesForm.tsx | 14 +++++------ .../ManagePayouts/screens/AddBankAccount.tsx | 12 ++++----- .../components/BasicDetails.tsx | 8 +++++- .../components/ProjectCertificates.tsx | 12 ++++----- .../components/ProjectMedia.tsx | 12 ++++----- .../components/ProjectSites.tsx | 12 ++++----- .../components/ProjectSpending.tsx | 12 ++++----- .../components/CreateAccountForm.tsx | 10 ++++---- .../Profile/ProfileCard/RedeemModal/index.tsx | 12 ++++----- .../RegisterTrees/UploadImages.tsx | 12 ++++----- .../RegisterTrees/RegisterTreesWidget.tsx | 12 ++++----- .../AddressManagement/AddAddress.tsx | 12 ++++----- .../Import/components/PlantingLocation.tsx | 12 ++++----- .../Import/components/SampleTrees.tsx | 12 ++++----- .../TreeMapper/MySpecies/MySpeciesForm.tsx | 10 ++++---- src/utils/apiRequests/api.ts | 25 +++++++++++++------ 18 files changed, 119 insertions(+), 104 deletions(-) diff --git a/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx b/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx index 5ce505ae9b..a839ae9e54 100644 --- a/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx +++ b/pages/sites/[slug]/[locale]/claim/[type]/[code].tsx @@ -83,13 +83,13 @@ function ClaimDonation({ pageProps }: Props): ReactElement { }; if (contextLoaded && user) { try { - const res = await postAuthenticatedRequest( - pageProps.tenantConfig.id, - `/app/redeem`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: pageProps.tenantConfig.id, + url: `/app/redeem`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setRedeemedCodeData(res); } catch (err) { const serializedErrors = handleError(err as APIError); diff --git a/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx b/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx index 9855b64586..7bec275df5 100644 --- a/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx +++ b/pages/sites/[slug]/[locale]/profile/redeem/[code].tsx @@ -78,13 +78,13 @@ const RedeemCode = ({ pageProps: { tenantConfig } }: Props) => { if (contextLoaded && user) { try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/redeem`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/redeem`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setRedeemedCodeData(res); } catch (err) { const serializedErrors = handleError(err as APIError); diff --git a/src/features/user/BulkCodes/forms/IssueCodesForm.tsx b/src/features/user/BulkCodes/forms/IssueCodesForm.tsx index 1c4b45e251..9641007ec7 100644 --- a/src/features/user/BulkCodes/forms/IssueCodesForm.tsx +++ b/src/features/user/BulkCodes/forms/IssueCodesForm.tsx @@ -152,16 +152,16 @@ const IssueCodesForm = (): ReactElement | null => { const cleanedData = cleanObject(donationData); try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/donations`, - cleanedData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/donations`, + data: cleanedData, token, logoutUser, - { + headers: { 'IDEMPOTENCY-KEY': uuidv4(), - } - ); + }, + }); // if request is successful, it will have a uid if (res?.uid) { resetBulkContext(); diff --git a/src/features/user/ManagePayouts/screens/AddBankAccount.tsx b/src/features/user/ManagePayouts/screens/AddBankAccount.tsx index ced3484c0a..efa29c3448 100644 --- a/src/features/user/ManagePayouts/screens/AddBankAccount.tsx +++ b/src/features/user/ManagePayouts/screens/AddBankAccount.tsx @@ -35,13 +35,13 @@ const AddBankAccount = (): ReactElement | null => { data.currency === PayoutCurrency.DEFAULT ? '' : data.payoutMinAmount, }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - '/app/accounts', - accountData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/accounts', + data: accountData, token, - logoutUser - ); + logoutUser, + }); if (accounts) { setAccounts([...accounts, res]); } else { diff --git a/src/features/user/ManageProjects/components/BasicDetails.tsx b/src/features/user/ManageProjects/components/BasicDetails.tsx index e9fd823a06..035f600c7e 100644 --- a/src/features/user/ManageProjects/components/BasicDetails.tsx +++ b/src/features/user/ManageProjects/components/BasicDetails.tsx @@ -384,7 +384,13 @@ export default function BasicDetails({ try { const res = await postAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >(tenantConfig?.id, `/app/projects`, submitData, token, logoutUser); + >({ + tenant: tenantConfig?.id, + url: `/app/projects`, + data: submitData, + token, + logoutUser, + }); setProjectGUID(res.id); setProjectDetails(res); router.push(`/profile/projects/${res.id}?type=media`); diff --git a/src/features/user/ManageProjects/components/ProjectCertificates.tsx b/src/features/user/ManageProjects/components/ProjectCertificates.tsx index 586a17199e..50f1e84c1b 100644 --- a/src/features/user/ManageProjects/components/ProjectCertificates.tsx +++ b/src/features/user/ManageProjects/components/ProjectCertificates.tsx @@ -87,13 +87,13 @@ function ProjectCertificates({ }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/certificates`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/certificates`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); let newUploadedFiles = uploadedFiles; if (newUploadedFiles === undefined) { diff --git a/src/features/user/ManageProjects/components/ProjectMedia.tsx b/src/features/user/ManageProjects/components/ProjectMedia.tsx index 11fbf17df9..07a6fcd665 100644 --- a/src/features/user/ManageProjects/components/ProjectMedia.tsx +++ b/src/features/user/ManageProjects/components/ProjectMedia.tsx @@ -95,13 +95,13 @@ export default function ProjectMedia({ }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/images`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/images`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); let newUploadedImages = [...uploadedImages]; if (!newUploadedImages) { diff --git a/src/features/user/ManageProjects/components/ProjectSites.tsx b/src/features/user/ManageProjects/components/ProjectSites.tsx index 206e6d98f3..c2a9b4c27d 100644 --- a/src/features/user/ManageProjects/components/ProjectSites.tsx +++ b/src/features/user/ManageProjects/components/ProjectSites.tsx @@ -373,13 +373,13 @@ export default function ProjectSites({ }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/sites`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/sites`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); const temp = siteList ? siteList : []; const _submitData = { id: res.id, diff --git a/src/features/user/ManageProjects/components/ProjectSpending.tsx b/src/features/user/ManageProjects/components/ProjectSpending.tsx index b2a0fc4dae..24032e7929 100644 --- a/src/features/user/ManageProjects/components/ProjectSpending.tsx +++ b/src/features/user/ManageProjects/components/ProjectSpending.tsx @@ -91,13 +91,13 @@ export default function ProjectSpending({ }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/expenses`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/expenses`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); const newUploadedFiles = uploadedFiles; newUploadedFiles.push(res); setUploadedFiles(newUploadedFiles); diff --git a/src/features/user/PlanetCash/components/CreateAccountForm.tsx b/src/features/user/PlanetCash/components/CreateAccountForm.tsx index 99ece1900e..225534d41c 100644 --- a/src/features/user/PlanetCash/components/CreateAccountForm.tsx +++ b/src/features/user/PlanetCash/components/CreateAccountForm.tsx @@ -43,13 +43,13 @@ const CreateAccountForm = ({ setIsProcessing(true); try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - '/app/planetCash', + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/planetCash', data, token, - logoutUser - ); + logoutUser, + }); setIsAccountCreated(true); setAccounts([res]); // go to accounts tab diff --git a/src/features/user/Profile/ProfileCard/RedeemModal/index.tsx b/src/features/user/Profile/ProfileCard/RedeemModal/index.tsx index 4e2f43b8e6..7c3cc94d2f 100644 --- a/src/features/user/Profile/ProfileCard/RedeemModal/index.tsx +++ b/src/features/user/Profile/ProfileCard/RedeemModal/index.tsx @@ -50,13 +50,13 @@ export default function RedeemModal({ }; if (contextLoaded && user) { try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/redeem`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/redeem`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setRedeemedCodeData(res); setRefetchUserData(true); setIsLoading(false); diff --git a/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx b/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx index 74969cbbcc..596e0cae3d 100644 --- a/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx +++ b/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx @@ -39,13 +39,13 @@ export default function UploadImages({ }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/contributions/${contributionGUID}/images`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/contributions/${contributionGUID}/images`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); const newUploadedImages: Image[] = uploadedImages; newUploadedImages.push(res); setUploadedImages(newUploadedImages); diff --git a/src/features/user/RegisterTrees/RegisterTreesWidget.tsx b/src/features/user/RegisterTrees/RegisterTreesWidget.tsx index dececc1e26..b0e0288627 100644 --- a/src/features/user/RegisterTrees/RegisterTreesWidget.tsx +++ b/src/features/user/RegisterTrees/RegisterTreesWidget.tsx @@ -189,13 +189,13 @@ function RegisterTreesForm({ geometry: geometry, }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/app/contributions`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/contributions`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setErrorMessage(''); setContributionGUID(res.id); setContributionDetails(res); diff --git a/src/features/user/Settings/EditProfile/AddressManagement/AddAddress.tsx b/src/features/user/Settings/EditProfile/AddressManagement/AddAddress.tsx index bada5fb106..e9981098bf 100644 --- a/src/features/user/Settings/EditProfile/AddressManagement/AddAddress.tsx +++ b/src/features/user/Settings/EditProfile/AddressManagement/AddAddress.tsx @@ -63,13 +63,13 @@ const AddAddress = ({ type: ADDRESS_TYPE.OTHER, }; try { - const res = await postAuthenticatedRequest
( - tenantConfig.id, - '/app/addresses', - bodyToSend, + const res = await postAuthenticatedRequest
({ + tenant: tenantConfig.id, + url: '/app/addresses', + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); if (res && setUserAddresses) { setUserAddresses((prevAddresses) => [...prevAddresses, res]); } diff --git a/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx b/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx index c066420306..a53684e8f1 100644 --- a/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx +++ b/src/features/user/TreeMapper/Import/components/PlantingLocation.tsx @@ -341,13 +341,13 @@ export default function PlantingLocation({ }; try { - const res = await postAuthenticatedRequest( - tenantConfig?.id, - `/treemapper/interventions`, - submitData, + const res = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/treemapper/interventions`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setPlantLocation(res); setIsUploadingData(false); handleNext(); diff --git a/src/features/user/TreeMapper/Import/components/SampleTrees.tsx b/src/features/user/TreeMapper/Import/components/SampleTrees.tsx index 1d9d8e62ca..d8e9607529 100644 --- a/src/features/user/TreeMapper/Import/components/SampleTrees.tsx +++ b/src/features/user/TreeMapper/Import/components/SampleTrees.tsx @@ -140,13 +140,13 @@ export default function SampleTrees({ setUploadStatus(newStatus); try { - const res: SampleTree = await postAuthenticatedRequest( - tenantConfig?.id, - `/treemapper/interventions`, - sampleTree, + const res: SampleTree = await postAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/treemapper/interventions`, + data: sampleTree, token, - logoutUser - ); + logoutUser, + }); const newSampleTrees = [...sampleTrees]; newSampleTrees[index] = res; setSampleTrees(newSampleTrees); diff --git a/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx b/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx index afd57b6033..9c11932af0 100644 --- a/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx +++ b/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx @@ -87,13 +87,13 @@ export default function MySpeciesForm() { scientificSpecies: species.scientificSpecies?.id, }; try { - await postAuthenticatedRequest( - tenantConfig.id, - `/treemapper/species`, + await postAuthenticatedRequest({ + tenant: tenantConfig.id, + url: `/treemapper/species`, data, token, - logoutUser - ); + logoutUser, + }); } catch (err) { setErrors(handleError(err as APIError)); } diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 311f3e273b..726110b0ab 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -28,6 +28,15 @@ type GetRequestOptions = Omit< GetAuthRequestOptions, 'header' | 'logoutUser' | 'token' >; + +interface PostAuthRequestOptions { + tenant: string | undefined; + url: string; + data: any; + token: string | null; + logoutUser: (value?: string | undefined) => void; + headers?: Record; +} // API call to private /profile endpoint export async function getAccountInfo({ tenant, @@ -151,14 +160,14 @@ export function getAuthenticatedRequest({ }); } -export function postAuthenticatedRequest( - tenant: string | undefined, - url: string, - data: any, - token: string | null, - logoutUser: (value?: string | undefined) => void, - headers?: Record -) { +export function postAuthenticatedRequest({ + tenant, + url, + data, + token, + logoutUser, + headers, +}: PostAuthRequestOptions) { const lang = localStorage.getItem('language') || 'en'; return new Promise((resolve, reject) => { (async () => { From 0720a9c16754ab2f5839232ad1b3694a7bdf212a Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Thu, 2 Jan 2025 16:57:36 +0530 Subject: [PATCH 07/14] refactor: refactor postRequest to use param object --- src/features/user/CompleteSignup/index.tsx | 10 +++++----- .../TreeMapper/MySpecies/SpeciesAutoComplete.tsx | 12 ++++++------ src/tenants/planet/LeaderBoard/components/Score.tsx | 8 ++++++-- src/utils/apiRequests/api.ts | 12 +++++++----- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/features/user/CompleteSignup/index.tsx b/src/features/user/CompleteSignup/index.tsx index e236c8c975..3b2374ab33 100644 --- a/src/features/user/CompleteSignup/index.tsx +++ b/src/features/user/CompleteSignup/index.tsx @@ -176,11 +176,11 @@ export default function CompleteSignup(): ReactElement | null { setRequestSent(true); setIsProcessing(true); try { - const res = await postRequest( - tenantConfig?.id, - `/app/profile`, - bodyToSend - ); + const res = await postRequest({ + tenant: tenantConfig?.id, + url: `/app/profile`, + data: bodyToSend, + }); setRequestSent(false); // successful signup -> goto me page setUser(res); diff --git a/src/features/user/TreeMapper/MySpecies/SpeciesAutoComplete.tsx b/src/features/user/TreeMapper/MySpecies/SpeciesAutoComplete.tsx index ef1a28bc30..0b8d9b852b 100644 --- a/src/features/user/TreeMapper/MySpecies/SpeciesAutoComplete.tsx +++ b/src/features/user/TreeMapper/MySpecies/SpeciesAutoComplete.tsx @@ -59,14 +59,14 @@ export default function SpeciesSelect< // Todo: debouncing if (value.length > 2) { try { - const res = await postRequest( - tenantConfig?.id, - `/suggest.php`, - { + const res = await postRequest({ + tenant: tenantConfig?.id, + url: `/suggest.php`, + data: { q: value, t: 'species', - } - ); + }, + }); if (res && res.length > 0) { const species = res.map((item) => ({ id: item.id, diff --git a/src/tenants/planet/LeaderBoard/components/Score.tsx b/src/tenants/planet/LeaderBoard/components/Score.tsx index c59eb7de4d..aee2519aab 100644 --- a/src/tenants/planet/LeaderBoard/components/Score.tsx +++ b/src/tenants/planet/LeaderBoard/components/Score.tsx @@ -30,8 +30,12 @@ export default function LeaderBoardSection(leaderboard: Props) { const { tenantConfig } = useTenant(); const fetchUsers = async (query: any) => { try { - const res = await postRequest(tenantConfig?.id, '/suggest.php', { - q: query, + const res = await postRequest({ + tenant: tenantConfig?.id, + url: '/suggest.php', + data: { + q: query, + }, }); const result = res.filter((item) => item.type !== 'competition'); setUsers(result); diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 726110b0ab..453a6eecf6 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -37,6 +37,12 @@ interface PostAuthRequestOptions { logoutUser: (value?: string | undefined) => void; headers?: Record; } + +interface PostRequestOptions { + tenant: string | undefined; + url: string; + data: any; +} // API call to private /profile endpoint export async function getAccountInfo({ tenant, @@ -210,11 +216,7 @@ export function postAuthenticatedRequest({ }); } -export function postRequest( - tenant: string | undefined, - url: string, - data: any -) { +export function postRequest({ tenant, url, data }: PostRequestOptions) { const lang = localStorage.getItem('language') || 'en'; return new Promise((resolve, reject) => { (async () => { From 7abf1c814d8bb1c7c10f2716a94c20a43f438a69 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:35:00 +0530 Subject: [PATCH 08/14] refactor: refactor deleteAuthenticatedRequest to use object param --- .../components/ProjectCertificates.tsx | 10 +++++----- .../components/ProjectMedia.tsx | 10 +++++----- .../components/ProjectSites.tsx | 10 +++++----- .../components/ProjectSpending.tsx | 10 +++++----- .../RegisterTrees/UploadImages.tsx | 10 +++++----- .../DeleteProfile/DeleteProfileForm.tsx | 10 +++++----- .../AddressManagement/DeleteAddress.tsx | 10 +++++----- src/utils/apiRequests/api.ts | 20 +++++++++++++------ 8 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/features/user/ManageProjects/components/ProjectCertificates.tsx b/src/features/user/ManageProjects/components/ProjectCertificates.tsx index 50f1e84c1b..a02fe94eed 100644 --- a/src/features/user/ManageProjects/components/ProjectCertificates.tsx +++ b/src/features/user/ManageProjects/components/ProjectCertificates.tsx @@ -174,12 +174,12 @@ function ProjectCertificates({ const deleteProjectCertificate = async (id: string) => { try { - await deleteAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/certificates/${id}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/certificates/${id}`, token, - logoutUser - ); + logoutUser, + }); const uploadedFilesTemp = uploadedFiles.filter((item) => item.id !== id); setUploadedFiles(uploadedFilesTemp); } catch (err) { diff --git a/src/features/user/ManageProjects/components/ProjectMedia.tsx b/src/features/user/ManageProjects/components/ProjectMedia.tsx index 07a6fcd665..17fa5f08a2 100644 --- a/src/features/user/ManageProjects/components/ProjectMedia.tsx +++ b/src/features/user/ManageProjects/components/ProjectMedia.tsx @@ -152,12 +152,12 @@ export default function ProjectMedia({ const deleteProjectCertificate = async (id: string) => { try { - await deleteAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/images/${id}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/images/${id}`, token, - logoutUser - ); + logoutUser, + }); const uploadedFilesTemp = uploadedImages.filter((item) => item.id !== id); setUploadedImages(uploadedFilesTemp); } catch (err) { diff --git a/src/features/user/ManageProjects/components/ProjectSites.tsx b/src/features/user/ManageProjects/components/ProjectSites.tsx index c2a9b4c27d..054cf129ce 100644 --- a/src/features/user/ManageProjects/components/ProjectSites.tsx +++ b/src/features/user/ManageProjects/components/ProjectSites.tsx @@ -410,12 +410,12 @@ export default function ProjectSites({ const deleteProjectSite = async (id: string) => { try { setIsUploadingData(true); - await deleteAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/sites/${id}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/sites/${id}`, token, - logoutUser - ); + logoutUser, + }); const siteListTemp = siteList.filter((item) => item.id !== id); setSiteList(siteListTemp); setIsUploadingData(false); diff --git a/src/features/user/ManageProjects/components/ProjectSpending.tsx b/src/features/user/ManageProjects/components/ProjectSpending.tsx index 24032e7929..abbefe5f73 100644 --- a/src/features/user/ManageProjects/components/ProjectSpending.tsx +++ b/src/features/user/ManageProjects/components/ProjectSpending.tsx @@ -148,12 +148,12 @@ export default function ProjectSpending({ const deleteProjectSpending = async (id: string) => { try { setIsUploadingData(true); - await deleteAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/expenses/${id}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/expenses/${id}`, token, - logoutUser - ); + logoutUser, + }); const uploadedFilesTemp = uploadedFiles.filter((item) => item.id !== id); setUploadedFiles(uploadedFilesTemp); setIsUploadingData(false); diff --git a/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx b/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx index 596e0cae3d..0c75774526 100644 --- a/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx +++ b/src/features/user/RegisterTrees/RegisterTrees/UploadImages.tsx @@ -82,12 +82,12 @@ export default function UploadImages({ const deleteContributionImage = async (id: string) => { try { - await deleteAuthenticatedRequest( - tenantConfig?.id, - `/app/contributions/${contributionGUID}/images/${id}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/contributions/${contributionGUID}/images/${id}`, token, - logoutUser - ); + logoutUser, + }); const uploadedImagesTemp = uploadedImages; const index = uploadedImagesTemp.findIndex((item) => { return item.id === id; diff --git a/src/features/user/Settings/DeleteProfile/DeleteProfileForm.tsx b/src/features/user/Settings/DeleteProfile/DeleteProfileForm.tsx index 815e043a02..e7a44c05a8 100644 --- a/src/features/user/Settings/DeleteProfile/DeleteProfileForm.tsx +++ b/src/features/user/Settings/DeleteProfile/DeleteProfileForm.tsx @@ -26,12 +26,12 @@ export default function DeleteProfileForm() { const handleDeleteAccount = async () => { setIsUploadingData(true); try { - await deleteAuthenticatedRequest( - tenantConfig?.id, - '/app/profile', + await deleteAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile', token, - logoutUser - ); + logoutUser, + }); setIsUploadingData(false); logoutUser(`${window.location.origin}/`); } catch (err) { diff --git a/src/features/user/Settings/EditProfile/AddressManagement/DeleteAddress.tsx b/src/features/user/Settings/EditProfile/AddressManagement/DeleteAddress.tsx index 11402f07b7..795e16697f 100644 --- a/src/features/user/Settings/EditProfile/AddressManagement/DeleteAddress.tsx +++ b/src/features/user/Settings/EditProfile/AddressManagement/DeleteAddress.tsx @@ -37,12 +37,12 @@ const DeleteAddress = ({ if (!contextLoaded || !user || !token) return; try { setIsLoading(true); - await deleteAuthenticatedRequest( - tenantConfig.id, - `/app/addresses/${addressId}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig.id, + url: `/app/addresses/${addressId}`, token, - logoutUser - ); + logoutUser, + }); updateUserAddresses(); } catch (error) { setErrors(handleError(error as APIError)); diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 453a6eecf6..749bbefabe 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -43,6 +43,14 @@ interface PostRequestOptions { url: string; data: any; } + +interface DeleteAuthRequestOptions { + tenant: string | undefined; + url: string; + token: string | null; + logoutUser: (value?: string | undefined) => void; +} + // API call to private /profile endpoint export async function getAccountInfo({ tenant, @@ -248,12 +256,12 @@ export function postRequest({ tenant, url, data }: PostRequestOptions) { }); } -export function deleteAuthenticatedRequest( - tenant: string | undefined, - url: string, - token: string | null, - logoutUser: (value?: string | undefined) => void -) { +export function deleteAuthenticatedRequest({ + tenant, + url, + token, + logoutUser, +}: DeleteAuthRequestOptions) { const lang = localStorage.getItem('language') || 'en'; return new Promise((resolve, reject) => { (async () => { From be430cc456530fc04ecbf6a85741c7620647659f Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Fri, 3 Jan 2025 12:44:31 +0530 Subject: [PATCH 09/14] refactor: refactor putAuthenticatedRequest to use object parameter --- src/features/user/Account/CancelModal.tsx | 12 ++++----- src/features/user/Account/EditModal.tsx | 12 ++++----- src/features/user/Account/PauseModal.tsx | 12 ++++----- src/features/user/Account/ReactivateModal.tsx | 12 ++++----- .../ManagePayouts/screens/EditBankAccount.tsx | 12 ++++----- .../screens/PayoutScheduleForm.tsx | 12 ++++----- .../components/BasicDetails.tsx | 12 ++++----- .../components/DetailedAnalysis.tsx | 12 ++++----- .../components/ProjectMedia.tsx | 12 ++++----- .../components/ProjectSites.tsx | 12 ++++----- src/features/user/ManageProjects/index.tsx | 12 ++++----- .../Profile/ForestProgress/TargetsModal.tsx | 12 ++++----- .../user/Settings/ApiKey/ApiKeyForm.tsx | 11 ++++---- .../AddressManagement/EditAddress.tsx | 12 ++++----- .../AddressManagement/UnsetBillingAddress.tsx | 12 ++++----- .../AddressManagement/UpdateAddressType.tsx | 12 ++++----- .../Settings/EditProfile/EditProfileForm.tsx | 25 ++++++++++--------- .../Settings/ImpersonateUser/SupportPin.tsx | 11 ++++---- src/features/user/Widget/EmbedModal.tsx | 12 ++++----- src/utils/apiRequests/api.ts | 22 ++++++++++------ 20 files changed, 134 insertions(+), 127 deletions(-) diff --git a/src/features/user/Account/CancelModal.tsx b/src/features/user/Account/CancelModal.tsx index d14f5d030b..7775d96d99 100644 --- a/src/features/user/Account/CancelModal.tsx +++ b/src/features/user/Account/CancelModal.tsx @@ -82,13 +82,13 @@ export const CancelModal = ({ }; try { - await putAuthenticatedRequest( - tenantConfig?.id, - `/app/subscriptions/${record.id}?scope=cancel`, - bodyToSend, + await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/subscriptions/${record.id}?scope=cancel`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); handleCancelModalClose(); fetchRecurrentDonations(); } catch (err) { diff --git a/src/features/user/Account/EditModal.tsx b/src/features/user/Account/EditModal.tsx index e2db5104bf..426b76731a 100644 --- a/src/features/user/Account/EditModal.tsx +++ b/src/features/user/Account/EditModal.tsx @@ -126,13 +126,13 @@ export const EditModal = ({ if (Object.keys(bodyToSend).length !== 0) { try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/subscriptions/${record?.id}?scope=modify`, - bodyToSend, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/subscriptions/${record?.id}?scope=modify`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); if (res?.status === 'action_required') { window.open(res.response.confirmationUrl, '_blank'); } diff --git a/src/features/user/Account/PauseModal.tsx b/src/features/user/Account/PauseModal.tsx index 16c8c13c78..ca2a115541 100644 --- a/src/features/user/Account/PauseModal.tsx +++ b/src/features/user/Account/PauseModal.tsx @@ -88,13 +88,13 @@ export const PauseModal = ({ }; try { - await putAuthenticatedRequest( - tenantConfig?.id, - `/app/subscriptions/${record.id}?scope=pause`, - bodyToSend, + await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/subscriptions/${record.id}?scope=pause`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); handlePauseModalClose(); fetchRecurrentDonations(); } catch (err) { diff --git a/src/features/user/Account/ReactivateModal.tsx b/src/features/user/Account/ReactivateModal.tsx index e640ef8a44..5a7a9d5d2e 100644 --- a/src/features/user/Account/ReactivateModal.tsx +++ b/src/features/user/Account/ReactivateModal.tsx @@ -40,13 +40,13 @@ export const ReactivateModal = ({ setDisabled(true); try { - await putAuthenticatedRequest( - tenantConfig?.id, - `/app/subscriptions/${record.id}?scope=reactivate`, - bodyToSend, + await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/subscriptions/${record.id}?scope=reactivate`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); handleReactivateModalClose(); fetchRecurrentDonations(); } catch (err) { diff --git a/src/features/user/ManagePayouts/screens/EditBankAccount.tsx b/src/features/user/ManagePayouts/screens/EditBankAccount.tsx index c8bd87c49e..579b79f283 100644 --- a/src/features/user/ManagePayouts/screens/EditBankAccount.tsx +++ b/src/features/user/ManagePayouts/screens/EditBankAccount.tsx @@ -41,13 +41,13 @@ const EditBankAccount = (): ReactElement | null => { }; try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/accounts/${accountToEdit?.id}`, - accountData, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/accounts/${accountToEdit?.id}`, + data: accountData, token, - logoutUser - ); + logoutUser, + }); // update accounts in context if (accounts) { const updatedAccounts = accounts.map((account) => { diff --git a/src/features/user/ManagePayouts/screens/PayoutScheduleForm.tsx b/src/features/user/ManagePayouts/screens/PayoutScheduleForm.tsx index 0f32c7377c..0faa5601bf 100644 --- a/src/features/user/ManagePayouts/screens/PayoutScheduleForm.tsx +++ b/src/features/user/ManagePayouts/screens/PayoutScheduleForm.tsx @@ -44,13 +44,13 @@ const PayoutScheduleForm = (): ReactElement | null => { setIsProcessing(true); try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - '/app/profile', - { scheduleFrequency: data.scheduleFrequency }, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile', + data: { scheduleFrequency: data.scheduleFrequency }, token, - logoutUser - ); + logoutUser, + }); setUser(res); setIsSaved(true); setIsProcessing(false); diff --git a/src/features/user/ManageProjects/components/BasicDetails.tsx b/src/features/user/ManageProjects/components/BasicDetails.tsx index 035f600c7e..127ab16fb7 100644 --- a/src/features/user/ManageProjects/components/BasicDetails.tsx +++ b/src/features/user/ManageProjects/components/BasicDetails.tsx @@ -366,13 +366,13 @@ export default function BasicDetails({ try { const res = await putAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >( - tenantConfig?.id, - `/app/projects/${projectGUID}`, - submitData, + >({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setProjectDetails(res); setIsUploadingData(false); handleNext(ProjectCreationTabs.PROJECT_MEDIA); diff --git a/src/features/user/ManageProjects/components/DetailedAnalysis.tsx b/src/features/user/ManageProjects/components/DetailedAnalysis.tsx index 4e58ef2018..a2b0acba4d 100644 --- a/src/features/user/ManageProjects/components/DetailedAnalysis.tsx +++ b/src/features/user/ManageProjects/components/DetailedAnalysis.tsx @@ -367,13 +367,13 @@ export default function DetailedAnalysis({ try { const res = await putAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >( - tenantConfig?.id, - `/app/projects/${projectGUID}`, - submitData, + >({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setProjectDetails(res); setIsUploadingData(false); setIsInterventionsMissing(null); diff --git a/src/features/user/ManageProjects/components/ProjectMedia.tsx b/src/features/user/ManageProjects/components/ProjectMedia.tsx index 17fa5f08a2..fdb2e3515a 100644 --- a/src/features/user/ManageProjects/components/ProjectMedia.tsx +++ b/src/features/user/ManageProjects/components/ProjectMedia.tsx @@ -232,13 +232,13 @@ export default function ProjectMedia({ }; try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/images/${id}`, - submitData, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/images/${id}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); const tempUploadedData = uploadedImages; tempUploadedData[index].description = res.description; setUploadedImages(tempUploadedData); diff --git a/src/features/user/ManageProjects/components/ProjectSites.tsx b/src/features/user/ManageProjects/components/ProjectSites.tsx index 054cf129ce..55b4ba0c53 100644 --- a/src/features/user/ManageProjects/components/ProjectSites.tsx +++ b/src/features/user/ManageProjects/components/ProjectSites.tsx @@ -102,13 +102,13 @@ function EditSite({ }; try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/sites/${siteGUID}`, - submitData, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/sites/${siteGUID}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); const temp = siteList; let siteIndex = 0; temp.find((site: Site, index: number) => { diff --git a/src/features/user/ManageProjects/index.tsx b/src/features/user/ManageProjects/index.tsx index 53ddac4bb4..082227ad7f 100644 --- a/src/features/user/ManageProjects/index.tsx +++ b/src/features/user/ManageProjects/index.tsx @@ -105,13 +105,13 @@ export default function ManageProjects({ try { const res = await putAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >( - tenantConfig?.id, - `/app/projects/${projectGUID}`, - submitData, + >({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setProjectDetails(res); setIsUploadingData(false); } catch (err) { diff --git a/src/features/user/Profile/ForestProgress/TargetsModal.tsx b/src/features/user/Profile/ForestProgress/TargetsModal.tsx index b4ce795df4..d17c95f1ed 100644 --- a/src/features/user/Profile/ForestProgress/TargetsModal.tsx +++ b/src/features/user/Profile/ForestProgress/TargetsModal.tsx @@ -78,13 +78,13 @@ const TargetsModal = ({ }, }; try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/profile`, - bodyToSend, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); const newUserInfo = { profileId: res.id, slug: res.slug, diff --git a/src/features/user/Settings/ApiKey/ApiKeyForm.tsx b/src/features/user/Settings/ApiKey/ApiKeyForm.tsx index 5df65ccfec..b8d38f2b8e 100644 --- a/src/features/user/Settings/ApiKey/ApiKeyForm.tsx +++ b/src/features/user/Settings/ApiKey/ApiKeyForm.tsx @@ -69,13 +69,12 @@ export default function ApiKey() { e.preventDefault(); setIsUploadingData(true); try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/apiKey', - undefined, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/apiKey', token, - logoutUser - ); + logoutUser, + }); if (res) { setApiKey(res.apiKey || ''); } diff --git a/src/features/user/Settings/EditProfile/AddressManagement/EditAddress.tsx b/src/features/user/Settings/EditProfile/AddressManagement/EditAddress.tsx index 25ab975bcc..537dd85ebb 100644 --- a/src/features/user/Settings/EditProfile/AddressManagement/EditAddress.tsx +++ b/src/features/user/Settings/EditProfile/AddressManagement/EditAddress.tsx @@ -54,13 +54,13 @@ const EditAddress = ({ type: selectedAddressForAction?.type, }; try { - const res = await putAuthenticatedRequest
( - tenantConfig.id, - `/app/addresses/${selectedAddressForAction?.id}`, - bodyToSend, + const res = await putAuthenticatedRequest
({ + tenant: tenantConfig.id, + url: `/app/addresses/${selectedAddressForAction?.id}`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); if (res && updateUserAddresses) updateUserAddresses(); } catch (error) { setErrors(handleError(error as APIError)); diff --git a/src/features/user/Settings/EditProfile/AddressManagement/UnsetBillingAddress.tsx b/src/features/user/Settings/EditProfile/AddressManagement/UnsetBillingAddress.tsx index f83b4dae4e..e4461371c7 100644 --- a/src/features/user/Settings/EditProfile/AddressManagement/UnsetBillingAddress.tsx +++ b/src/features/user/Settings/EditProfile/AddressManagement/UnsetBillingAddress.tsx @@ -43,13 +43,13 @@ const UnsetBillingAddress = ({ type: ADDRESS_TYPE.OTHER, }; try { - const res = await putAuthenticatedRequest
( - tenantConfig.id, - `/app/addresses/${selectedAddressForAction.id}`, - bodyToSend, + const res = await putAuthenticatedRequest
({ + tenant: tenantConfig.id, + url: `/app/addresses/${selectedAddressForAction.id}`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); if (res) updateUserAddresses(); } catch (error) { setErrors(handleError(error as APIError)); diff --git a/src/features/user/Settings/EditProfile/AddressManagement/UpdateAddressType.tsx b/src/features/user/Settings/EditProfile/AddressManagement/UpdateAddressType.tsx index e3e92a9f47..01cbc4669e 100644 --- a/src/features/user/Settings/EditProfile/AddressManagement/UpdateAddressType.tsx +++ b/src/features/user/Settings/EditProfile/AddressManagement/UpdateAddressType.tsx @@ -45,13 +45,13 @@ const UpdateAddressType = ({ type: addressType, }; try { - const res = await putAuthenticatedRequest
( - tenantConfig.id, - `/app/addresses/${selectedAddressForAction.id}`, - bodyToSend, + const res = await putAuthenticatedRequest
({ + tenant: tenantConfig.id, + url: `/app/addresses/${selectedAddressForAction.id}`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); if (res) updateUserAddresses(); } catch (error) { setErrors(handleError(error as APIError)); diff --git a/src/features/user/Settings/EditProfile/EditProfileForm.tsx b/src/features/user/Settings/EditProfile/EditProfileForm.tsx index 424aefe17c..cc9fcf158b 100644 --- a/src/features/user/Settings/EditProfile/EditProfileForm.tsx +++ b/src/features/user/Settings/EditProfile/EditProfileForm.tsx @@ -162,13 +162,14 @@ export default function EditProfileForm() { imageFile: string | ArrayBuffer | null | undefined; }) => { try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/profile`, - bodyToSend, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); + if (user) { const newUserInfo = { ...user, image: res.image }; setUpdatingPic(false); @@ -233,13 +234,13 @@ export default function EditProfileForm() { if (contextLoaded && token) { try { - const res: User = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/profile`, - bodyToSend, + const res: User = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); setSeverity('success'); setSnackbarMessage(t('profileSaved')); handleSnackbarOpen(); diff --git a/src/features/user/Settings/ImpersonateUser/SupportPin.tsx b/src/features/user/Settings/ImpersonateUser/SupportPin.tsx index e43cc377e6..ff72fe6d51 100644 --- a/src/features/user/Settings/ImpersonateUser/SupportPin.tsx +++ b/src/features/user/Settings/ImpersonateUser/SupportPin.tsx @@ -15,13 +15,12 @@ const SupportPin = () => { const { tenantConfig } = useTenant(); const handleNewPin = async () => { try { - const response = await putAuthenticatedRequest( - tenantConfig?.id, - '/app/profile/supportPin', - undefined, + const response = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: '/app/profile/supportPin', token, - logoutUser - ); + logoutUser, + }); if (response) { const updateUserData = { ...user }; updateUserData['supportPin'] = response?.supportPin; diff --git a/src/features/user/Widget/EmbedModal.tsx b/src/features/user/Widget/EmbedModal.tsx index 70acb6969c..218f3fe0dd 100644 --- a/src/features/user/Widget/EmbedModal.tsx +++ b/src/features/user/Widget/EmbedModal.tsx @@ -59,13 +59,13 @@ export default function EmbedModal({ }; if (contextLoaded && token) { try { - const res = await putAuthenticatedRequest( - tenantConfig?.id, - `/app/profile`, - bodyToSend, + const res = await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/profile`, + data: bodyToSend, token, - logoutUser - ); + logoutUser, + }); setSeverity('success'); setSnackbarMessage(t('profileSaved')); handleSnackbarOpen(); diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 749bbefabe..e1e909bdf0 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -51,6 +51,14 @@ interface DeleteAuthRequestOptions { logoutUser: (value?: string | undefined) => void; } +interface PutAuthRequestOptions { + tenant: string | undefined; + url: string; + data?: any; + token: string | null; + logoutUser: (value?: string | undefined) => void; +} + // API call to private /profile endpoint export async function getAccountInfo({ tenant, @@ -302,13 +310,13 @@ export function deleteAuthenticatedRequest({ }); } -export function putAuthenticatedRequest( - tenant: string | undefined, - url: string, - data: any, - token: string | null, - logoutUser: (value?: string | undefined) => void -) { +export function putAuthenticatedRequest({ + tenant, + url, + data, + token, + logoutUser, +}: PutAuthRequestOptions) { return new Promise((resolve, reject) => { const lang = localStorage.getItem('language') || 'en'; (async () => { From e885b3d5cfa93578757cdf96441ea690face89d1 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Fri, 3 Jan 2025 13:41:17 +0530 Subject: [PATCH 10/14] refactor(types): introduce base interfaces to reduce redundancy in request options --- .../TreeMapper/MySpecies/MySpeciesForm.tsx | 10 ++--- src/utils/apiRequests/api.ts | 43 +++++++------------ 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx b/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx index 9c11932af0..b7b531bae0 100644 --- a/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx +++ b/src/features/user/TreeMapper/MySpecies/MySpeciesForm.tsx @@ -65,12 +65,12 @@ export default function MySpeciesForm() { const deleteSpecies = async (id: string) => { try { - await deleteAuthenticatedRequest( - tenantConfig.id, - `/treemapper/species/${id}`, + await deleteAuthenticatedRequest({ + tenant: tenantConfig.id, + url: `/treemapper/species/${id}`, token, - logoutUser - ); + logoutUser, + }); fetchMySpecies(); } catch (err) { setErrors(handleError(err as APIError)); diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index e1e909bdf0..1bf620fa5f 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -8,57 +8,46 @@ import { setHeaderForImpersonation } from './setHeader'; const INVALID_TOKEN_STATUS_CODE = 498; +interface BaseRequestOptions { + tenant?: string | undefined; + url: string; +} + interface GetAccountInfo { tenant: string | undefined; token: string | null; impersonationData?: ImpersonationData; } -interface GetAuthRequestOptions { - tenant?: string | undefined; - url: string; +interface GetAuthRequestOptions extends BaseRequestOptions { token: string | null; logoutUser: (value?: string | undefined) => void; header?: Record | null; queryParams?: { [key: string]: string }; version?: string; } - -type GetRequestOptions = Omit< - GetAuthRequestOptions, - 'header' | 'logoutUser' | 'token' ->; - -interface PostAuthRequestOptions { - tenant: string | undefined; - url: string; +interface PostAuthRequestOptions extends BaseRequestOptions { data: any; token: string | null; logoutUser: (value?: string | undefined) => void; headers?: Record; } - -interface PostRequestOptions { - tenant: string | undefined; - url: string; - data: any; -} - -interface DeleteAuthRequestOptions { - tenant: string | undefined; - url: string; +interface DeleteAuthRequestOptions extends BaseRequestOptions { token: string | null; logoutUser: (value?: string | undefined) => void; } - -interface PutAuthRequestOptions { - tenant: string | undefined; - url: string; +interface PutAuthRequestOptions extends BaseRequestOptions { data?: any; token: string | null; logoutUser: (value?: string | undefined) => void; } - +interface PostRequestOptions extends BaseRequestOptions { + data: any; +} +interface GetRequestOptions extends BaseRequestOptions { + queryParams?: { [key: string]: string }; + version?: string; +} // API call to private /profile endpoint export async function getAccountInfo({ tenant, From 1a99fa676aaec4e7675a780d6c97759df49d2c7e Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:32:38 +0530 Subject: [PATCH 11/14] refactor: remove duplicate imports --- pages/sites/[slug]/[locale]/profile/projects/index.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pages/sites/[slug]/[locale]/profile/projects/index.tsx b/pages/sites/[slug]/[locale]/profile/projects/index.tsx index decf71d768..9ad7e040d8 100644 --- a/pages/sites/[slug]/[locale]/profile/projects/index.tsx +++ b/pages/sites/[slug]/[locale]/profile/projects/index.tsx @@ -1,11 +1,7 @@ -import { AbstractIntlMessages, useTranslations } from 'next-intl'; -import { useUserProps } from '../../../../../../src/features/common/Layout/UserPropsContext'; -import AccessDeniedLoader from '../../../../../../src/features/common/ContentLoaders/Projects/AccessDeniedLoader'; -import { - GetStaticPaths, import type { ReactElement } from 'react'; import type { AbstractIntlMessages } from 'next-intl'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, From 0093b629c39a26268e855d495d7417a266b0b62e Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:38:32 +0530 Subject: [PATCH 12/14] refactor: add missing import --- pages/sites/[slug]/[locale]/mangroves.tsx | 1 + pages/sites/[slug]/[locale]/profile/delete-account.tsx | 1 + pages/sites/[slug]/[locale]/profile/register-trees.tsx | 1 + pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx | 1 + 4 files changed, 4 insertions(+) diff --git a/pages/sites/[slug]/[locale]/mangroves.tsx b/pages/sites/[slug]/[locale]/mangroves.tsx index c9ee18d28c..d6ade371df 100644 --- a/pages/sites/[slug]/[locale]/mangroves.tsx +++ b/pages/sites/[slug]/[locale]/mangroves.tsx @@ -1,6 +1,7 @@ import type { AbstractIntlMessages } from 'next-intl'; import type { Tenant } from '@planet-sdk/common'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, diff --git a/pages/sites/[slug]/[locale]/profile/delete-account.tsx b/pages/sites/[slug]/[locale]/profile/delete-account.tsx index 2e92b2b317..fb2b3b8b7d 100644 --- a/pages/sites/[slug]/[locale]/profile/delete-account.tsx +++ b/pages/sites/[slug]/[locale]/profile/delete-account.tsx @@ -1,6 +1,7 @@ import type { ReactElement } from 'react'; import type { AbstractIntlMessages } from 'next-intl'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, diff --git a/pages/sites/[slug]/[locale]/profile/register-trees.tsx b/pages/sites/[slug]/[locale]/profile/register-trees.tsx index b50155b466..58ccebbe67 100644 --- a/pages/sites/[slug]/[locale]/profile/register-trees.tsx +++ b/pages/sites/[slug]/[locale]/profile/register-trees.tsx @@ -1,6 +1,7 @@ import type { ReactElement } from 'react'; import type { AbstractIntlMessages } from 'next-intl'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, diff --git a/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx b/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx index 3c2888a46b..599faef0dd 100644 --- a/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx +++ b/pages/sites/[slug]/[locale]/vto-fitness-challenge-2023.tsx @@ -5,6 +5,7 @@ import type { import type { AbstractIntlMessages } from 'next-intl'; import type { Tenant } from '@planet-sdk/common'; import type { + GetStaticPaths, GetStaticProps, GetStaticPropsContext, GetStaticPropsResult, From 3a860095e69e22d7f965a58cb668f3a1be0091c3 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:01:01 +0530 Subject: [PATCH 13/14] address the feedbacks --- .../components/ProjectMedia.tsx | 24 +++++++++---------- src/features/user/ManageProjects/index.tsx | 12 +++++----- src/utils/apiRequests/api.ts | 18 +++++++------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/features/user/ManageProjects/components/ProjectMedia.tsx b/src/features/user/ManageProjects/components/ProjectMedia.tsx index fdb2e3515a..a843787272 100644 --- a/src/features/user/ManageProjects/components/ProjectMedia.tsx +++ b/src/features/user/ManageProjects/components/ProjectMedia.tsx @@ -176,13 +176,13 @@ export default function ProjectMedia({ try { const res = await putAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >( - tenantConfig?.id, - `/app/projects/${projectGUID}`, - submitData, + >({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setProjectDetails(res); setIsUploadingData(false); handleNext(ProjectCreationTabs.DETAILED_ANALYSIS); @@ -200,13 +200,13 @@ export default function ProjectMedia({ }; try { - await putAuthenticatedRequest( - tenantConfig?.id, - `/app/projects/${projectGUID}/images/${id}`, - submitData, + await putAuthenticatedRequest({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}/images/${id}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); const tempUploadedData = uploadedImages; tempUploadedData.forEach((image) => { image.isDefault = false; diff --git a/src/features/user/ManageProjects/index.tsx b/src/features/user/ManageProjects/index.tsx index 082227ad7f..4409cb1453 100644 --- a/src/features/user/ManageProjects/index.tsx +++ b/src/features/user/ManageProjects/index.tsx @@ -129,13 +129,13 @@ export default function ManageProjects({ try { const res = await putAuthenticatedRequest< ProfileProjectTrees | ProfileProjectConservation - >( - tenantConfig?.id, - `/app/projects/${projectGUID}`, - submitData, + >({ + tenant: tenantConfig?.id, + url: `/app/projects/${projectGUID}`, + data: submitData, token, - logoutUser - ); + logoutUser, + }); setProjectDetails(res); setIsUploadingData(false); } catch (err) { diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 1bf620fa5f..446af5e31b 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -13,9 +13,9 @@ interface BaseRequestOptions { url: string; } -interface GetAccountInfo { - tenant: string | undefined; +interface GetAccountInfoOptions { token: string | null; + tenant: string | undefined; impersonationData?: ImpersonationData; } @@ -26,9 +26,9 @@ interface GetAuthRequestOptions extends BaseRequestOptions { queryParams?: { [key: string]: string }; version?: string; } -interface PostAuthRequestOptions extends BaseRequestOptions { - data: any; +interface PostAuthRequestOptions extends BaseRequestOptions { token: string | null; + data: T; logoutUser: (value?: string | undefined) => void; headers?: Record; } @@ -36,13 +36,13 @@ interface DeleteAuthRequestOptions extends BaseRequestOptions { token: string | null; logoutUser: (value?: string | undefined) => void; } -interface PutAuthRequestOptions extends BaseRequestOptions { - data?: any; +interface PutAuthRequestOptions extends BaseRequestOptions { token: string | null; + data?: T; logoutUser: (value?: string | undefined) => void; } -interface PostRequestOptions extends BaseRequestOptions { - data: any; +interface PostRequestOptions extends BaseRequestOptions { + data: T; } interface GetRequestOptions extends BaseRequestOptions { queryParams?: { [key: string]: string }; @@ -53,7 +53,7 @@ export async function getAccountInfo({ tenant, token, impersonationData, -}: GetAccountInfo): Promise { +}: GetAccountInfoOptions): Promise { const lang = localStorage.getItem('language') || 'en'; const header = { 'tenant-key': `${tenant}`, From b578413889b772252217800e0bda44e971b9afb3 Mon Sep 17 00:00:00 2001 From: sunilsabatp <101264823+sunilsabatp@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:30:05 +0530 Subject: [PATCH 14/14] address the feedbacks --- src/utils/apiRequests/api.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/utils/apiRequests/api.ts b/src/utils/apiRequests/api.ts index 446af5e31b..8dba539abb 100644 --- a/src/utils/apiRequests/api.ts +++ b/src/utils/apiRequests/api.ts @@ -26,9 +26,9 @@ interface GetAuthRequestOptions extends BaseRequestOptions { queryParams?: { [key: string]: string }; version?: string; } -interface PostAuthRequestOptions extends BaseRequestOptions { +interface PostAuthRequestOptions extends BaseRequestOptions { token: string | null; - data: T; + data: any; logoutUser: (value?: string | undefined) => void; headers?: Record; } @@ -36,13 +36,13 @@ interface DeleteAuthRequestOptions extends BaseRequestOptions { token: string | null; logoutUser: (value?: string | undefined) => void; } -interface PutAuthRequestOptions extends BaseRequestOptions { +interface PutAuthRequestOptions extends BaseRequestOptions { token: string | null; - data?: T; + data?: any; logoutUser: (value?: string | undefined) => void; } -interface PostRequestOptions extends BaseRequestOptions { - data: T; +interface PostRequestOptions extends BaseRequestOptions { + data: any; } interface GetRequestOptions extends BaseRequestOptions { queryParams?: { [key: string]: string };