Skip to content

Commit

Permalink
feat: separates pCashSignupDetails in context
Browse files Browse the repository at this point in the history
- creates new property `pCashSignupDetails` in context
- removes type `PlanetCashSignupDetails` from projectDetails in context
- uses`pCashSignupDetails` to display `PlanetCashSignup` (Donations/index)
- refactors LeftPanel & child components to use `pCashSignupDetails`
- renames `projectDetails` arg to `getTenantBackground` for clarity
  • Loading branch information
mohitb35 authored and Shreyaschorge committed Sep 11, 2023
1 parent 46c5570 commit 9d546e5
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 91 deletions.
11 changes: 5 additions & 6 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
SentGift,
FetchedProjectDetails,
PaymentOptions,
PlanetCashSignupDetails,
} from "src/Common/Types";
import {
Donation,
Expand All @@ -23,7 +22,7 @@ import {
import { GetServerSideProps } from "next/types";

interface Props {
projectDetails?: FetchedProjectDetails | PlanetCashSignupDetails;
projectDetails?: FetchedProjectDetails;
donationStep: number | null;
giftDetails: SentGift | null;
isGift: boolean;
Expand Down Expand Up @@ -129,9 +128,9 @@ function index({
settenant(tenant);
// If gift details are present, initialize gift in context
if (giftDetails && isGift) {
setGiftDetails(giftDetails);
setisGift(true);
}
setGiftDetails(giftDetails);
setisGift(true);
}
}, []);

// If project details are present set project details
Expand Down Expand Up @@ -251,7 +250,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 Down
8 changes: 5 additions & 3 deletions src/Common/Types/QueryParamContextInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ export default interface QueryParamContextInterface {
setEnabledCurrencies: Dispatch<SetStateAction<CurrencyList | null>>;
donationStep: number | null;
setdonationStep: Dispatch<SetStateAction<number | null>>;
projectDetails: FetchedProjectDetails | PlanetCashSignupDetails | null;
setprojectDetails: Dispatch<
SetStateAction<FetchedProjectDetails | PlanetCashSignupDetails | null>
projectDetails: FetchedProjectDetails | null;
setprojectDetails: Dispatch<SetStateAction<FetchedProjectDetails | null>>;
pCashSignupDetails: PlanetCashSignupDetails | null;
setPCashSignupDetails: Dispatch<
SetStateAction<PlanetCashSignupDetails | null>
>;
quantity: number;
setquantity: Dispatch<SetStateAction<number>>;
Expand Down
19 changes: 10 additions & 9 deletions src/Donations/LeftPanel/Avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import {
} from "src/Common/Types";
import getImageUrl from "src/Utils/getImageURL";
import styles from "./LeftPanel.module.scss";

interface Props {
projectDetails: FetchedProjectDetails | PlanetCashSignupDetails;
info: FetchedProjectDetails | PlanetCashSignupDetails;
}

const Avatar = ({ projectDetails }: Props): ReactElement => {
const Avatar = ({ info }: Props): ReactElement => {
const { ownerName, ownerAvatar } = info;

const renderAvatarImage = (
ownerName: string | null,
ownerAvatar: string | null
) => {
ownerAvatar: string | null,
): JSX.Element => {
return ownerAvatar ? (
<img
className={styles.avatar}
Expand All @@ -27,17 +28,17 @@ const Avatar = ({ projectDetails }: Props): ReactElement => {
);
};

return projectDetails.purpose === "trees" ? (
return info.purpose === "trees" ? (
<a
rel="noreferrer"
target="_blank"
href={`https://www.trilliontreecampaign.org/${projectDetails.id}`}
href={`https://www.trilliontreecampaign.org/${info.id}`}
className={styles["avatar-link"]}
>
{renderAvatarImage(projectDetails.ownerName, projectDetails.ownerAvatar)}
{renderAvatarImage(ownerName, ownerAvatar)}
</a>
) : (
renderAvatarImage(projectDetails.ownerName, projectDetails.ownerAvatar)
renderAvatarImage(ownerName, ownerAvatar)
);
};

Expand Down
11 changes: 4 additions & 7 deletions src/Donations/LeftPanel/LeftPanelContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ import { getTenantBackground } from "../../Utils/getTenantBackground";
import styles from "./LeftPanel.module.scss";

interface Props {
projectDetails?: FetchedProjectDetails | PlanetCashSignupDetails | null;
info?: FetchedProjectDetails | PlanetCashSignupDetails | null;
donationStep?: number | null;
tenant: string;
}

const LeftPanelContainer: FC<Props> = ({
projectDetails,
info = null,
donationStep,
tenant,
children,
}) => {
const getBackgroundImage = () => {
if (projectDetails || donationStep === 0) {
return `url(${getTenantBackground(
tenant,
projectDetails as FetchedProjectDetails | null
)})`;
if (info || donationStep === 0) {
return `url(${getTenantBackground(tenant, info)})`;
}
return "none";
};
Expand Down
21 changes: 12 additions & 9 deletions src/Donations/LeftPanel/LeftPanelInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "src/Common/Types";
import Avatar from "./Avatar";
import TransactionSummary from "./TransactionSummary";
import ProjectTitle from "./ProjectTitle";
import SummaryTitle from "./SummaryTitle";
import ProjectInfo from "./ProjectInfo";
import styles from "./LeftPanel.module.scss";
import GiftInfo from "./GiftInfo";
Expand All @@ -21,6 +21,7 @@ import ContactDetailsInfo from "./ContactDetailsInfo";
interface Props {
isMobile: boolean;
projectDetails: FetchedProjectDetails | PlanetCashSignupDetails | null;
pCashSignupDetails: PlanetCashSignupDetails | null;
donationStep: number | null;
donationID: string | null;
paymentSetup: PaymentOptions | null;
Expand All @@ -40,6 +41,7 @@ interface Props {
const LeftPanelInfo = ({
isMobile,
projectDetails,
pCashSignupDetails,
donationStep,
donationID,
paymentSetup,
Expand All @@ -58,10 +60,11 @@ const LeftPanelInfo = ({
const { i18n } = useTranslation("common");
const router = useRouter();

const info = projectDetails || pCashSignupDetails;
const canShowAvatar = donationStep !== null && donationStep > 0;
const canShowTransactionSummary =
paymentSetup !== null && (donationStep === 2 || donationStep === 3);
const canShowProject = donationStep !== null && donationStep > 0;
const canShowSummary = donationStep !== null && donationStep > 0;
const canShowGift =
(donationStep === 1 || donationStep === 2 || donationStep === 3) &&
giftDetails.type !== null &&
Expand All @@ -78,9 +81,9 @@ const LeftPanelInfo = ({
const canShowDonationLink =
donationID !== null && !(isMobile && router.query.step === "thankyou");

return projectDetails ? (
return info !== null ? (
<div className={styles["left-panel-info"]}>
{canShowAvatar && <Avatar projectDetails={projectDetails} />}
{canShowAvatar && <Avatar info={info} />}
{canShowTransactionSummary && (
<TransactionSummary
currency={currency}
Expand All @@ -89,11 +92,11 @@ const LeftPanelInfo = ({
paymentSetup={paymentSetup}
/>
)}
{canShowProject && (
<div className={styles["project-info-container"]}>
<ProjectTitle projectDetails={projectDetails} />
{projectDetails.purpose !== "planet-cash-signup" && (
<ProjectInfo projectDetails={projectDetails} />
{canShowSummary && (
<div className={styles["summary-container"]}>
<SummaryTitle info={info} />
{info.purpose !== "planet-cash-signup" && (
<ProjectInfo projectDetails={info} />
)}
</div>
)}
Expand Down
38 changes: 0 additions & 38 deletions src/Donations/LeftPanel/ProjectTitle.tsx

This file was deleted.

36 changes: 36 additions & 0 deletions src/Donations/LeftPanel/SummaryTitle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ReactElement } from "react";
import {
FetchedProjectDetails,
PlanetCashSignupDetails,
} from "src/Common/Types";
import VerifiedBadge from "./VerifiedBadge";
import styles from "./LeftPanel.module.scss";

interface Props {
info: FetchedProjectDetails | PlanetCashSignupDetails;
}

const ProjectTitle = ({ info }: Props): ReactElement => {
const isLinked = info.purpose === "trees" || info.purpose === "conservation";

return (
<h1 className={`${styles["project-title"]} title-text`}>
{isLinked ? (
<a
rel="noreferrer"
target="_blank"
href={`https://www.trilliontreecampaign.org/${info.id}`}
>
{info.name + " "}
</a>
) : (
<>{info.name + " "}</>
)}
{info.purpose !== "planet-cash-signup" &&
info.name !== null &&
info.isApproved && <VerifiedBadge />}
</h1>
);
};

export default ProjectTitle;
4 changes: 3 additions & 1 deletion src/Donations/LeftPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CancelButton from "../../Common/CancelButton";
function LeftPanel(): ReactElement {
const {
projectDetails,
pCashSignupDetails,
donationID,
donationStep,
quantity,
Expand Down Expand Up @@ -48,7 +49,7 @@ function LeftPanel(): ReactElement {

return (
<LeftPanelContainer
projectDetails={projectDetails}
info={pCashSignupDetails || projectDetails}
donationStep={donationStep}
tenant={tenant}
>
Expand All @@ -61,6 +62,7 @@ function LeftPanel(): ReactElement {
{/* TODO - evaluate whether to send this info to LeftPanelInfo, or use context instead */}
<LeftPanelInfo
projectDetails={projectDetails}
pCashSignupDetails={pCashSignupDetails}
donationStep={donationStep}
donationID={donationID}
paymentSetup={paymentSetup}
Expand Down
32 changes: 26 additions & 6 deletions src/Donations/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {
SELECT_PROJECT,
THANK_YOU,
} from "src/Utils/donationStepConstants";
import PlanetCashSignup from "./Micros/PlanetCashSignup";

function Donations(): ReactElement {
const router = useRouter();

const { donationStep, setdonationStep } = React.useContext(QueryParamContext);
const { donationStep, setdonationStep, pCashSignupDetails } =
React.useContext(QueryParamContext);

useEffect(() => {
if (router.query?.step) {
Expand Down Expand Up @@ -55,18 +57,36 @@ function Donations(): ReactElement {
}
return () => {};
}, [router.query.step]);

const renderDonationStep = (step: number | null) => {
switch (step) {
case 0:
return <SelectProject />;
case 1:
return <DonationsForm />;
case 2:
return <ContactsForm />;
case 3:
return <PaymentsForm />;
case 4:
return <PaymentStatus />;
default:
return null;
}
};

return (
<div className="donations-container">
<div className="donations-card-container">
{/* Left panel */}
<LeftPanel />

{/* Right panel */}
{donationStep === 0 && <SelectProject />}
{donationStep === 1 && <DonationsForm />}
{donationStep === 2 && <ContactsForm />}
{donationStep === 3 && <PaymentsForm />}
{donationStep === 4 && <PaymentStatus />}
{pCashSignupDetails !== null ? (
<PlanetCashSignup />
) : (
renderDonationStep(donationStep)
)}
</div>
</div>
);
Expand Down
14 changes: 9 additions & 5 deletions src/Layout/QueryParamContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ const QueryParamProvider: FC = ({ children }) => {

const [paymentSetup, setpaymentSetup] = useState<PaymentOptions | null>(null);

const [projectDetails, setprojectDetails] = useState<
FetchedProjectDetails | PlanetCashSignupDetails | null
>(null);
const [projectDetails, setprojectDetails] =
useState<FetchedProjectDetails | null>(null);
const [pCashSignupDetails, setPCashSignupDetails] =
useState<PlanetCashSignupDetails | null>(null);

// Query token is the access token which is passed in the query params
const [queryToken, setqueryToken] = useState<string | null>(null);
Expand Down Expand Up @@ -276,13 +277,14 @@ const QueryParamProvider: FC = ({ children }) => {
}, []);

const showPlanetCashSignUpScreen = () => {
setprojectDetails({
setprojectDetails(null);
setPCashSignupDetails({
name: `PlanetCash - ${profile?.displayName}`,
ownerName: profile?.displayName || "",
ownerAvatar: profile?.image || "",
purpose: "planet-cash-signup",
});
setdonationStep(4);
setdonationStep(null);
};

useEffect(() => {
Expand Down Expand Up @@ -543,6 +545,8 @@ const QueryParamProvider: FC = ({ children }) => {
setdonationStep,
projectDetails,
setprojectDetails,
pCashSignupDetails,
setPCashSignupDetails,
quantity,
setquantity,
language,
Expand Down
Loading

0 comments on commit 9d546e5

Please sign in to comment.