| 1 |
import * as React from "react"; |
| 2 |
import { useContext, useEffect, useState } from "react"; |
| 3 |
import { |
| 4 |
Dialog, |
| 5 |
DialogContent, |
| 6 |
DialogTitle, |
| 7 |
Alert, |
| 8 |
CardActionArea, |
| 9 |
CardContent, |
| 10 |
Chip, |
| 11 |
Link, |
| 12 |
ListItem, |
| 13 |
Switch, |
| 14 |
useMediaQuery, |
| 15 |
Button, |
| 16 |
Card, |
| 17 |
Typography, |
| 18 |
List, |
| 19 |
ListItemIcon, |
| 20 |
ListItemText, |
| 21 |
Box, |
| 22 |
DialogContentText, |
| 23 |
DialogActions, |
| 24 |
useTheme, |
| 25 |
} from "@mui/material"; |
| 26 |
import { Trans, useTranslation } from "react-i18next"; |
| 27 |
import Check from "@mui/icons-material/Check"; |
| 28 |
import Close from "@mui/icons-material/Close"; |
| 29 |
import { NavLink } from "react-router-dom"; |
| 30 |
import { UnauthorizedError } from "../app/errors"; |
| 31 |
import { formatBytes, formatNumber, formatPrice, formatDate } from "../app/utils"; |
| 32 |
import AccountContext from "./AccountContext"; |
| 33 |
import { usePrefCache } from "./PrefCache"; |
| 34 |
import routes from "./routes"; |
| 35 |
import session from "../app/Session"; |
| 36 |
import accountApi, { SubscriptionInterval } from "../app/AccountApi"; |
| 37 |
|
| 38 |
const Feature = (props) => <FeatureItem feature>{props.children}</FeatureItem>; |
| 39 |
|
| 40 |
const NoFeature = (props) => <FeatureItem feature={false}>{props.children}</FeatureItem>; |
| 41 |
|
| 42 |
const FeatureItem = (props) => ( |
| 43 |
<ListItem disableGutters sx={{ m: 0, p: 0 }}> |
| 44 |
<ListItemIcon sx={{ minWidth: "24px" }}> |
| 45 |
{props.feature && <Check fontSize="small" sx={{ color: "#338574" }} />} |
| 46 |
{!props.feature && <Close fontSize="small" sx={{ color: "gray" }} />} |
| 47 |
</ListItemIcon> |
| 48 |
<ListItemText sx={{ mt: "2px", mb: "2px" }} primary={<Typography variant="body1">{props.children}</Typography>} /> |
| 49 |
</ListItem> |
| 50 |
); |
| 51 |
|
| 52 |
const Action = { |
| 53 |
REDIRECT_SIGNUP: 1, |
| 54 |
CREATE_SUBSCRIPTION: 2, |
| 55 |
UPDATE_SUBSCRIPTION: 3, |
| 56 |
CANCEL_SUBSCRIPTION: 4, |
| 57 |
}; |
| 58 |
|
| 59 |
const Banner = { |
| 60 |
CANCEL_WARNING: 1, |
| 61 |
PRORATION_INFO: 2, |
| 62 |
RESERVATIONS_WARNING: 3, |
| 63 |
}; |
| 64 |
|
| 65 |
const UpgradeDialog = (props) => { |
| 66 |
const theme = useTheme(); |
| 67 |
const { t } = useTranslation(); |
| 68 |
const { dateFormat } = usePrefCache(); |
| 69 |
const { account } = useContext(AccountContext); // May be undefined! |
| 70 |
const [error, setError] = useState(""); |
| 71 |
const [tiers, setTiers] = useState(null); |
| 72 |
const [interval, setInterval] = useState(account?.billing?.interval || SubscriptionInterval.YEAR); |
| 73 |
const [newTierCode, setNewTierCode] = useState(account?.tier?.code); // May be undefined |
| 74 |
const [loading, setLoading] = useState(false); |
| 75 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 76 |
|
| 77 |
useEffect(() => { |
| 78 |
const fetchTiers = async () => { |
| 79 |
setTiers(await accountApi.billingTiers()); |
| 80 |
}; |
| 81 |
fetchTiers(); // Dangle |
| 82 |
}, []); |
| 83 |
|
| 84 |
if (!tiers) { |
| 85 |
return <></>; |
| 86 |
} |
| 87 |
|
| 88 |
const tiersMap = Object.assign(...tiers.map((tier) => ({ [tier.code]: tier }))); |
| 89 |
const newTier = tiersMap[newTierCode]; // May be undefined |
| 90 |
const currentTier = account?.tier; // May be undefined |
| 91 |
const currentInterval = account?.billing?.interval; // May be undefined |
| 92 |
const currentTierCode = currentTier?.code; // May be undefined |
| 93 |
|
| 94 |
// Figure out buttons, labels and the submit action |
| 95 |
let submitAction; |
| 96 |
let submitButtonLabel; |
| 97 |
let banner; |
| 98 |
if (!account) { |
| 99 |
submitButtonLabel = t("account_upgrade_dialog_button_redirect_signup"); |
| 100 |
submitAction = Action.REDIRECT_SIGNUP; |
| 101 |
banner = null; |
| 102 |
} else if (currentTierCode === newTierCode && (currentInterval === undefined || currentInterval === interval)) { |
| 103 |
submitButtonLabel = t("account_upgrade_dialog_button_update_subscription"); |
| 104 |
submitAction = null; |
| 105 |
banner = currentTierCode ? Banner.PRORATION_INFO : null; |
| 106 |
} else if (!currentTierCode) { |
| 107 |
submitButtonLabel = t("account_upgrade_dialog_button_pay_now"); |
| 108 |
submitAction = Action.CREATE_SUBSCRIPTION; |
| 109 |
banner = null; |
| 110 |
} else if (!newTierCode) { |
| 111 |
submitButtonLabel = t("account_upgrade_dialog_button_cancel_subscription"); |
| 112 |
submitAction = Action.CANCEL_SUBSCRIPTION; |
| 113 |
banner = Banner.CANCEL_WARNING; |
| 114 |
} else { |
| 115 |
submitButtonLabel = t("account_upgrade_dialog_button_update_subscription"); |
| 116 |
submitAction = Action.UPDATE_SUBSCRIPTION; |
| 117 |
banner = Banner.PRORATION_INFO; |
| 118 |
} |
| 119 |
|
| 120 |
// Exceptional conditions |
| 121 |
if (loading) { |
| 122 |
submitAction = null; |
| 123 |
} else if (newTier?.code && account?.reservations?.length > newTier?.limits?.reservations) { |
| 124 |
submitAction = null; |
| 125 |
banner = Banner.RESERVATIONS_WARNING; |
| 126 |
} |
| 127 |
|
| 128 |
const handleSubmit = async () => { |
| 129 |
if (submitAction === Action.REDIRECT_SIGNUP) { |
| 130 |
window.location.href = routes.signup; |
| 131 |
return; |
| 132 |
} |
| 133 |
try { |
| 134 |
setLoading(true); |
| 135 |
if (submitAction === Action.CREATE_SUBSCRIPTION) { |
| 136 |
const response = await accountApi.createBillingSubscription(newTierCode, interval); |
| 137 |
window.location.href = response.redirect_url; |
| 138 |
} else if (submitAction === Action.UPDATE_SUBSCRIPTION) { |
| 139 |
await accountApi.updateBillingSubscription(newTierCode, interval); |
| 140 |
} else if (submitAction === Action.CANCEL_SUBSCRIPTION) { |
| 141 |
await accountApi.deleteBillingSubscription(); |
| 142 |
} |
| 143 |
props.onCancel(); |
| 144 |
} catch (e) { |
| 145 |
console.log(`[UpgradeDialog] Error changing billing subscription`, e); |
| 146 |
if (e instanceof UnauthorizedError) { |
| 147 |
await session.resetAndRedirect(routes.login); |
| 148 |
} else { |
| 149 |
setError(e.message); |
| 150 |
} |
| 151 |
} finally { |
| 152 |
setLoading(false); |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
// Figure out discount |
| 157 |
let discount = 0; |
| 158 |
let upto = false; |
| 159 |
if (newTier?.prices) { |
| 160 |
discount = Math.round(((newTier.prices.month * 12) / newTier.prices.year - 1) * 100); |
| 161 |
} else { |
| 162 |
let n = 0; |
| 163 |
for (const tier of tiers) { |
| 164 |
if (tier.prices) { |
| 165 |
const tierDiscount = Math.round(((tier.prices.month * 12) / tier.prices.year - 1) * 100); |
| 166 |
if (tierDiscount > discount) { |
| 167 |
discount = tierDiscount; |
| 168 |
n += 1; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
upto = n > 1; |
| 173 |
} |
| 174 |
|
| 175 |
return ( |
| 176 |
<Dialog open={props.open} onClose={props.onCancel} maxWidth="lg" fullScreen={fullScreen}> |
| 177 |
<DialogTitle> |
| 178 |
<div style={{ display: "flex", flexDirection: "row" }}> |
| 179 |
<div style={{ flexGrow: 1 }}>{t("account_upgrade_dialog_title")}</div> |
| 180 |
<div |
| 181 |
style={{ |
| 182 |
display: "flex", |
| 183 |
flexDirection: "row", |
| 184 |
alignItems: "center", |
| 185 |
marginTop: "4px", |
| 186 |
}} |
| 187 |
> |
| 188 |
<Typography component="span" variant="subtitle1"> |
| 189 |
{t("account_upgrade_dialog_interval_monthly")} |
| 190 |
</Typography> |
| 191 |
<Switch |
| 192 |
checked={interval === SubscriptionInterval.YEAR} |
| 193 |
onChange={(ev) => setInterval(ev.target.checked ? SubscriptionInterval.YEAR : SubscriptionInterval.MONTH)} |
| 194 |
/> |
| 195 |
<Typography component="span" variant="subtitle1"> |
| 196 |
{t("account_upgrade_dialog_interval_yearly")} |
| 197 |
</Typography> |
| 198 |
{discount > 0 && ( |
| 199 |
<Chip |
| 200 |
label={ |
| 201 |
upto |
| 202 |
? t("account_upgrade_dialog_interval_yearly_discount_save_up_to", { discount }) |
| 203 |
: t("account_upgrade_dialog_interval_yearly_discount_save", { discount }) |
| 204 |
} |
| 205 |
color="primary" |
| 206 |
size="small" |
| 207 |
variant={interval === SubscriptionInterval.YEAR ? "filled" : "outlined"} |
| 208 |
sx={{ marginLeft: "5px" }} |
| 209 |
/> |
| 210 |
)} |
| 211 |
</div> |
| 212 |
</div> |
| 213 |
</DialogTitle> |
| 214 |
<DialogContent> |
| 215 |
<div |
| 216 |
style={{ |
| 217 |
display: "flex", |
| 218 |
flexDirection: "row", |
| 219 |
marginBottom: "8px", |
| 220 |
width: "100%", |
| 221 |
}} |
| 222 |
> |
| 223 |
{tiers.map((tier) => ( |
| 224 |
<TierCard |
| 225 |
key={`tierCard${tier.code || "_free"}`} |
| 226 |
tier={tier} |
| 227 |
current={currentTierCode === tier.code} // tier.code or currentTierCode may be undefined! |
| 228 |
selected={newTierCode === tier.code} // tier.code may be undefined! |
| 229 |
interval={interval} |
| 230 |
onClick={() => setNewTierCode(tier.code)} // tier.code may be undefined! |
| 231 |
/> |
| 232 |
))} |
| 233 |
</div> |
| 234 |
{banner === Banner.CANCEL_WARNING && ( |
| 235 |
<Alert severity="warning" sx={{ fontSize: "1rem" }}> |
| 236 |
<Trans |
| 237 |
i18nKey="account_upgrade_dialog_cancel_warning" |
| 238 |
values={{ |
| 239 |
date: formatDate(account?.billing?.paid_until || 0, dateFormat), |
| 240 |
}} |
| 241 |
/> |
| 242 |
</Alert> |
| 243 |
)} |
| 244 |
{banner === Banner.PRORATION_INFO && ( |
| 245 |
<Alert severity="info" sx={{ fontSize: "1rem" }}> |
| 246 |
<Trans i18nKey="account_upgrade_dialog_proration_info" /> |
| 247 |
</Alert> |
| 248 |
)} |
| 249 |
{banner === Banner.RESERVATIONS_WARNING && ( |
| 250 |
<Alert severity="warning" sx={{ fontSize: "1rem" }}> |
| 251 |
<Trans |
| 252 |
i18nKey="account_upgrade_dialog_reservations_warning" |
| 253 |
count={(account?.reservations.length ?? 0) - (newTier?.limits.reservations ?? 0)} |
| 254 |
components={{ |
| 255 |
Link: <NavLink to={routes.settings} />, |
| 256 |
}} |
| 257 |
/> |
| 258 |
</Alert> |
| 259 |
)} |
| 260 |
</DialogContent> |
| 261 |
<Box |
| 262 |
sx={{ |
| 263 |
display: "flex", |
| 264 |
flexDirection: "row", |
| 265 |
justifyContent: "space-between", |
| 266 |
paddingLeft: "24px", |
| 267 |
paddingBottom: "8px", |
| 268 |
}} |
| 269 |
> |
| 270 |
<DialogContentText |
| 271 |
component="div" |
| 272 |
aria-live="polite" |
| 273 |
sx={{ |
| 274 |
margin: "0px", |
| 275 |
paddingTop: "12px", |
| 276 |
paddingBottom: "4px", |
| 277 |
}} |
| 278 |
> |
| 279 |
{config.billing_contact.indexOf("@") !== -1 && ( |
| 280 |
<> |
| 281 |
<Trans |
| 282 |
i18nKey="account_upgrade_dialog_billing_contact_email" |
| 283 |
components={{ |
| 284 |
Link: <Link href={`mailto:${config.billing_contact}`} />, |
| 285 |
}} |
| 286 |
/>{" "} |
| 287 |
</> |
| 288 |
)} |
| 289 |
{config.billing_contact.match(`^http?s://`) && ( |
| 290 |
<> |
| 291 |
<Trans |
| 292 |
i18nKey="account_upgrade_dialog_billing_contact_website" |
| 293 |
components={{ |
| 294 |
Link: <Link href={config.billing_contact} target="_blank" />, |
| 295 |
}} |
| 296 |
/>{" "} |
| 297 |
</> |
| 298 |
)} |
| 299 |
{error} |
| 300 |
</DialogContentText> |
| 301 |
<DialogActions sx={{ paddingRight: 2 }}> |
| 302 |
<Button onClick={props.onCancel}>{t("account_upgrade_dialog_button_cancel")}</Button> |
| 303 |
<Button onClick={handleSubmit} disabled={!submitAction}> |
| 304 |
{submitButtonLabel} |
| 305 |
</Button> |
| 306 |
</DialogActions> |
| 307 |
</Box> |
| 308 |
</Dialog> |
| 309 |
); |
| 310 |
}; |
| 311 |
|
| 312 |
const TierCard = (props) => { |
| 313 |
const { t } = useTranslation(); |
| 314 |
const { tier } = props; |
| 315 |
|
| 316 |
let cardStyle; |
| 317 |
let labelStyle; |
| 318 |
let labelText; |
| 319 |
if (props.selected) { |
| 320 |
cardStyle = { background: "#eee", border: "3px solid #338574" }; |
| 321 |
labelStyle = { background: "#338574", color: "white" }; |
| 322 |
labelText = t("account_upgrade_dialog_tier_selected_label"); |
| 323 |
} else if (props.current) { |
| 324 |
cardStyle = { border: "3px solid #eee" }; |
| 325 |
labelStyle = { background: "#eee", color: "black" }; |
| 326 |
labelText = t("account_upgrade_dialog_tier_current_label"); |
| 327 |
} else { |
| 328 |
cardStyle = { border: "3px solid transparent" }; |
| 329 |
} |
| 330 |
|
| 331 |
let monthlyPrice; |
| 332 |
if (!tier.prices) { |
| 333 |
monthlyPrice = 0; |
| 334 |
} else if (props.interval === SubscriptionInterval.YEAR) { |
| 335 |
monthlyPrice = tier.prices.year / 12; |
| 336 |
} else if (props.interval === SubscriptionInterval.MONTH) { |
| 337 |
monthlyPrice = tier.prices.month; |
| 338 |
} |
| 339 |
|
| 340 |
return ( |
| 341 |
<Box |
| 342 |
sx={{ |
| 343 |
m: "7px", |
| 344 |
minWidth: "240px", |
| 345 |
flexGrow: 1, |
| 346 |
flexShrink: 1, |
| 347 |
flexBasis: 0, |
| 348 |
borderRadius: "5px", |
| 349 |
"&:first-of-type": { ml: 0 }, |
| 350 |
"&:last-of-type": { mr: 0 }, |
| 351 |
...cardStyle, |
| 352 |
}} |
| 353 |
> |
| 354 |
<Card sx={{ height: "100%" }}> |
| 355 |
<CardActionArea sx={{ height: "100%" }}> |
| 356 |
<CardContent onClick={props.onClick} sx={{ height: "100%" }}> |
| 357 |
{labelStyle && ( |
| 358 |
<div |
| 359 |
style={{ |
| 360 |
position: "absolute", |
| 361 |
top: "0", |
| 362 |
right: "15px", |
| 363 |
padding: "2px 10px", |
| 364 |
borderRadius: "3px", |
| 365 |
...labelStyle, |
| 366 |
}} |
| 367 |
> |
| 368 |
{labelText} |
| 369 |
</div> |
| 370 |
)} |
| 371 |
<Typography variant="subtitle1" component="div"> |
| 372 |
{tier.name || t("account_basics_tier_free")} |
| 373 |
</Typography> |
| 374 |
<div> |
| 375 |
<Typography component="span" variant="h4" sx={{ fontWeight: 500, marginRight: "3px" }}> |
| 376 |
{formatPrice(monthlyPrice)} |
| 377 |
</Typography> |
| 378 |
{monthlyPrice > 0 && <>/ {t("account_upgrade_dialog_tier_price_per_month")}</>} |
| 379 |
</div> |
| 380 |
<List dense> |
| 381 |
{tier.limits.reservations > 0 && ( |
| 382 |
<Feature> |
| 383 |
{t("account_upgrade_dialog_tier_features_reservations", { |
| 384 |
reservations: tier.limits.reservations, |
| 385 |
count: tier.limits.reservations, |
| 386 |
})} |
| 387 |
</Feature> |
| 388 |
)} |
| 389 |
<Feature> |
| 390 |
{t("account_upgrade_dialog_tier_features_messages", { |
| 391 |
messages: formatNumber(tier.limits.messages), |
| 392 |
count: tier.limits.messages, |
| 393 |
})} |
| 394 |
</Feature> |
| 395 |
<Feature> |
| 396 |
{t("account_upgrade_dialog_tier_features_emails", { |
| 397 |
emails: formatNumber(tier.limits.emails), |
| 398 |
count: tier.limits.emails, |
| 399 |
})} |
| 400 |
</Feature> |
| 401 |
{tier.limits.calls > 0 && ( |
| 402 |
<Feature> |
| 403 |
{t("account_upgrade_dialog_tier_features_calls", { |
| 404 |
calls: formatNumber(tier.limits.calls), |
| 405 |
count: tier.limits.calls, |
| 406 |
})} |
| 407 |
</Feature> |
| 408 |
)} |
| 409 |
<Feature> |
| 410 |
{t("account_upgrade_dialog_tier_features_attachment_file_size", { |
| 411 |
filesize: formatBytes(tier.limits.attachment_file_size, 0), |
| 412 |
})} |
| 413 |
</Feature> |
| 414 |
{tier.limits.reservations === 0 && <NoFeature>{t("account_upgrade_dialog_tier_features_no_reservations")}</NoFeature>} |
| 415 |
{tier.limits.calls === 0 && <NoFeature>{t("account_upgrade_dialog_tier_features_no_calls")}</NoFeature>} |
| 416 |
</List> |
| 417 |
{tier.prices && props.interval === SubscriptionInterval.MONTH && ( |
| 418 |
<Typography variant="body2" color="gray"> |
| 419 |
{t("account_upgrade_dialog_tier_price_billed_monthly", { |
| 420 |
price: formatPrice(tier.prices.month * 12), |
| 421 |
})} |
| 422 |
</Typography> |
| 423 |
)} |
| 424 |
{tier.prices && props.interval === SubscriptionInterval.YEAR && ( |
| 425 |
<Typography variant="body2" color="gray"> |
| 426 |
{t("account_upgrade_dialog_tier_price_billed_yearly", { |
| 427 |
price: formatPrice(tier.prices.year), |
| 428 |
save: formatPrice(tier.prices.month * 12 - tier.prices.year), |
| 429 |
})} |
| 430 |
</Typography> |
| 431 |
)} |
| 432 |
</CardContent> |
| 433 |
</CardActionArea> |
| 434 |
</Card> |
| 435 |
</Box> |
| 436 |
); |
| 437 |
}; |
| 438 |
|
| 439 |
export default UpgradeDialog; |