import Input from "@/components/Low/Input"; import Modal from "@/components/Modal"; import usePackages from "@/hooks/usePackages"; import { Module } from "@/interfaces"; import { Package } from "@/interfaces/paypal"; import { User } from "@/interfaces/user"; import { createColumnHelper, flexRender, getCoreRowModel, useReactTable, } from "@tanstack/react-table"; import axios from "axios"; import { capitalize } from "lodash"; import { useCallback, useMemo, useState } from "react"; import { BsPencil, BsTrash } from "react-icons/bs"; import { toast } from "react-toastify"; import Select from "react-select"; import { CURRENCIES } from "@/resources/paypal"; import Button from "@/components/Low/Button"; const CLASSES: { [key in Module]: string } = { reading: "text-ielts-reading", listening: "text-ielts-listening", speaking: "text-ielts-speaking", writing: "text-ielts-writing", level: "text-ielts-level", }; const columnHelper = createColumnHelper(); type DurationUnit = "days" | "weeks" | "months" | "years"; const currencyOptions = CURRENCIES.map(({ label, currency }) => ({ value: currency, label, })); function PackageCreator({ pack, onClose, }: { pack?: Package; onClose: () => void; }) { const [duration, setDuration] = useState(pack?.duration || 1); const [unit, setUnit] = useState( pack?.duration_unit || "months" ); const [price, setPrice] = useState(pack?.price || 0); const [currency, setCurrency] = useState(pack?.currency || "OMR"); const submit = useCallback(() => { (pack ? axios.patch : axios.post)( pack ? `/api/packages/${pack.id}` : "/api/packages", { duration, duration_unit: unit, price, currency, } ) .then(() => { toast.success("New payment has been created successfully!"); onClose(); }) .catch(() => { toast.error("Something went wrong, please try again later!"); }); }, [duration, unit, price, currency, pack, onClose]); const currencyDefaultValue = useMemo(() => { return { value: currency || "EUR", label: CURRENCIES.find((c) => c.currency === currency)?.label || "Euro", }; }, [currency]); return (
setPrice(parseInt(e))} /> setDuration(parseInt(e))} />