From a8c7c61873939c2550038367f16da0e53750094a Mon Sep 17 00:00:00 2001 From: Mauricio Araujo Date: Thu, 2 May 2024 16:45:58 -0400 Subject: [PATCH] Add billing page link on billing modal (#4598) --- dashboard/src/main/home/Home.tsx | 36 ++----------- .../src/main/home/app-dashboard/apps/Apps.tsx | 54 +++++++++++++++++-- .../src/main/home/modals/BillingModal.tsx | 3 ++ .../home/project-settings/ProjectSettings.tsx | 5 -- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/dashboard/src/main/home/Home.tsx b/dashboard/src/main/home/Home.tsx index ecc5695cff..40cc5effa3 100644 --- a/dashboard/src/main/home/Home.tsx +++ b/dashboard/src/main/home/Home.tsx @@ -221,7 +221,7 @@ const Home: React.FC = (props) => { } else { setHasFinishedOnboarding(true); } - } catch (error) {} + } catch (error) { } }; useEffect(() => { @@ -428,32 +428,6 @@ const Home: React.FC = (props) => { {dayjs().to(dayjs(plan.trial_info.ending_before))} )} - {!trialExpired && showBillingModal && ( - { - setShowBillingModal(false); - }} - onCreate={async () => { - await refetchPaymentEnabled({ - throwOnError: false, - cancelRefetch: false, - }); - setShowBillingModal(false); - }} - /> - )} - {trialExpired && ( - { - await refetchPaymentEnabled({ - throwOnError: false, - cancelRefetch: false, - }); - setShowBillingModal(false); - }} - /> - )} )} @@ -550,8 +524,8 @@ const Home: React.FC = (props) => { {currentProject?.capi_provisioner_enabled && - currentProject?.simplified_view_enabled && - currentProject?.beta_features_enabled ? ( + currentProject?.simplified_view_enabled && + currentProject?.beta_features_enabled ? ( ) : ( @@ -565,8 +539,8 @@ const Home: React.FC = (props) => { {currentProject?.capi_provisioner_enabled && - currentProject?.simplified_view_enabled && - currentProject?.beta_features_enabled ? ( + currentProject?.simplified_view_enabled && + currentProject?.beta_features_enabled ? ( ) : ( diff --git a/dashboard/src/main/home/app-dashboard/apps/Apps.tsx b/dashboard/src/main/home/app-dashboard/apps/Apps.tsx index 3d1cf066b3..de6c8fa056 100644 --- a/dashboard/src/main/home/app-dashboard/apps/Apps.tsx +++ b/dashboard/src/main/home/app-dashboard/apps/Apps.tsx @@ -1,6 +1,13 @@ -import React, { useCallback, useContext, useMemo, useState } from "react"; +import React, { + useCallback, + useContext, + useEffect, + useMemo, + useState, +} from "react"; import { AddonWithEnvVars } from "@porter-dev/api-contracts"; import { useQueries } from "@tanstack/react-query"; +import dayjs from "dayjs"; import { useHistory } from "react-router"; import styled from "styled-components"; import { z } from "zod"; @@ -27,7 +34,7 @@ import { useDeploymentTargetList, type DeploymentTarget, } from "lib/hooks/useDeploymentTarget"; -import { checkIfProjectHasPayment } from "lib/hooks/useStripe"; +import { checkIfProjectHasPayment, useCustomerPlan } from "lib/hooks/useStripe"; import api from "shared/api"; import { Context } from "shared/Context"; @@ -58,7 +65,8 @@ const Apps: React.FC = () => { const { deploymentTargetList } = useDeploymentTargetList({ preview: false }); const [deploymentTargetIdFilter, setDeploymentTargetIdFilter] = useState("all"); - const { hasPaymentEnabled } = checkIfProjectHasPayment(); + const { hasPaymentEnabled, refetchPaymentEnabled } = + checkIfProjectHasPayment(); const history = useHistory(); @@ -69,6 +77,24 @@ const Apps: React.FC = () => { const [envDeleting, setEnvDeleting] = useState(false); const [showBillingModal, setShowBillingModal] = useState(false); + const { plan } = useCustomerPlan(); + + const isTrialExpired = (timestamp: string): boolean => { + if (timestamp === "") { + return true; + } + const timestampDate = dayjs(timestamp); + return timestampDate.isBefore(dayjs(new Date())); + }; + const trialExpired = + plan != null && isTrialExpired(plan.trial_info.ending_before); + + useEffect(() => { + if (trialExpired && !hasPaymentEnabled) { + setShowBillingModal(true); + } + }, []); + const [{ data: apps = [], status }, { data: addons = [] }] = useQueries({ queries: [ { @@ -257,7 +283,9 @@ const Apps: React.FC = () => { Get started by creating an application. - {currentProject?.sandbox_enabled && currentProject?.billing_enabled && !hasPaymentEnabled ? ( + {currentProject?.sandbox_enabled && + currentProject?.billing_enabled && + !hasPaymentEnabled ? (