Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: support for grouppasses #49

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/orval.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default defineConfig({
uitpas: {
input: {
target:
"https://stoplight.io/api/v1/projects/publiq/uitpas/nodes/reference/uitpas.json?deref=optimizedBundle",
"https://stoplight.io/api/v1/projects/publiq/uitpas/nodes/reference/uitpas.json?deref=optimizedBundle&branch=unreleased",
},
output: {
mode: "tags-split",
Expand Down
48 changes: 48 additions & 0 deletions src/mobile/feature-saving/components/GroupPass.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use client";

import { Grouppass } from "@/shared/lib/dataAccess";
import { useTranslation } from "@/shared/lib/utils/hooks";
import { Stack, Typography, useTheme } from "@mui/material";
import { OpportunityStateGrouppass } from "./OpportunityStateGrouppass";
import { Alert } from "@/mobile/lib/ui";

type GroupPassProps = {
groupPass: Grouppass;
alertData?: {
alertType: "error" | "success";
message?: string;
};
firstCardEntry: boolean;
};

export const GroupPass = ({
groupPass,
alertData,
firstCardEntry,
}: GroupPassProps) => {
const { t } = useTranslation();
const theme = useTheme();

return (
<Stack
sx={{
rowGap: "10px",
}}
>
<Typography variant="h1">{t("saving.mobile.grouppass")}</Typography>

<Stack sx={{ rowGap: "4px" }}>
<Typography variant="h1" sx={{ color: theme.palette.neutral[900] }}>
{groupPass.name}
</Typography>
<OpportunityStateGrouppass groupPass={groupPass} />
</Stack>

{alertData && (
<Alert type={alertData.alertType} newAlert={!firstCardEntry}>
{alertData.message}
</Alert>
)}
</Stack>
);
};
179 changes: 96 additions & 83 deletions src/mobile/feature-saving/components/MobileSavingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,37 @@ import {
useGetPassholders,
usePostTicketSales,
usePostRewardsRedeemed,
useGetGrouppasses,
} from "@/shared/lib/dataAccess";
import { MobileNavBar } from "@/mobile/layouts";
import {
ActivitySwitcher,
Alert,
Button,
MobileContentStack,
OutlinedButton,
UitpasLoading,
} from "@/mobile/lib/ui";
import {
ScanFailed,
OpportunityState,
TariffDrawer,
RewardsDrawer,
} from "@/mobile/feature-saving";
import { Stack, Typography, Divider, useTheme } from "@mui/material";
import { useActivity } from "@/mobile/feature-activities/useActivity";
import { ElementRef, useEffect, useRef, useState } from "react";
import { ManualCardInput } from "@/mobile/feature-identification";
import { formatUitpasNumber } from "@/shared/lib/utils/stringUtils";
import { usePostCheckins } from "@/shared/lib/dataAccess/uitpas/generated/checkins/checkins";
import { getUuid } from "@/shared/lib/utils";
import { useTranslation } from "@/shared/lib/utils/hooks";
import { PassHolder } from "./PassHolder";
import { GroupPass } from "./GroupPass";

export const MobileSavingPage = () => {
const { t, LANG_KEY } = useTranslation();
const params = useSearchParams();
const theme = useTheme();
const uitpasNumber = params.get("uitpas");
const inszNumber = params.get("insz");
const uitpasNumber = params.get("uitpas") ?? undefined;
const inszNumber = params.get("insz") ?? undefined;
const { selectedActivity, navigateToScanner } = useActivity();
const [showTariffDrawer, setShowTariffDrawer] = useState<boolean>(false);
const [showRewardsDrawer, setShowRewardsDrawer] = useState<boolean>(false);
Expand All @@ -52,6 +52,7 @@ export const MobileSavingPage = () => {
}
| undefined
>(undefined);
const [isGroupPass, setIsGroupPass] = useState<boolean>(false);

const {
data: passHoldersData,
Expand All @@ -64,6 +65,16 @@ export const MobileSavingPage = () => {
...(inszNumber && { inszNumber }),
});

const { data: groupPassHolder, isLoading: isGroupPassLoading } =
useGetGrouppasses(
{ uitpasNumber: uitpasNumber },
{
query: {
enabled: isGroupPass && !!uitpasNumber,
},
}
);

const { mutateAsync: postCheckin, status: checkinStatus } = usePostCheckins({
mutation: {
onSuccess: () => {
Expand Down Expand Up @@ -93,7 +104,7 @@ export const MobileSavingPage = () => {
setAlertData({
alertType: "success",
message: t("saving.mobile.tariff.discountRegistered", {
price: data.data.at(0)?.tariff.price,
price: (data.data.at(0)?.tariff?.price ?? 0) * data.data.length,
}),
});
},
Expand Down Expand Up @@ -142,18 +153,27 @@ export const MobileSavingPage = () => {
setShowRewardsDrawer(true);
};

const handleTicketSaleMutation = (tariffId: string, regularPrice: number) => {
if (!passHoldersData?.data?.member || !selectedActivity) return;
const handleTicketSaleMutation = (
tariffId: string,
regularPrice: number,
count?: number
) => {
if ((!isGroupPass && !passHoldersData?.data?.member) || !selectedActivity)
return;

const uitpasNumber =
passHoldersData.data.member[0].uitpasNumber ??
passHoldersData.data.member[0].cardSystemMemberships?.at(0)?.uitpasNumber;
const uitpasNumber = isGroupPass
? groupPassHolder?.data.member?.[0]?.uitpasNumber
: passHoldersData?.data.member?.[0]?.uitpasNumber ??
passHoldersData?.data.member?.[0]?.cardSystemMemberships?.[0]
?.uitpasNumber;

if (!uitpasNumber || !selectedActivity["@id"]) return;

if (!uitpasNumber) return;
if (!count) count = 1;

postTicketSale({
data: Array.of({
eventId: getUuid(selectedActivity?.["@id"] ?? ""),
data: Array(count).fill({
eventId: getUuid(selectedActivity["@id"]),
tariff: {
id: tariffId,
},
Expand Down Expand Up @@ -182,7 +202,10 @@ export const MobileSavingPage = () => {
};

useEffect(() => {
if (passHoldersData?.data?.member) {
if (
passHoldersData?.data?.member &&
passHoldersData.data.member.length > 0
) {
const uitpasNumber =
passHoldersData.data.member[0].uitpasNumber ??
passHoldersData.data.member[0].cardSystemMemberships?.at(0)
Expand All @@ -209,11 +232,21 @@ export const MobileSavingPage = () => {
selectedActivity,
]);

useEffect(() => {
if (
passHoldersData?.data.member &&
passHoldersData.data.member.length === 0
) {
setIsGroupPass(true);
}
}, [passHoldersData]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afgeleide van data bijhouden in local state is een anti-pattern in React, het is beter gewoon een lokale variabele te voorzien

const isGroupPass = passHoldersData?.data.member && passHoldersData.data.member.length === 0


if (
isPassholdersLoading ||
isCheckinLoading ||
isTicketSaleLoading ||
isRewardsRedeemedLoading
isRewardsRedeemedLoading ||
isGroupPassLoading
)
return (
<MobileNavBar>
Expand All @@ -238,52 +271,20 @@ export const MobileSavingPage = () => {
</Typography>
<ActivitySwitcher ref={activityRef} />

{passHoldersData?.data.member?.[0] && (
<Stack sx={{ rowGap: "10px" }}>
<Stack
sx={{
flexDirection: "row",
columnGap: "5px",
alignItems: "center",
}}
>
<Typography variant="h1">
{t("saving.mobile.passholder")}
</Typography>
<Typography
sx={{
color: theme.palette.brand[800],
fontSize: "13px",
}}
>
{formatUitpasNumber(
passHoldersData.data.member
.at(0)
?.cardSystemMemberships?.at(0)?.uitpasNumber
)}
</Typography>
</Stack>

<Stack sx={{ rowGap: "4px" }}>
<Typography
variant="h1"
sx={{ color: theme.palette.neutral[900] }}
>
{t("saving.mobile.namePointsTxt", {
firstName: passHoldersData.data.member[0].firstName,
lastName: passHoldersData.data.member[0].name,
points: passHoldersData.data.member[0].points,
})}
</Typography>
<OpportunityState passholder={passHoldersData.data.member[0]} />
</Stack>

{alertData && (
<Alert type={alertData.alertType} newAlert={!firstCardEntry}>
{alertData.message}
</Alert>
)}
</Stack>
{passHoldersData?.data.member?.[0] ? (
<PassHolder
passholder={passHoldersData?.data.member?.[0]}
firstCardEntry={firstCardEntry}
alertData={alertData}
/>
) : (
groupPassHolder?.data.member?.[0] && (
<GroupPass
groupPass={groupPassHolder?.data.member?.[0]}
firstCardEntry={firstCardEntry}
alertData={alertData}
/>
)
)}

<Stack rowGap="10px" sx={{ marginTop: "-10px" }}>
Expand All @@ -292,9 +293,11 @@ export const MobileSavingPage = () => {
{t("saving.mobile.chooseTariffBtn")}
</OutlinedButton>
)}
<OutlinedButton onClick={handleChooseBenefitClick}>
{t("saving.mobile.tradeBenefitBtn")}
</OutlinedButton>
{!isGroupPass && (
<OutlinedButton onClick={handleChooseBenefitClick}>
{t("saving.mobile.tradeBenefitBtn")}
</OutlinedButton>
)}
</Stack>

{/*-16 comes from the padding that's already in the stack*/}
Expand All @@ -313,43 +316,53 @@ export const MobileSavingPage = () => {
</Typography>

<ManualCardInput firstCardEntry={false} />

{selectedActivity &&
selectedActivity["@id"] &&
passHoldersData?.data?.member &&
activityRef.current && (
<TariffDrawer
eventId={selectedActivity["@id"]}
passHolderName={
passHoldersData.data.member
isGroupPass
? groupPassHolder?.data.member?.[0]?.name ?? undefined
: passHoldersData.data.member
? `${passHoldersData.data.member[0].firstName} ${passHoldersData.data.member[0].name}`
: undefined
}
isOpen={showTariffDrawer}
setIsOpen={setShowTariffDrawer}
startPosition={activityRef.current.getBoundingClientRect().bottom}
uitpasNumber={
passHoldersData.data.member?.at(0)?.cardSystemMemberships?.at(0)
?.uitpasNumber!
isGroupPass
? groupPassHolder?.data.member?.at(0)?.uitpasNumber!
: passHoldersData.data.member
?.at(0)
?.cardSystemMemberships?.at(0)?.uitpasNumber!
}
ticketSaleMutation={handleTicketSaleMutation}
isGroupPass={isGroupPass}
/>
)}

{activityRef.current && passHoldersData?.data?.member && (
<RewardsDrawer
isOpen={showRewardsDrawer}
setIsOpen={setShowRewardsDrawer}
startPosition={activityRef.current.getBoundingClientRect().bottom}
passHolderId={passHoldersData.data.member[0].id}
passHolderName={
passHoldersData.data.member
? `${passHoldersData.data.member[0].firstName} ${passHoldersData.data.member[0].name}`
: undefined
}
passHolderPoints={passHoldersData.data.member[0].points ?? 0}
rewardRedemptionMutation={handleRewardRedemption}
/>
)}
{/* Grouppass holders can't claim rewards, so this drawer will not render with grouppasses */}
{!isGroupPass &&
activityRef.current &&
passHoldersData?.data?.member && (
<RewardsDrawer
isOpen={showRewardsDrawer}
setIsOpen={setShowRewardsDrawer}
startPosition={activityRef.current.getBoundingClientRect().bottom}
passHolderId={passHoldersData.data.member[0].id}
passHolderName={
passHoldersData.data.member
? `${passHoldersData.data.member[0].firstName} ${passHoldersData.data.member[0].name}`
: undefined
}
passHolderPoints={passHoldersData.data.member[0].points ?? 0}
rewardRedemptionMutation={handleRewardRedemption}
/>
)}
</MobileContentStack>
</MobileNavBar>
);
Expand Down
8 changes: 3 additions & 5 deletions src/mobile/feature-saving/components/OpportunityStateCard.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { PropsWithChildren, useState } from "react";
import { Box, BoxProps, IconButton, useTheme } from "@mui/material";
import { useTranslation } from "@/shared/lib/i18n/client";
import { ExpandMore, ExpandLess } from "@mui/icons-material";

type OpportunityStateCardProps = PropsWithChildren &
BoxProps & {
title: string;
status: "ACTIVE" | "SUSPENDED" | "EXPIRED" | "BLOCKED";
};

export const OpportunityStateCard = ({
title,
status,
...props
}: OpportunityStateCardProps) => {
const { t } = useTranslation();
const theme = useTheme();
const [open, setOpen] = useState<boolean>(false);

Expand Down Expand Up @@ -54,9 +54,7 @@ export const OpportunityStateCard = ({
width: "100%",
}}
>
<p style={{ fontWeight: 700, fontSize: "18px", margin: 0 }}>
{t(`saving.mobile.opportunityState.${status.toLowerCase()}.title`)}
</p>
<p style={{ fontWeight: 700, fontSize: "18px", margin: 0 }}>{title}</p>
<IconButton
disableRipple
sx={{
Expand Down
Loading