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

Billing notifications #1861

Merged
merged 45 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
28aeb22
initial changes
FSM1 Dec 16, 2021
b4b1f21
wire up wallet
FSM1 Dec 16, 2021
c339448
Merge remote-tracking branch 'origin/dev' into feat/crypto-payment-1782
FSM1 Jan 6, 2022
cfbabe9
add circular progress bar label
FSM1 Jan 6, 2022
cb8c861
add currency icons
FSM1 Jan 7, 2022
b421d2d
add copy buttons
FSM1 Jan 7, 2022
db103c4
Merge branch 'dev' into feat/crypto-payment-1782
FSM1 Jan 7, 2022
120505a
lingui extract
actions-user Jan 7, 2022
26ff251
lint and padding
FSM1 Jan 7, 2022
1475b27
Apply suggestions from code review
FSM1 Jan 11, 2022
c66bbda
implement fixes
FSM1 Jan 11, 2022
7649eb7
Merge remote-tracking branch 'origin/dev' into feat/crypto-payment-1782
FSM1 Jan 11, 2022
5d2493f
lingui extract
actions-user Jan 11, 2022
a364094
Update packages/files-ui/src/Components/Modules/Settings/Subscription…
FSM1 Jan 11, 2022
c27ead3
fixes from comments
FSM1 Jan 12, 2022
8907d28
remove unnecessary prop
FSM1 Jan 12, 2022
c2f7a7d
temp daily duration for testing - revert before merge
FSM1 Jan 12, 2022
ace49c3
fix dark mode icon colors
FSM1 Jan 12, 2022
c6cd7ca
update api client to latest
FSM1 Jan 14, 2022
1279178
Merge branch 'dev' into feat/crypto-payment-1782
FSM1 Jan 14, 2022
ec4048c
Apply suggestions from code review
FSM1 Jan 14, 2022
1d29c99
revert conflict marker
FSM1 Jan 14, 2022
c0e680f
Merge branch 'feat/crypto-payment-1782' of https://github.com/ChainSa…
FSM1 Jan 14, 2022
a9da33f
remove max-content width
FSM1 Jan 14, 2022
13347fe
Merge branch 'dev' into feat/crypto-payment-1782
Tbaut Jan 14, 2022
74ddf46
fix mobile width issues
FSM1 Jan 14, 2022
348447e
Revert "fix mobile width issues"
FSM1 Jan 17, 2022
46dfe09
ensure that text doesnt overflow
FSM1 Jan 17, 2022
6599357
fix width
FSM1 Jan 17, 2022
2a4006c
add notifications for account restricted, unpaid
FSM1 Jan 18, 2022
c018cb7
update notification text
FSM1 Jan 18, 2022
8a5fc1f
Merge remote-tracking branch 'origin/dev' into feat/billing-notificat…
FSM1 Jan 18, 2022
5ce381f
update translations
FSM1 Jan 18, 2022
e5edaa8
add credit card expiring notification
FSM1 Jan 19, 2022
9b6e42f
fix lint
FSM1 Jan 19, 2022
9f28b08
Merge branch 'dev' into feat/billing-notifications-1819
tanmoyAtb Jan 19, 2022
1804b86
Merge branch 'dev' into feat/billing-notifications-1819
FSM1 Jan 20, 2022
c07340d
move download to context
FSM1 Jan 20, 2022
ce1af2a
Apply suggestions from code review
FSM1 Jan 20, 2022
2cccf99
lingui extract
actions-user Jan 20, 2022
c52756e
remove unused vars
FSM1 Jan 20, 2022
eb1913e
Merge branch 'feat/billing-notifications-1819' of https://github.com/…
FSM1 Jan 20, 2022
491f8e2
Merge branch 'dev' into feat/billing-notifications-1819
FSM1 Jan 21, 2022
a33d755
lingui extract
actions-user Jan 21, 2022
109cb7d
Merge branch 'dev' into feat/billing-notifications-1819
FSM1 Jan 21, 2022
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
20 changes: 10 additions & 10 deletions packages/files-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ const App = () => {
enableLogging={directAuthNetwork !== "mainnet"}
network={directAuthNetwork}
>
<NotificationsProvider>
<UserProvider>
<FilesProvider>
<BillingProvider>
<Router>
<Router>
<NotificationsProvider>
<UserProvider>
<FilesProvider>
<BillingProvider>
<PosthogProvider>
<AppWrapper>
<FilesRoutes />
</AppWrapper>
</PosthogProvider>
</Router>
</BillingProvider>
</FilesProvider>
</UserProvider>
</NotificationsProvider>
</BillingProvider>
</FilesProvider>
</UserProvider>
</NotificationsProvider>
</Router>
</ThresholdKeyProvider>
</FilesApiProvider>
</Web3Provider>
Expand Down
35 changes: 28 additions & 7 deletions packages/files-ui/src/Components/Elements/InvoiceLines.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useMemo } from "react"
import React, { useCallback, useMemo } from "react"
import { makeStyles, createStyles } from "@chainsafe/common-theme"
import { CSFTheme } from "../../Themes/types"
import { Typography, Loading } from "@chainsafe/common-components"
import { Typography, Loading, Button } from "@chainsafe/common-components"
import { Trans } from "@lingui/macro"
import dayjs from "dayjs"
import { useBilling } from "../../Contexts/BillingContext"
import { useFilesApi } from "../../Contexts/FilesApiContext"

