|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + TextField, |
| 5 | + Button, |
| 6 | + FormGroup, |
| 7 | + InputLabel, |
| 8 | + Paper, |
| 9 | + Stack, |
| 10 | + Dialog, |
| 11 | + DialogTitle, |
| 12 | + DialogContent, |
| 13 | + DialogActions, |
| 14 | + MenuItem, |
| 15 | + Select, |
| 16 | +} from "@mui/material"; |
| 17 | +import { axios } from "../data/axios"; |
| 18 | +import { LoadingButton } from "@mui/lab"; |
| 19 | +import { enqueueSnackbar } from "notistack"; |
| 20 | + |
| 21 | +const SubscriptionUpdateModal = ({ open, handleCloseCb, userEmail }) => { |
| 22 | + const [subscriptionPrices, setSubscriptionPrices] = useState([]); |
| 23 | + const [subscription, setSubscription] = useState(""); |
| 24 | + const [updateButtonLoading, setUpdateButtonLoading] = useState(false); |
| 25 | + const [updateButtonDisabled, setUpdateButtonDisabled] = useState(false); |
| 26 | + const [cancelButtonDisabled, setCancelButtonDisabled] = useState(false); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + axios() |
| 30 | + .get("/api/subscriptions/prices") |
| 31 | + .then((res) => { |
| 32 | + setSubscriptionPrices(res.data.prices || []); |
| 33 | + }) |
| 34 | + .catch((err) => { |
| 35 | + enqueueSnackbar("Error loading subscription prices", { |
| 36 | + variant: "error", |
| 37 | + }); |
| 38 | + }); |
| 39 | + }, []); |
| 40 | + |
| 41 | + return ( |
| 42 | + <Dialog open={open} onClose={handleCloseCb} fullWidth> |
| 43 | + <DialogTitle>{"Manage Subscription"}</DialogTitle> |
| 44 | + <DialogContent> |
| 45 | + <FormGroup> |
| 46 | + <Stack spacing={2} sx={{ textAlign: "left", margin: "10px" }}> |
| 47 | + <Paper sx={{ marginBottom: "30px" }}> |
| 48 | + <TextField |
| 49 | + label="Email" |
| 50 | + value={userEmail} |
| 51 | + fullWidth |
| 52 | + variant="outlined" |
| 53 | + /> |
| 54 | + </Paper> |
| 55 | + <Paper sx={{ marginBottom: "30px" }}> |
| 56 | + <InputLabel id="subscription-id">Subscription</InputLabel> |
| 57 | + <Select |
| 58 | + labelId="subscription-id" |
| 59 | + value={subscription} |
| 60 | + label="Subscription" |
| 61 | + fullWidth |
| 62 | + variant="outlined" |
| 63 | + onChange={(e) => { |
| 64 | + setSubscription(e.target.value); |
| 65 | + }} |
| 66 | + > |
| 67 | + {subscriptionPrices.map((subscriptionPrice) => ( |
| 68 | + <MenuItem |
| 69 | + key={subscriptionPrice.id} |
| 70 | + value={subscriptionPrice.id} |
| 71 | + > |
| 72 | + {subscriptionPrice.name} |
| 73 | + </MenuItem> |
| 74 | + ))} |
| 75 | + </Select> |
| 76 | + </Paper> |
| 77 | + </Stack> |
| 78 | + </FormGroup> |
| 79 | + </DialogContent> |
| 80 | + <DialogActions> |
| 81 | + <Button disabled={cancelButtonDisabled} onClick={handleCloseCb}> |
| 82 | + Cancel |
| 83 | + </Button> |
| 84 | + <LoadingButton |
| 85 | + disabled={updateButtonDisabled || !subscription} |
| 86 | + loading={updateButtonLoading} |
| 87 | + onClick={() => { |
| 88 | + setCancelButtonDisabled(true); |
| 89 | + setUpdateButtonDisabled(true); |
| 90 | + setUpdateButtonLoading(true); |
| 91 | + axios() |
| 92 | + .post("/api/subscriptions/checkout", { |
| 93 | + price_id: subscription, |
| 94 | + }) |
| 95 | + .then((res) => { |
| 96 | + enqueueSnackbar("Loading Checkout Page", { |
| 97 | + variant: "success", |
| 98 | + }); |
| 99 | + window.location.href = res.data.checkout_session_url; |
| 100 | + }) |
| 101 | + .catch((err) => {}) |
| 102 | + .finally(() => { |
| 103 | + setCancelButtonDisabled(false); |
| 104 | + setUpdateButtonDisabled(false); |
| 105 | + setUpdateButtonLoading(false); |
| 106 | + }); |
| 107 | + }} |
| 108 | + variant="contained" |
| 109 | + > |
| 110 | + Update |
| 111 | + </LoadingButton> |
| 112 | + </DialogActions> |
| 113 | + </Dialog> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export default SubscriptionUpdateModal; |
0 commit comments