Skip to content

Commit

Permalink
feat: refactor project details handling with createProjectDetails uti…
Browse files Browse the repository at this point in the history
…lity

- also populates `classification` in projectDetails
  • Loading branch information
mohitb35 committed Nov 12, 2024
1 parent e80151a commit 330aeda
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
29 changes: 4 additions & 25 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ContactDetails,
} from "@planet-sdk/common/build/types/donation";
import { GetServerSideProps } from "next/types";
import { createProjectDetails } from "src/Utils/createProjectDetails";

interface Props {
projectDetails?: FetchedProjectDetails;
Expand Down Expand Up @@ -250,7 +251,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
const queryCountry = context.query.country;
const found = countriesData.some(
(country) =>
country.countryCode?.toUpperCase() === queryCountry.toUpperCase(),
country.countryCode?.toUpperCase() === queryCountry.toUpperCase()
);
if (found) {
country = queryCountry.toUpperCase();
Expand All @@ -276,18 +277,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
const paymentOptionsResponse = await apiRequest(requestParams);
const paymentOptionsData: PaymentOptions = paymentOptionsResponse?.data;
if (paymentOptionsData) {
projectDetails = {
id: paymentOptionsData.id,
name: paymentOptionsData.name,
description: paymentOptionsData.description,
purpose: paymentOptionsData.purpose,
ownerName: paymentOptionsData.ownerName,
taxDeductionCountries: paymentOptionsData.taxDeductionCountries,
image: paymentOptionsData.image,
ownerAvatar: paymentOptionsData.ownerAvatar,
isApproved: paymentOptionsData.isApproved ? true : false,
isTopProject: paymentOptionsData.isTopProject ? true : false,
};
projectDetails = createProjectDetails(paymentOptionsData);
donationStep = 1;
}
} catch (err) {
Expand Down Expand Up @@ -360,18 +350,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
if (paymentSetupData) {
currency = paymentSetupData.currency;
paymentSetup = paymentSetupData;
projectDetails = {
id: paymentSetupData.id,
name: paymentSetupData.name,
description: paymentSetupData.description,
purpose: paymentSetupData.purpose,
ownerName: paymentSetupData.ownerName,
taxDeductionCountries: paymentSetupData.taxDeductionCountries,
image: paymentSetupData.image,
ownerAvatar: paymentSetupData.ownerAvatar,
isApproved: paymentSetupData.isApproved ? true : false,
isTopProject: paymentSetupData.isTopProject ? true : false,
};
projectDetails = createProjectDetails(paymentSetupData);
donationStep = 3;
}
} catch (err) {
Expand Down
14 changes: 2 additions & 12 deletions src/Layout/QueryParamContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import ErrorPopup from "src/Common/ErrorPopup/ErrorPopup";
import { APIError, handleError, SerializedError } from "@planet-sdk/common";
import { PaymentRequest } from "@stripe/stripe-js/types/stripe-js/payment-request";
import { createProjectDetails } from "src/Utils/createProjectDetails";

export const QueryParamContext =
createContext<QueryParamContextInterface>(null);
Expand Down Expand Up @@ -509,18 +510,7 @@ const QueryParamProvider: FC = ({ children }) => {

setpaymentSetup(paymentSetup);
}
setprojectDetails({
id: paymentSetup.id,
name: paymentSetup.name,
description: paymentSetup.description,
purpose: paymentSetup.purpose,
ownerName: paymentSetup.ownerName,
taxDeductionCountries: paymentSetup.taxDeductionCountries,
image: paymentSetup.image,
ownerAvatar: paymentSetup.ownerAvatar,
isApproved: paymentSetup.isApproved ? true : false,
isTopProject: paymentSetup.isTopProject ? true : false,
});
setprojectDetails(createProjectDetails(paymentSetup));
}
setIsPaymentOptionsLoading(false);
} catch (err) {
Expand Down
38 changes: 38 additions & 0 deletions src/Utils/createProjectDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { FetchedProjectDetails, PaymentOptions } from "src/Common/Types";

export const createProjectDetails = (
paymentOptions: PaymentOptions
): FetchedProjectDetails => {
const baseDetails = {
id: paymentOptions.id,
name: paymentOptions.name,
description: paymentOptions.description,
ownerName: paymentOptions.ownerName,
taxDeductionCountries: paymentOptions.taxDeductionCountries,
image: paymentOptions.image,
ownerAvatar: paymentOptions.ownerAvatar,
isApproved: !!paymentOptions.isApproved,
isTopProject: !!paymentOptions.isTopProject,
};

switch (paymentOptions.purpose) {
case "trees":
return {
...baseDetails,
purpose: "trees",
classification: paymentOptions.classification,
};
case "funds":
return {
...baseDetails,
purpose: "funds",
classification: paymentOptions.classification,
};
default:
return {
...baseDetails,
purpose: paymentOptions.purpose,
classification: null,
};
}
};

0 comments on commit 330aeda

Please sign in to comment.