diff --git a/src/app/admin/subscriptions/createSubscription/page.tsx b/src/app/admin/subscriptions/createSubscription/page.tsx new file mode 100644 index 0000000..f6efe61 --- /dev/null +++ b/src/app/admin/subscriptions/createSubscription/page.tsx @@ -0,0 +1,180 @@ +"use client"; +import React, { useMemo, useState } from 'react'; +import PageLayout from '../../../components/PageLayout'; +import useCoffeeManagement from '../hooks/useCoffeeManagement'; +import { PhotoIcon } from '@heroicons/react/24/solid'; +import Link from 'next/link'; +import { useRouter } from 'next/navigation'; + +export default function CreateSubscriptionPage() { + const { createProduct } = useCoffeeManagement(); + const router = useRouter(); + + const [error, setError] = useState(null); + + // form state + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [quantity, setQuantity] = useState(1); + const [price, setPrice] = useState(0); + const [state, setState] = useState<'available'|'unavailable'>('available'); + const [pictureFile, setPictureFile] = useState(undefined); + const [currency, setCurrency] = useState('EUR'); + const [taxRate, setTaxRate] = useState(undefined); + const [isFeatured, setIsFeatured] = useState(false); + const [billingInterval, setBillingInterval] = useState<'day'|'week'|'month'|'year'|''>(''); + const [intervalCount, setIntervalCount] = useState(undefined); + const [sku, setSku] = useState(''); + const [slug, setSlug] = useState(''); + + const onCreate = async (e: React.FormEvent) => { + e.preventDefault(); + setError(null); + try { + const normalizedIntervalCount = billingInterval ? (intervalCount && intervalCount > 0 ? intervalCount : 1) : undefined; + await createProduct({ + title, + description, + quantity, + price, + currency, + tax_rate: taxRate, + is_featured: isFeatured, + billing_interval: billingInterval || undefined, + interval_count: normalizedIntervalCount, + sku: sku || undefined, + slug: slug || undefined, + state: state === 'available', + pictureFile + }); + router.push('/admin/subscriptions'); + } catch (e: any) { + setError(e.message || 'Failed to create'); + } + }; + + return ( + +
+
+
+
+

Create Subscription

+

Add a new product or subscription plan.

+
+ + Back to list + +
+ +
+
+
+
+ + setTitle(e.target.value)} /> +
+
+ + setQuantity(Number(e.target.value))} /> +
+ +
+ + setPrice(Number(e.target.value))} /> +
+
+ + setCurrency(e.target.value.toUpperCase().slice(0,3))} /> +
+ +
+ + setTaxRate(e.target.value === '' ? undefined : Number(e.target.value))} /> +
+
+ setIsFeatured(e.target.checked)} /> + +
+ +
+ + +
+
+ + setIntervalCount(e.target.value === '' ? undefined : Number(e.target.value))} disabled={!billingInterval} /> +
+ +
+ + setSku(e.target.value)} /> +
+
+ + setSlug(e.target.value)} /> +
+ +
+ + +
+
+ +
+ +