const useStyles = makeStyles(
({ constants, breakpoints, palette, typography }: CSFTheme) =>
Expand Down Expand Up @@ -61,6 +62,7 @@ interface IInvoiceProps {
const InvoiceLines = ({ lineNumber }: IInvoiceProps) => {
const classes = useStyles()
const { invoices } = useBilling()
const { filesApiClient } = useFilesApi()
const invoicesToShow = useMemo(() => {
if (!invoices) return

Expand All @@ -69,6 +71,18 @@ const InvoiceLines = ({ lineNumber }: IInvoiceProps) => {
: invoices
}, [invoices, lineNumber])

const downloadInvoice = useCallback(async (invoiceId: string) => {
Tbaut marked this conversation as resolved.
Show resolved Hide resolved
try {
const result = await filesApiClient.downloadInvoice(invoiceId)
const link = document.createElement("a")
link.href = URL.createObjectURL(result.data)
link.download = "Chainsafe Files Invoice"
link.click()
} catch (error) {
console.error(error)
}
}, [filesApiClient])

return (
<>
{!invoicesToShow && (
Expand All @@ -92,7 +106,7 @@ const InvoiceLines = ({ lineNumber }: IInvoiceProps) => {
</div>
)}
{!!invoicesToShow?.length && (
invoicesToShow.map(({ paid_on, amount, currency, uuid }) =>
invoicesToShow.map(({ amount, currency, uuid, period_start, status }) =>
<section
className={classes.setOption}
key={uuid}
Expand All @@ -108,11 +122,18 @@ const InvoiceLines = ({ lineNumber }: IInvoiceProps) => {
variant="body2"
className="receiptDate"
>
{dayjs(paid_on).format("MMM D, YYYY")}
</Typography>
<Typography>
{uuid}
{dayjs.unix(period_start).format("MMM D, YYYY")}
</Typography>
{(status === "paid") && (
<Button onClick={() => downloadInvoice(uuid)}>
Download
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
</Button>
)}
{(status === "open") && (
<Button onClick={() => console.log("Not implemented")}>
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
Pay invoice
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
</Button>
)}
</div>
</section>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const NotificationList = ({ notifications }: INotificationListProps) => {
className={classes.notificationTime}
component="p"
>
{dayjs(n.createdAt).fromNow()}
{dayjs.unix(n.createdAt).fromNow()}
</Typography>
</div>
))}
Expand Down
78 changes: 68 additions & 10 deletions packages/files-ui/src/Contexts/BillingContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { useCallback } from "react"
import { t } from "@lingui/macro"
import { PaymentMethod as StripePaymentMethod } from "@stripe/stripe-js"
import { useFiles } from "./FilesContext"
import { useNotifications } from "./NotificationsContext"
import dayjs from "dayjs"
import { useHistory } from "@chainsafe/common-components"
import { ROUTE_LINKS } from "../Components/FilesRoutes"

export type PaymentMethod = "crypto" | "creditCard"

Expand Down Expand Up @@ -49,32 +53,86 @@ const BillingContext = React.createContext<IBillingContext | undefined>(
)

const BillingProvider = ({ children }: BillingContextProps) => {
const { filesApiClient, isLoggedIn } = useFilesApi()
const { filesApiClient, isLoggedIn, accountRestricted } = useFilesApi()
const { redirect } = useHistory()
const { addNotification, removeNotification } = useNotifications()
const { refreshBuckets } = useFiles()
const [currentSubscription, setCurrentSubscription] = useState<CurrentSubscription | undefined>()
const [defaultCard, setDefaultCard] = useState<Card | undefined>(undefined)
const [invoices, setInvoices] = useState<InvoiceResponse[] | undefined>()
const [restrictedNotification, setRestrictedNotification] = useState<string | undefined>()
Tbaut marked this conversation as resolved.
Show resolved Hide resolved
const [unpaidInvoiceNotification, setUnpaidInvoiceNotification] = useState<string | undefined>()
const [cardExpiringNotification, setCardExpiringNotification] = useState<string | undefined>()

useEffect(() => {
if (!currentSubscription) return

filesApiClient.getAllInvoices(currentSubscription.id)
.then(({ invoices }) => {
setInvoices(invoices)
})
.catch((e: any) => {
setInvoices(invoices
.filter(i => i.status !== "void")
.sort((a, b) => b.period_start - a.period_start))
}).catch((e: any) => {
console.error(e)
setInvoices([])
})
}, [currentSubscription, filesApiClient])

useEffect(() => {
if (accountRestricted && !restrictedNotification) {
const notif = addNotification({
createdAt: dayjs().unix(),
title: t`Account is restricted`,
onClick: () => redirect(ROUTE_LINKS.SettingsPath("plan"))
})
setRestrictedNotification(notif)
} else if (accountRestricted === false && restrictedNotification) {
removeNotification(restrictedNotification)
setRestrictedNotification(undefined)
}
}, [accountRestricted, addNotification, redirect, removeNotification, restrictedNotification])

useEffect(() => {
const outstandingInvoice = invoices?.find(i => i.status === "open")
if (outstandingInvoice && !unpaidInvoiceNotification) {
const notif = addNotification({
createdAt: outstandingInvoice.period_start,
title: t`Invoice outstanding`,
onClick: () => redirect(ROUTE_LINKS.SettingsPath("plan"))
})
setUnpaidInvoiceNotification(notif)
} else if (!outstandingInvoice && unpaidInvoiceNotification) {
removeNotification(unpaidInvoiceNotification)
setUnpaidInvoiceNotification(undefined)
}
}, [addNotification, invoices, redirect, removeNotification, unpaidInvoiceNotification])

useEffect(() => {
if (defaultCard && currentSubscription) {
if (!cardExpiringNotification && currentSubscription.expiry_date >
dayjs(`${defaultCard.exp_year}-${defaultCard.exp_month}-01`, "YYYY-MM-DD").endOf("month").unix()) {
const notif = addNotification({
createdAt: dayjs().unix(),
title: t`Credit Card is expiring soon`,
onClick: () => redirect(ROUTE_LINKS.SettingsPath("plan"))
})
setCardExpiringNotification(notif)
} else if (cardExpiringNotification && currentSubscription?.expiry_date <=
dayjs(`${defaultCard?.exp_year}-${defaultCard?.exp_month}-01`, "YYYY-MM-DD").endOf("month").unix()) {
removeNotification(cardExpiringNotification)
setCardExpiringNotification(undefined)
}
}
}, [addNotification, cardExpiringNotification, currentSubscription, defaultCard, redirect, removeNotification])

const refreshDefaultCard = useCallback(() => {
filesApiClient.getDefaultCard().then((card) => {
setDefaultCard(card)
}).catch((err) => {
console.error(err)
setDefaultCard(undefined)
})
filesApiClient.getDefaultCard()
.then((card) => {
setDefaultCard(card)
}).catch((err) => {
console.error(err)
setDefaultCard(undefined)
})
}, [filesApiClient])

const deleteCard = useCallback((card: Card) =>
Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/de/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ msgstr ""
msgid "Account"
msgstr "Konto"

msgid "Account is restricted"
msgstr ""

msgid "Active links"
msgstr ""

Expand Down Expand Up @@ -250,6 +253,9 @@ msgstr ""
msgid "Create your public username in <0>Settings</0>!"
msgstr ""

msgid "Credit Card is expiring soon"
msgstr ""

msgid "Credit card saved"
msgstr ""

Expand Down Expand Up @@ -472,6 +478,9 @@ msgstr "Infos"
msgid "Insufficient balance"
msgstr ""

msgid "Invoice outstanding"
msgstr ""

msgid "I’m done saving my backup secret phrase"
msgstr "Ich bin fertig mit dem Speichern meines Sixherungsgeheimsatzes"

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ msgstr "Access your billing history in settings or view your"
msgid "Account"
msgstr "Account"

msgid "Account is restricted"
msgstr "Account is restricted"

msgid "Active links"
msgstr "Active links"

Expand Down Expand Up @@ -250,6 +253,9 @@ msgstr "Create link"
msgid "Create your public username in <0>Settings</0>!"
msgstr "Create your public username in <0>Settings</0>!"

msgid "Credit Card is expiring soon"
msgstr "Credit Card is expiring soon"

msgid "Credit card saved"
msgstr "Credit card saved"

Expand Down Expand Up @@ -475,6 +481,9 @@ msgstr "Info"
msgid "Insufficient balance"
msgstr "Insufficient balance"

msgid "Invoice outstanding"
msgstr "Invoice outstanding"

msgid "I’m done saving my backup secret phrase"
msgstr "I’m done saving my backup secret phrase"

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ msgstr ""
msgid "Account"
msgstr "Cuenta"

msgid "Account is restricted"
msgstr ""

msgid "Active links"
msgstr ""

Expand Down Expand Up @@ -251,6 +254,9 @@ msgstr ""
msgid "Create your public username in <0>Settings</0>!"
msgstr ""

msgid "Credit Card is expiring soon"
msgstr ""

msgid "Credit card saved"
msgstr ""

Expand Down Expand Up @@ -476,6 +482,9 @@ msgstr "Info"
msgid "Insufficient balance"
msgstr ""

msgid "Invoice outstanding"
msgstr ""

msgid "I’m done saving my backup secret phrase"
msgstr ""

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ msgstr ""
msgid "Account"
msgstr "Compte"

msgid "Account is restricted"
msgstr ""

msgid "Active links"
msgstr "Liens actifs"

Expand Down Expand Up @@ -251,6 +254,9 @@ msgstr "Créer un lien"
msgid "Create your public username in <0>Settings</0>!"
msgstr "Créez votre nom d'utilisateur public dans <0>Paramètres</0> !"

msgid "Credit Card is expiring soon"
msgstr ""

msgid "Credit card saved"
msgstr "Carte de crédit enregistrée"

Expand Down Expand Up @@ -476,6 +482,9 @@ msgstr "Infos"
msgid "Insufficient balance"
msgstr ""

msgid "Invoice outstanding"
msgstr ""

msgid "I’m done saving my backup secret phrase"
msgstr "Phrase de sauvegarde secrète enregistrée"

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/no/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ msgstr ""
msgid "Account"
msgstr "Konto"

msgid "Account is restricted"
msgstr ""

msgid "Active links"
msgstr ""

Expand Down Expand Up @@ -250,6 +253,9 @@ msgstr ""
msgid "Create your public username in <0>Settings</0>!"
msgstr ""

msgid "Credit Card is expiring soon"
msgstr ""

msgid "Credit card saved"
msgstr ""

Expand Down Expand Up @@ -472,6 +478,9 @@ msgstr "Info"
msgid "Insufficient balance"
msgstr ""

msgid "Invoice outstanding"
msgstr ""

msgid "I’m done saving my backup secret phrase"
msgstr ""

Expand Down