/* eslint-disable @next/next/no-img-element */ import useUsers from "@/hooks/useUsers"; import { User } from "@/interfaces/user"; import clsx from "clsx"; import { capitalize } from "lodash"; import { useEffect, useMemo, useState } from "react"; import useInvites from "@/hooks/useInvites"; import { BsArrowRepeat } from "react-icons/bs"; import InviteCard from "@/components/Medium/InviteCard"; import { useRouter } from "next/router"; import { ToastContainer } from "react-toastify"; import PaymobPayment from "@/components/PaymobPayment"; import moment from "moment"; import { EntityWithRoles } from "@/interfaces/entity"; import { Discount, Package } from "@/interfaces/paypal"; import { isAdmin } from "@/utils/users"; import { useAllowedEntities } from "@/hooks/useEntityPermissions"; import Select from "@/components/Low/Select"; interface Props { user: User; discounts: Discount[]; packages: Package[]; entities: EntityWithRoles[]; hasExpired?: boolean; reload: () => void; } export default function PaymentDue({ user, discounts = [], entities = [], packages = [], hasExpired = false, reload, }: Props) { const [isLoading, setIsLoading] = useState(false); const [entity, setEntity] = useState(); const router = useRouter(); const { users } = useUsers(); const { invites, isLoading: isInvitesLoading, reload: reloadInvites, } = useInvites({ to: user?.id }); const isIndividual = useMemo(() => { if (isAdmin(user)) return false; if (user?.type !== "student") return false; return user.entities.length === 0; }, [user]); const appliedDiscount = useMemo(() => { const biggestDiscount = [...discounts] .sort((a, b) => b.percentage - a.percentage) .shift(); if ( !biggestDiscount || (biggestDiscount.validUntil && moment(biggestDiscount.validUntil).isBefore(moment())) ) return 0; return biggestDiscount.percentage; }, [discounts]); const entitiesThatCanBePaid = useAllowedEntities( user, entities, "pay_entity" ); useEffect(() => { if (entitiesThatCanBePaid.length > 0) setEntity(entitiesThatCanBePaid[0]); }, [entitiesThatCanBePaid]); return ( <> {isLoading && (
Completing your payment... If you canceled your payment or it failed, please click the button below to restart
)} <> {invites.length > 0 && (
Invites
{invites.map((invite) => ( { reloadInvites(); router.reload(); }} /> ))}
)}
{hasExpired && ( You do not have time credits for your account type! )} {isIndividual && (
To add to your use of EnCoach, please purchase one of the time packages available below:
{packages.map((p) => (
EnCoach's Logo EnCoach - {p.duration}{" "} {capitalize( p.duration === 1 ? p.duration_unit.slice( 0, p.duration_unit.length - 1 ) : p.duration_unit )}
{appliedDiscount === 0 && ( {p.price} {p.currency} )} {appliedDiscount > 0 && (
{p.price} {p.currency} {( p.price - p.price * (appliedDiscount / 100) ).toFixed(2)}{" "} {p.currency}
)} { setTimeout(reload, 500); }} currency={p.currency} duration={p.duration} duration_unit={p.duration_unit} price={ +( p.price - p.price * (appliedDiscount / 100) ).toFixed(2) } />
This includes:
  • - Train your abilities for the IELTS exam
  • - Gain insights into your weaknesses and strengths
  • - Allow yourself to correctly prepare for the exam
))}
)} {!isIndividual && entitiesThatCanBePaid.length > 0 && entity?.payment && (
({ value: e.id, label: e.label, entity: e, }))} onChange={(e) => (e?.value ? setEntity(e?.entity) : null)} className="!w-full max-w-[400px] self-center" />
An admin nor your agent have yet set the price intended to your requirements in terms of the amount of users you desire and your expected monthly duration. Please try again later or contact your agent or an admin, thank you for your patience.
)}
); }