/*eslint-disable*/ 'use client'; import Card from '@/components/card/Card'; import DashboardLayout from '@/components/layout'; import { Database } from '@/types_db'; import { createStripePortal } from '@/utils/stripe/server'; import { Badge, Button, Flex, Icon, Link, Text } from '@chakra-ui/react'; import { User } from '@supabase/supabase-js'; import { redirect, usePathname, useRouter } from 'next/navigation'; import { useState } from 'react'; import { MdChevronRight } from 'react-icons/md'; type Subscription = Database['public']['Tables']['subscriptions']['Row']; type Product = Database['public']['Tables']['products']['Row']; type Price = Database['public']['Tables']['prices']['Row']; interface ProductWithPrices extends Product { prices: Price[]; } interface PriceWithProduct extends Price { products: Product | null; } interface SubscriptionWithProduct extends Subscription { prices: PriceWithProduct | null; } interface Props { user: User | null | undefined; products: ProductWithPrices[]; subscription: SubscriptionWithProduct | null | any; userDetails: { [x: string]: any } | null | any; } export default function Subscription(props: Props) { const router = useRouter(); const [isSubmitting, setIsSubmitting] = useState(false); const currentPath = usePathname(); const handleStripePortalRequest = async () => { setIsSubmitting(true); const redirectUrl = await createStripePortal(currentPath); setIsSubmitting(false); return router.push(redirectUrl); }; if (!props.user) { return redirect('/dashboard/signin'); } return ( <> CURRENT PLAN {props.subscription ? ( props.products?.map((product: any) => { const price = product?.prices?.find( (price: any) => price.id === props.subscription?.price_id ); // {props.subscription?.map((subscription:any) => { // const price = subscription?.prices?.find( // (user:any) => user.id === props?.userDetails.id, // ); if (price?.id === props.subscription.price_id) return ( // IN CASE USER HAS PLAN {product?.name ? product.name?.toString() : 'No plan active'} {product?.name ? `You are currently on ${product.name?.toString()}` : "You don't have an active subscription."} $ {price?.unit_amount !== null ? price?.unit_amount / 100 : '0'} {price?.interval === 'year' ? '/year' : price?.interval === 'month' ? '/month' : 'error'} ); }) ) : ( // IN CASE OF NOW PLAN No plan active You don't have an active subscription. $0/month )} Got a question regarding your subscription? Chat with us via{' '} hello@horizon-ui.com. ); }