2023-01-10 09:40:46 +13:00
|
|
|
import * as React from 'react';
|
2023-01-31 07:10:45 +13:00
|
|
|
import {useContext, useEffect, useState} from 'react';
|
2023-01-10 09:40:46 +13:00
|
|
|
import Dialog from '@mui/material/Dialog';
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
2023-01-18 13:40:03 +13:00
|
|
|
import {Alert, CardActionArea, CardContent, ListItem, useMediaQuery} from "@mui/material";
|
2023-01-10 09:40:46 +13:00
|
|
|
import theme from "./theme";
|
|
|
|
import DialogFooter from "./DialogFooter";
|
2023-01-15 00:43:44 +13:00
|
|
|
import Button from "@mui/material/Button";
|
2023-02-03 09:19:37 +13:00
|
|
|
import accountApi from "../app/AccountApi";
|
2023-01-15 00:43:44 +13:00
|
|
|
import session from "../app/Session";
|
|
|
|
import routes from "./routes";
|
|
|
|
import Card from "@mui/material/Card";
|
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
import {AccountContext} from "./App";
|
2023-01-18 13:40:03 +13:00
|
|
|
import {formatBytes, formatNumber, formatShortDate} from "../app/utils";
|
|
|
|
import {Trans, useTranslation} from "react-i18next";
|
|
|
|
import List from "@mui/material/List";
|
|
|
|
import {Check} from "@mui/icons-material";
|
|
|
|
import ListItemIcon from "@mui/material/ListItemIcon";
|
|
|
|
import ListItemText from "@mui/material/ListItemText";
|
2023-01-18 14:21:19 +13:00
|
|
|
import Box from "@mui/material/Box";
|
2023-01-21 16:47:37 +13:00
|
|
|
import {NavLink} from "react-router-dom";
|
2023-02-03 09:19:37 +13:00
|
|
|
import {UnauthorizedError} from "../app/errors";
|
2023-01-10 09:40:46 +13:00
|
|
|
|
|
|
|
const UpgradeDialog = (props) => {
|
2023-01-17 10:35:37 +13:00
|
|
|
const { t } = useTranslation();
|
2023-01-19 07:46:40 +13:00
|
|
|
const { account } = useContext(AccountContext); // May be undefined!
|
2023-02-03 09:19:37 +13:00
|
|
|
const [error, setError] = useState("");
|
2023-01-18 04:09:37 +13:00
|
|
|
const [tiers, setTiers] = useState(null);
|
2023-01-21 16:47:37 +13:00
|
|
|
const [newTierCode, setNewTierCode] = useState(account?.tier?.code); // May be undefined
|
2023-01-18 14:21:19 +13:00
|
|
|
const [loading, setLoading] = useState(false);
|
2023-02-03 09:19:37 +13:00
|
|
|
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
|
2023-01-10 09:40:46 +13:00
|
|
|
|
2023-01-18 04:09:37 +13:00
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
2023-02-03 09:19:37 +13:00
|
|
|
try {
|
|
|
|
setTiers(await accountApi.billingTiers());
|
|
|
|
} catch (e) {
|
|
|
|
setError(e.message);
|
|
|
|
}
|
2023-01-18 04:09:37 +13:00
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
2023-01-19 07:46:40 +13:00
|
|
|
if (!tiers) {
|
2023-01-17 10:35:37 +13:00
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:47:37 +13:00
|
|
|
const tiersMap = Object.assign(...tiers.map(tier => ({[tier.code]: tier})));
|
|
|
|
const newTier = tiersMap[newTierCode]; // May be undefined
|
|
|
|
const currentTier = account?.tier; // May be undefined
|
|
|
|
const currentTierCode = currentTier?.code; // May be undefined
|
|
|
|
|
|
|
|
// Figure out buttons, labels and the submit action
|
|
|
|
let submitAction, submitButtonLabel, banner;
|
2023-01-19 07:46:40 +13:00
|
|
|
if (!account) {
|
|
|
|
submitButtonLabel = t("account_upgrade_dialog_button_redirect_signup");
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = Action.REDIRECT_SIGNUP;
|
|
|
|
banner = null;
|
|
|
|
} else if (currentTierCode === newTierCode) {
|
2023-01-18 13:40:03 +13:00
|
|
|
submitButtonLabel = t("account_upgrade_dialog_button_update_subscription");
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = null;
|
|
|
|
banner = (currentTierCode) ? Banner.PRORATION_INFO : null;
|
|
|
|
} else if (!currentTierCode) {
|
2023-01-18 13:40:03 +13:00
|
|
|
submitButtonLabel = t("account_upgrade_dialog_button_pay_now");
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = Action.CREATE_SUBSCRIPTION;
|
|
|
|
banner = null;
|
|
|
|
} else if (!newTierCode) {
|
2023-01-18 13:40:03 +13:00
|
|
|
submitButtonLabel = t("account_upgrade_dialog_button_cancel_subscription");
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = Action.CANCEL_SUBSCRIPTION;
|
|
|
|
banner = Banner.CANCEL_WARNING;
|
2023-01-17 10:35:37 +13:00
|
|
|
} else {
|
2023-01-18 13:40:03 +13:00
|
|
|
submitButtonLabel = t("account_upgrade_dialog_button_update_subscription");
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = Action.UPDATE_SUBSCRIPTION;
|
|
|
|
banner = Banner.PRORATION_INFO;
|
2023-01-17 10:35:37 +13:00
|
|
|
}
|
|
|
|
|
2023-01-21 16:47:37 +13:00
|
|
|
// Exceptional conditions
|
2023-01-18 14:21:19 +13:00
|
|
|
if (loading) {
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = null;
|
2023-01-22 02:55:31 +13:00
|
|
|
} else if (newTier?.code && account?.reservations?.length > newTier?.limits?.reservations) {
|
2023-01-21 16:47:37 +13:00
|
|
|
submitAction = null;
|
|
|
|
banner = Banner.RESERVATIONS_WARNING;
|
2023-01-18 14:21:19 +13:00
|
|
|
}
|
|
|
|
|
2023-01-17 10:35:37 +13:00
|
|
|
const handleSubmit = async () => {
|
2023-01-21 16:47:37 +13:00
|
|
|
if (submitAction === Action.REDIRECT_SIGNUP) {
|
2023-01-19 07:46:40 +13:00
|
|
|
window.location.href = routes.signup;
|
|
|
|
return;
|
|
|
|
}
|
2023-01-15 00:43:44 +13:00
|
|
|
try {
|
2023-01-18 14:21:19 +13:00
|
|
|
setLoading(true);
|
2023-01-21 16:47:37 +13:00
|
|
|
if (submitAction === Action.CREATE_SUBSCRIPTION) {
|
|
|
|
const response = await accountApi.createBillingSubscription(newTierCode);
|
2023-01-17 10:35:37 +13:00
|
|
|
window.location.href = response.redirect_url;
|
2023-01-21 16:47:37 +13:00
|
|
|
} else if (submitAction === Action.UPDATE_SUBSCRIPTION) {
|
|
|
|
await accountApi.updateBillingSubscription(newTierCode);
|
|
|
|
} else if (submitAction === Action.CANCEL_SUBSCRIPTION) {
|
2023-01-16 17:29:46 +13:00
|
|
|
await accountApi.deleteBillingSubscription();
|
2023-01-15 00:43:44 +13:00
|
|
|
}
|
2023-01-17 10:35:37 +13:00
|
|
|
props.onCancel();
|
2023-01-15 00:43:44 +13:00
|
|
|
} catch (e) {
|
2023-01-17 10:35:37 +13:00
|
|
|
console.log(`[UpgradeDialog] Error changing billing subscription`, e);
|
2023-02-03 09:19:37 +13:00
|
|
|
if (e instanceof UnauthorizedError) {
|
2023-01-15 00:43:44 +13:00
|
|
|
session.resetAndRedirect(routes.login);
|
2023-02-03 09:19:37 +13:00
|
|
|
} else {
|
|
|
|
setError(e.message);
|
2023-01-15 00:43:44 +13:00
|
|
|
}
|
2023-01-18 14:21:19 +13:00
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
2023-01-15 00:43:44 +13:00
|
|
|
}
|
2023-01-10 09:40:46 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-18 13:40:03 +13:00
|
|
|
<Dialog
|
|
|
|
open={props.open}
|
|
|
|
onClose={props.onCancel}
|
|
|
|
maxWidth="md"
|
|
|
|
fullWidth
|
|
|
|
fullScreen={fullScreen}
|
|
|
|
>
|
2023-01-18 04:09:37 +13:00
|
|
|
<DialogTitle>{t("account_upgrade_dialog_title")}</DialogTitle>
|
2023-01-10 09:40:46 +13:00
|
|
|
<DialogContent>
|
2023-01-15 00:43:44 +13:00
|
|
|
<div style={{
|
|
|
|
display: "flex",
|
2023-01-18 04:09:37 +13:00
|
|
|
flexDirection: "row",
|
|
|
|
marginBottom: "8px",
|
|
|
|
width: "100%"
|
2023-01-15 00:43:44 +13:00
|
|
|
}}>
|
2023-01-18 04:09:37 +13:00
|
|
|
{tiers.map(tier =>
|
|
|
|
<TierCard
|
2023-01-18 13:40:03 +13:00
|
|
|
key={`tierCard${tier.code || '_free'}`}
|
|
|
|
tier={tier}
|
2023-01-21 16:47:37 +13:00
|
|
|
current={currentTierCode === tier.code} // tier.code or currentTierCode may be undefined!
|
|
|
|
selected={newTierCode === tier.code} // tier.code may be undefined!
|
|
|
|
onClick={() => setNewTierCode(tier.code)} // tier.code may be undefined!
|
2023-01-18 04:09:37 +13:00
|
|
|
/>
|
|
|
|
)}
|
2023-01-15 00:43:44 +13:00
|
|
|
</div>
|
2023-01-21 16:47:37 +13:00
|
|
|
{banner === Banner.CANCEL_WARNING &&
|
2023-01-17 10:35:37 +13:00
|
|
|
<Alert severity="warning">
|
2023-01-18 13:40:03 +13:00
|
|
|
<Trans
|
|
|
|
i18nKey="account_upgrade_dialog_cancel_warning"
|
2023-01-19 07:46:40 +13:00
|
|
|
values={{ date: formatShortDate(account?.billing?.paid_until || 0) }} />
|
2023-01-17 10:35:37 +13:00
|
|
|
</Alert>
|
|
|
|
}
|
2023-01-21 16:47:37 +13:00
|
|
|
{banner === Banner.PRORATION_INFO &&
|
2023-01-18 04:09:37 +13:00
|
|
|
<Alert severity="info">
|
2023-01-18 13:40:03 +13:00
|
|
|
<Trans i18nKey="account_upgrade_dialog_proration_info" />
|
2023-01-18 04:09:37 +13:00
|
|
|
</Alert>
|
|
|
|
}
|
2023-01-21 16:47:37 +13:00
|
|
|
{banner === Banner.RESERVATIONS_WARNING &&
|
|
|
|
<Alert severity="warning">
|
|
|
|
<Trans
|
|
|
|
i18nKey="account_upgrade_dialog_reservations_warning"
|
|
|
|
count={account?.reservations.length - newTier?.limits.reservations}
|
|
|
|
components={{
|
|
|
|
Link: <NavLink to={routes.settings}/>,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Alert>
|
|
|
|
}
|
2023-01-10 09:40:46 +13:00
|
|
|
</DialogContent>
|
2023-02-03 09:19:37 +13:00
|
|
|
<DialogFooter status={error}>
|
2023-01-18 14:21:19 +13:00
|
|
|
<Button onClick={props.onCancel}>{t("account_upgrade_dialog_button_cancel")}</Button>
|
2023-01-21 16:47:37 +13:00
|
|
|
<Button onClick={handleSubmit} disabled={!submitAction}>{submitButtonLabel}</Button>
|
2023-01-10 09:40:46 +13:00
|
|
|
</DialogFooter>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-15 00:43:44 +13:00
|
|
|
const TierCard = (props) => {
|
2023-01-18 13:40:03 +13:00
|
|
|
const { t } = useTranslation();
|
|
|
|
const tier = props.tier;
|
2023-01-21 16:47:37 +13:00
|
|
|
let cardStyle, labelStyle, labelText;
|
|
|
|
if (props.selected) {
|
|
|
|
cardStyle = { background: "#eee", border: "2px solid #338574" };
|
|
|
|
labelStyle = { background: "#338574", color: "white" };
|
|
|
|
labelText = t("account_upgrade_dialog_tier_selected_label");
|
|
|
|
} else if (props.current) {
|
|
|
|
cardStyle = { border: "2px solid #eee" };
|
|
|
|
labelStyle = { background: "#eee", color: "black" };
|
|
|
|
labelText = t("account_upgrade_dialog_tier_current_label");
|
|
|
|
} else {
|
|
|
|
cardStyle = { border: "2px solid transparent" };
|
|
|
|
}
|
2023-01-18 13:40:03 +13:00
|
|
|
|
2023-01-15 00:43:44 +13:00
|
|
|
return (
|
2023-01-18 14:21:19 +13:00
|
|
|
<Box sx={{
|
|
|
|
m: "7px",
|
2023-01-18 04:09:37 +13:00
|
|
|
minWidth: "190px",
|
|
|
|
maxWidth: "250px",
|
2023-01-18 13:40:03 +13:00
|
|
|
flexGrow: 1,
|
|
|
|
flexShrink: 1,
|
|
|
|
flexBasis: 0,
|
2023-01-18 14:21:19 +13:00
|
|
|
borderRadius: "3px",
|
2023-01-19 07:46:40 +13:00
|
|
|
"&:first-of-type": { ml: 0 },
|
|
|
|
"&:last-of-type": { mr: 0 },
|
2023-01-18 04:09:37 +13:00
|
|
|
...cardStyle
|
|
|
|
}}>
|
2023-01-18 14:21:19 +13:00
|
|
|
<Card sx={{ height: "100%" }}>
|
|
|
|
<CardActionArea sx={{ height: "100%" }}>
|
|
|
|
<CardContent onClick={props.onClick} sx={{ height: "100%" }}>
|
2023-01-21 16:47:37 +13:00
|
|
|
{labelStyle &&
|
2023-01-18 14:21:19 +13:00
|
|
|
<div style={{
|
|
|
|
position: "absolute",
|
|
|
|
top: "0",
|
|
|
|
right: "15px",
|
|
|
|
padding: "2px 10px",
|
|
|
|
borderRadius: "3px",
|
2023-01-21 16:47:37 +13:00
|
|
|
...labelStyle
|
|
|
|
}}>{labelText}</div>
|
2023-01-18 14:21:19 +13:00
|
|
|
}
|
|
|
|
<Typography variant="h5" component="div">
|
2023-02-10 09:24:12 +13:00
|
|
|
{tier.name || t("account_basics_tier_free")}
|
2023-01-18 04:09:37 +13:00
|
|
|
</Typography>
|
2023-01-18 14:21:19 +13:00
|
|
|
<List dense>
|
|
|
|
{tier.limits.reservations > 0 && <FeatureItem>{t("account_upgrade_dialog_tier_features_reservations", { reservations: tier.limits.reservations })}</FeatureItem>}
|
|
|
|
<FeatureItem>{t("account_upgrade_dialog_tier_features_messages", { messages: formatNumber(tier.limits.messages) })}</FeatureItem>
|
|
|
|
<FeatureItem>{t("account_upgrade_dialog_tier_features_emails", { emails: formatNumber(tier.limits.emails) })}</FeatureItem>
|
|
|
|
<FeatureItem>{t("account_upgrade_dialog_tier_features_attachment_file_size", { filesize: formatBytes(tier.limits.attachment_file_size, 0) })}</FeatureItem>
|
|
|
|
<FeatureItem>{t("account_upgrade_dialog_tier_features_attachment_total_size", { totalsize: formatBytes(tier.limits.attachment_total_size, 0) })}</FeatureItem>
|
|
|
|
</List>
|
|
|
|
{tier.price &&
|
|
|
|
<Typography variant="subtitle1" sx={{fontWeight: 500}}>
|
|
|
|
{tier.price} / month
|
|
|
|
</Typography>
|
|
|
|
}
|
|
|
|
</CardContent>
|
|
|
|
</CardActionArea>
|
|
|
|
</Card>
|
|
|
|
</Box>
|
|
|
|
|
2023-01-15 00:43:44 +13:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:40:03 +13:00
|
|
|
const FeatureItem = (props) => {
|
|
|
|
return (
|
|
|
|
<ListItem disableGutters sx={{m: 0, p: 0}}>
|
|
|
|
<ListItemIcon sx={{minWidth: "24px"}}>
|
|
|
|
<Check fontSize="small" sx={{ color: "#338574" }}/>
|
|
|
|
</ListItemIcon>
|
|
|
|
<ListItemText
|
|
|
|
sx={{mt: "2px", mb: "2px"}}
|
|
|
|
primary={
|
|
|
|
<Typography variant="body2">
|
|
|
|
{props.children}
|
|
|
|
</Typography>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</ListItem>
|
|
|
|
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-17 10:35:37 +13:00
|
|
|
const Action = {
|
2023-01-21 16:47:37 +13:00
|
|
|
REDIRECT_SIGNUP: 1,
|
|
|
|
CREATE_SUBSCRIPTION: 2,
|
|
|
|
UPDATE_SUBSCRIPTION: 3,
|
|
|
|
CANCEL_SUBSCRIPTION: 4
|
2023-01-17 10:35:37 +13:00
|
|
|
};
|
|
|
|
|
2023-01-21 16:47:37 +13:00
|
|
|
const Banner = {
|
|
|
|
CANCEL_WARNING: 1,
|
|
|
|
PRORATION_INFO: 2,
|
|
|
|
RESERVATIONS_WARNING: 3
|
|
|
|
};
|
|
|
|
|
2023-01-10 09:40:46 +13:00
|
|
|
export default UpgradeDialog;
|