import FieldLabel from "@/components/general/modal-components/FieldLabel"; import { SelectContent } from "@/components/ui/select"; import { SelectTrigger, SelectValue } from "@/components/ui/select"; import { SelectItem } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogTrigger, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Select } from "@/components/ui/select"; import React, { useEffect, useState } from "react"; import { Input } from "@/components/ui/input"; import { FeatureService } from "@/services/FeatureService"; import { useAxiosInstance } from "@/services/useAxiosInstance"; import toast from "react-hot-toast"; import { slugify } from "@/utils/formatUtils/formatTextUtils"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faXmark } from "@fortawesome/pro-solid-svg-icons"; import { useCreditsContext } from "./CreditsContext"; import { PlusIcon } from "lucide-react"; import { Feature, FeatureType, CreditSystemConfig } from "@autumn/shared"; import { getBackendErr } from "@/utils/genUtils"; const defaultFields = { name: "", id: "", }; const defaultConfig = { schema: [{ metered_feature_id: "", feature_amount: 0, credit_amount: 0 }], }; function CreateCreditSystem() { const { features, mutate, env } = useCreditsContext(); const axiosInstance = useAxiosInstance({ env: env }); const [fields, setFields] = useState(defaultFields); const [creditSystemConfig, setCreditSystemConfig] = useState(defaultConfig); const [isLoading, setIsLoading] = useState(false); const [idChanged, setIdChanged] = useState(false); const [open, setOpen] = useState(false); useEffect(() => { setFields(defaultFields); setCreditSystemConfig(defaultConfig); setIdChanged(false); }, []); const handleSubmit = async () => { if (!fields.id || !fields.name) { toast.error("Please fill in all fields"); return; } if (creditSystemConfig.schema.length === 0) { toast.error("Need at least one metered feature"); return; } for (const item of creditSystemConfig.schema) { if (!item.metered_feature_id) { toast.error("Select a metered feature"); return; } if (item.feature_amount <= 0 || item.credit_amount <= 0) { toast.error("Feature amount and credit amount must be greater than 0"); return; } } setIsLoading(true); try { await FeatureService.createFeature(axiosInstance, { name: fields.name, id: fields.id, type: FeatureType.CreditSystem, config: creditSystemConfig, }); await mutate(); setOpen(false); } catch (error) { toast.error(getBackendErr(error, "Failed to create credit system")); } setIsLoading(false); }; const addSchemaItem = () => { setCreditSystemConfig({ ...creditSystemConfig, schema: [ ...creditSystemConfig.schema, { metered_feature_id: "", feature_amount: 0, credit_amount: 0 }, ], }); }; const removeSchemaItem = (index: number) => { setCreditSystemConfig({ ...creditSystemConfig, schema: creditSystemConfig.schema.filter((_, i) => i !== index), }); }; const handleSchemaChange = (index: number, field: string, value: string) => { const newSchema = [...creditSystemConfig.schema]; newSchema[index] = { ...newSchema[index], [field]: value }; setCreditSystemConfig({ ...creditSystemConfig, schema: newSchema }); }; return ( Create Credit System
Name { const newFields: any = { ...fields, name: e.target.value }; if (!idChanged) { newFields.id = slugify(e.target.value); } setFields(newFields); }} />
ID { setFields({ ...fields, id: e.target.value }); setIdChanged(true); }} />
Schema
Meter Metered Amount Credit Amount

{creditSystemConfig.schema.map((item, index) => ( handleSchemaChange( index, "feature_amount", e.target.value ) } /> handleSchemaChange(index, "credit_amount", e.target.value) } />
))}
); } export default CreateCreditSystem;