Skip to content

Commit

Permalink
Merge branch 'develop' into feature/new_header
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitb35 committed Aug 8, 2024
2 parents ca96446 + 78bc187 commit d421756
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
27 changes: 17 additions & 10 deletions pages/sites/[slug]/[locale]/profile/bulk-codes/[method]/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Head from 'next/head';
import { BulkCodeMethods } from '../../../../../../../src/utils/constants/bulkCodeConstants';
import { useBulkCode } from '../../../../../../../src/features/common/Layout/BulkCodeContext';
import { ErrorHandlingContext } from '../../../../../../../src/features/common/Layout/ErrorHandlingContext';
import { getRequest } from '../../../../../../../src/utils/apiRequests/api';
import { getAuthenticatedRequest } from '../../../../../../../src/utils/apiRequests/api';
import { useRouter } from 'next/router';
import { AbstractIntlMessages, useTranslations } from 'next-intl';
import { handleError, APIError } from '@planet-sdk/common';
Expand All @@ -26,6 +26,7 @@ import {
} from 'next';
import { defaultTenant } from '../../../../../../../tenant.config';
import getMessagesForPage from '../../../../../../../src/utils/language/getMessagesForPage';
import { useUserProps } from '../../../../../../../src/features/common/Layout/UserPropsContext';

interface Props {
pageProps: PageProps;
Expand All @@ -41,20 +42,26 @@ export default function BulkCodeIssueCodesPage({

const { project, setProject, bulkMethod, setBulkMethod, planetCashAccount } =
useBulkCode();
const { token, user, logoutUser, contextLoaded } = useUserProps();

// Checks context and sets project, bulk method if not already set within context
const checkContext = useCallback(async () => {
if (planetCashAccount) {
if (planetCashAccount && token && contextLoaded) {
if (!project) {
if (router.isReady) {
try {
const paymentOptions = await getRequest<PaymentOptions>(
pageProps.tenantConfig.id,
`/app/paymentOptions/${router.query.id}`,
{
currency: planetCashAccount.country,
}
);
const paymentOptions =
await getAuthenticatedRequest<PaymentOptions>(
pageProps.tenantConfig.id,
`/app/paymentOptions/${router.query.id}`,
token,
logoutUser,
undefined,
{
country: planetCashAccount.country,
...(user !== null && { legacyPriceFor: user.id }),
}
);

if (paymentOptions) {
const _project = {
Expand Down Expand Up @@ -92,7 +99,7 @@ export default function BulkCodeIssueCodesPage({
}
}
}
}, [router.isReady, planetCashAccount]);
}, [router.isReady, planetCashAccount, token, contextLoaded]);

React.useEffect(() => {
if (router.isReady) {
Expand Down
12 changes: 9 additions & 3 deletions src/features/user/BulkCodes/components/ProjectSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { ReactElement } from 'react';
import { getRequest } from '../../../../utils/apiRequests/api';
import { getAuthenticatedRequest } from '../../../../utils/apiRequests/api';
import { PlanetCashAccount } from '../../../common/Layout/BulkCodeContext';
import { ErrorHandlingContext } from '../../../common/Layout/ErrorHandlingContext';
import { PaymentOptions } from '../BulkCodesTypes';
Expand All @@ -8,6 +8,7 @@ import { useTenant } from '../../../common/Layout/TenantContext';
import ProjectSelectAutocomplete from './ProjectSelectAutocomplete';
import UnitCostDisplay from './UnitCostDisplay';
import { handleError, APIError } from '@planet-sdk/common';
import { useUserProps } from '../../../common/Layout/UserPropsContext';

interface ProjectSelectorProps {
projectList: ProjectOption[];
Expand All @@ -31,21 +32,26 @@ const ProjectSelector = ({

return 'tree';
};
const { user, token, logoutUser, contextLoaded } = useUserProps();

const fetchPaymentOptions = async (guid: string) => {
const paymentOptions = await getRequest<PaymentOptions>(
const paymentOptions = await getAuthenticatedRequest<PaymentOptions>(
`${tenantConfig?.id}`,
`/app/paymentOptions/${guid}`,
token,
logoutUser,
undefined,
{
country: planetCashAccount?.country || '',
...(user !== null && { legacyPriceFor: user.id }),
}
);
return paymentOptions;
};

const handleProjectChange = async (project: ProjectOption | null) => {
// fetch project details
if (project) {
if (project && user && token && contextLoaded) {
try {
const paymentOptions = await fetchPaymentOptions(project.guid);
// Add to project object
Expand Down

0 comments on commit d421756

Please sign in to comment.