207 lines
6.2 KiB
TypeScript
207 lines
6.2 KiB
TypeScript
'use client'
|
|
|
|
// apps/admin/src/components/subscription-form.tsx
|
|
// 구독 생성/수정 공용 폼
|
|
|
|
import { useEffect, useRef, useState } from 'react'
|
|
import {
|
|
Box,
|
|
TextField,
|
|
Button,
|
|
FormControl,
|
|
InputLabel,
|
|
Select,
|
|
MenuItem,
|
|
Alert,
|
|
} from '@mui/material'
|
|
import { MetalCard, PhosphorText } from '@d3ro/ui/components/ds'
|
|
import { d3roPalette, d3roFontMono } from '@d3ro/ui/theme'
|
|
import { callAdminApi } from '@/lib/admin-api'
|
|
import { parseOptionalIsoDate } from '@/lib/admin-url-safety'
|
|
|
|
type Tier = 'free' | 'pro' | 'pro_plus'
|
|
type SubStatus = 'active' | 'canceled' | 'past_due' | 'expired'
|
|
|
|
interface SubscriptionFormProps {
|
|
mode: 'create' | 'edit'
|
|
userId: string
|
|
initial?: {
|
|
tier: Tier
|
|
status: SubStatus
|
|
currentPeriodEnd: string | null
|
|
overageCredits: number
|
|
adminNote: string | null
|
|
}
|
|
onSuccess: () => void
|
|
}
|
|
|
|
export function SubscriptionForm({ mode, userId, initial, onSuccess }: SubscriptionFormProps): React.ReactElement {
|
|
const [tier, setTier] = useState<Tier>(initial?.tier ?? 'free')
|
|
const [status, setStatus] = useState<SubStatus>(initial?.status ?? 'active')
|
|
const [periodEnd, setPeriodEnd] = useState(initial?.currentPeriodEnd?.split('T')[0] ?? '')
|
|
const [overageCredits, setOverageCredits] = useState(initial?.overageCredits ?? 0)
|
|
const [adminNote, setAdminNote] = useState(initial?.adminNote ?? '')
|
|
const [memo, setMemo] = useState('')
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
const [success, setSuccess] = useState(false)
|
|
const mountedRef = useRef(true)
|
|
|
|
useEffect(() => {
|
|
mountedRef.current = true
|
|
return () => {
|
|
mountedRef.current = false
|
|
}
|
|
}, [])
|
|
|
|
const handleSubmit = async (): Promise<void> => {
|
|
if (!memo.trim()) return
|
|
|
|
const parsedEnd = parseOptionalIsoDate(periodEnd)
|
|
if (!parsedEnd.ok) {
|
|
setError(parsedEnd.message)
|
|
return
|
|
}
|
|
|
|
setLoading(true)
|
|
setError(null)
|
|
setSuccess(false)
|
|
|
|
try {
|
|
if (mode === 'create') {
|
|
await callAdminApi('admin-subscriptions', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
userId,
|
|
tier,
|
|
status,
|
|
currentPeriodEnd: parsedEnd.iso,
|
|
adminNote: adminNote || undefined,
|
|
memo: memo.trim(),
|
|
}),
|
|
})
|
|
} else {
|
|
await callAdminApi(`admin-subscriptions?userId=${encodeURIComponent(userId)}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify({
|
|
tier,
|
|
status,
|
|
currentPeriodEnd: parsedEnd.iso,
|
|
overageCredits,
|
|
adminNote: adminNote || undefined,
|
|
memo: memo.trim(),
|
|
}),
|
|
})
|
|
}
|
|
if (!mountedRef.current) return
|
|
setSuccess(true)
|
|
setMemo('')
|
|
onSuccess()
|
|
} catch (err) {
|
|
if (mountedRef.current) {
|
|
setError(err instanceof Error ? err.message : 'Operation failed')
|
|
}
|
|
} finally {
|
|
if (mountedRef.current) {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
const inputSx = {
|
|
'& .MuiInputBase-root': {
|
|
fontFamily: d3roFontMono,
|
|
fontSize: 13,
|
|
color: d3roPalette.text.primary,
|
|
},
|
|
}
|
|
|
|
return (
|
|
<MetalCard>
|
|
<Box sx={{ p: 2, display: 'flex', flexDirection: 'column', gap: 2 }}>
|
|
<PhosphorText variant="label">
|
|
{mode === 'create' ? 'CREATE SUBSCRIPTION' : 'EDIT SUBSCRIPTION'}
|
|
</PhosphorText>
|
|
|
|
<FormControl fullWidth sx={inputSx}>
|
|
<InputLabel id="subscription-tier-label" sx={{ fontFamily: d3roFontMono }}>Tier</InputLabel>
|
|
<Select id="subscription-tier" labelId="subscription-tier-label" value={tier} onChange={(e) => setTier(e.target.value as Tier)} label="Tier">
|
|
<MenuItem value="free">FREE</MenuItem>
|
|
<MenuItem value="pro">PRO</MenuItem>
|
|
<MenuItem value="pro_plus">PRO+</MenuItem>
|
|
</Select>
|
|
</FormControl>
|
|
|
|
<FormControl fullWidth sx={inputSx}>
|
|
<InputLabel id="subscription-status-label" sx={{ fontFamily: d3roFontMono }}>Status</InputLabel>
|
|
<Select id="subscription-status" labelId="subscription-status-label" value={status} onChange={(e) => setStatus(e.target.value as SubStatus)} label="Status">
|
|
<MenuItem value="active">ACTIVE</MenuItem>
|
|
<MenuItem value="canceled">CANCELED</MenuItem>
|
|
<MenuItem value="past_due">PAST DUE</MenuItem>
|
|
<MenuItem value="expired">EXPIRED</MenuItem>
|
|
</Select>
|
|
</FormControl>
|
|
|
|
<TextField
|
|
fullWidth
|
|
label="Period End"
|
|
type="date"
|
|
value={periodEnd}
|
|
onChange={(e) => setPeriodEnd(e.target.value)}
|
|
slotProps={{ inputLabel: { shrink: true } }}
|
|
sx={inputSx}
|
|
/>
|
|
|
|
{mode === 'edit' && (
|
|
<TextField
|
|
fullWidth
|
|
label="Overage Credits"
|
|
type="number"
|
|
value={overageCredits}
|
|
onChange={(e) => setOverageCredits(parseInt(e.target.value, 10) || 0)}
|
|
sx={inputSx}
|
|
/>
|
|
)}
|
|
|
|
<TextField
|
|
fullWidth
|
|
label="Admin Note"
|
|
multiline
|
|
rows={2}
|
|
value={adminNote}
|
|
onChange={(e) => setAdminNote(e.target.value)}
|
|
sx={inputSx}
|
|
/>
|
|
|
|
<TextField
|
|
fullWidth
|
|
label="Memo (required for audit log)"
|
|
multiline
|
|
rows={2}
|
|
value={memo}
|
|
onChange={(e) => setMemo(e.target.value)}
|
|
placeholder="Reason for this action..."
|
|
sx={{
|
|
...inputSx,
|
|
'& .MuiInputBase-root': {
|
|
...inputSx['& .MuiInputBase-root'],
|
|
bgcolor: d3roPalette.bg.inset,
|
|
},
|
|
}}
|
|
/>
|
|
|
|
{error && <Alert severity="error" sx={{ fontFamily: d3roFontMono }}>{error}</Alert>}
|
|
{success && <Alert severity="success" sx={{ fontFamily: d3roFontMono }}>Operation successful</Alert>}
|
|
|
|
<Button
|
|
variant="contained"
|
|
onClick={() => void handleSubmit()}
|
|
disabled={!memo.trim() || loading}
|
|
sx={{ fontFamily: d3roFontMono, alignSelf: 'flex-end' }}
|
|
>
|
|
{loading ? 'Processing...' : mode === 'create' ? 'Create' : 'Update'}
|
|
</Button>
|
|
</Box>
|
|
</MetalCard>
|
|
)
|
|
}
|