Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | 63x 63x 2x 63x 63x 63x 63x 13x 6x 63x 1x 63x 63x 63x 63x 63x 63x 63x 22x 22x 41x | import TaskCard from "@features/Merchants/MerchantSidePanel/UnderwritingTasks/components/TaskCard";
import { Stack, Box } from "@mui/material";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { CircleNotchIcon, WarningIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import GiveButton from "@shared/Button/GiveButton";
import JSONViewer from "@components/Merchants/MerchantPreview/modals/shared/JSONViewer";
import { useRequestMastercardAPI } from "../hooks/useMastercardAPI";
import { useMatchTranslation } from "../hooks/useMatchTranslation";
import TranslateWithAIButton from "./TranslateWithAIButton";
import MatchAITranslationPanel from "./MatchAITranslationPanel";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useEffect } from "react";
import { isEmpty } from "lodash";
interface Props {
merchantID: number;
title?: string;
response?: Record<string, any> | "NONE";
setValue?: (val: any) => void;
// A summary already persisted for this MATCH (surfaced on saved reports via
// the report read-back). Undefined in the create flow.
existingSummary?: string | null;
}
export default function MastercardResponse({
title = "Response",
response,
merchantID,
setValue,
existingSummary,
}: Props) {
const { isMatchAITranslationEnabled } = useGetFeatureFlagValues();
const handleSuccess = (response: any) => {
Eif (setValue) setValue(response);
};
const { mutate, data, error, isLoading } = useRequestMastercardAPI({
onSuccessCallback: handleSuccess,
});
const isMastercardError = `${error?.response?.status}`.startsWith("5"); //TODO: based on endpoint response //TODO: take error from API response
const isErrorOrLoading = isMastercardError || isLoading;
useEffect(() => {
if (!!merchantID && !response) {
mutate(merchantID);
}
}, [merchantID]);
const handleRerunCheck = () => {
mutate(merchantID);
};
// The MATCH JSON currently on screen: the live inquiry result once it
// resolves, else a saved report's matchResult. Undefined while loading, on a
// Mastercard error, or when the "Response" is an error body — so the Translate
// button is not offered in those cases (AC013).
const savedResponse: Record<string, unknown> | undefined =
response && response !== "NONE" && !isEmpty(response) ? response : undefined;
const matchResultForTranslation: Record<string, unknown> | undefined =
data ?? savedResponse;
const translation = useMatchTranslation({
merchantID,
matchResult: matchResultForTranslation,
existingSummary,
});
const showTranslateButton =
isMatchAITranslationEnabled &&
!isErrorOrLoading &&
translation.status === "idle" &&
translation.canTranslate;
const showTranslationPanel =
isMatchAITranslationEnabled && translation.status !== "idle";
Iif (!!response && isEmpty(response)) return null;
return (
<Stack gap="20px">
<TaskCard
title={title}
sx={isErrorOrLoading ? undefined : { padding: 0 }}
actions={
showTranslateButton
? [
{
element: (
<TranslateWithAIButton onClick={translation.translate} />
),
},
]
: undefined
}
>
{isErrorOrLoading ? (
<EmptyOrErrorContent
error={!isLoading ? "Internal Server Error" : undefined}
handleRerunCheck={handleRerunCheck}
/>
) : (
<StyledJSONContainer>
<JSONViewer
response={
!isMastercardError && !data && (!response || isEmpty(response))
? error?.response?.data
: data || response
}
/>
</StyledJSONContainer>
)}
</TaskCard>
{showTranslationPanel && (
<MatchAITranslationPanel
status={translation.status}
summary={translation.summary}
onRetry={translation.translate}
canRetry={translation.canRetry}
/>
)}
</Stack>
);
}
function EmptyOrErrorContent({
error,
handleRerunCheck,
}: {
error?: string;
handleRerunCheck: () => void;
}) {
const { palette } = useAppTheme();
return (
<Stack gap="20px" alignItems="center" justifyContent="center">
<Box
padding="8px"
bgcolor={palette.primitive?.transparent["darken-5"]}
borderRadius="30px"
width="36px"
height="36px"
>
{error ? <WarningIcon size="20px" /> : <CircleNotchIcon size="20px" />}
</Box>
<Stack gap="8px">
<GiveText variant="bodyS" color="secondary" textAlign="center">
{error ? "API Error" : "API check is in progress"}
</GiveText>
<GiveText variant="bodyXS" color="secondary" textAlign="center">
{error ? `${error}` : "This may take a few minutes"}
</GiveText>
</Stack>
{!!error && (
<GiveButton
variant="filled"
size="small"
label="Rerun Check"
onClick={handleRerunCheck}
/>
)}{" "}
</Stack>
);
}
const StyledJSONContainer = styled(Box)(({ theme }) => ({
"& textarea": {
overflow: "scroll !important",
minHeight: "10px",
maxHeight: "337px",
width: "100%",
color: theme.palette.text.primary,
resize: "none",
fontSize: "14px",
fontWeight: "400",
lineHeight: "20px",
outline: "none",
padding: "20px",
border: 0,
cursor: "default",
borderRadius: "20px",
},
}));
|