feat: implement public order details page with submission view, order actions, and sidebar branding updates

This commit is contained in:
Firman Ramdhani
2026-09-03 14:54:33 +07:00
parent d32e7eda91
commit ff57d52dde
11 changed files with 643 additions and 288 deletions
+278
View File
@@ -0,0 +1,278 @@
'use client'
import { useState, useEffect } from 'react'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
import { DialogContent, DialogTitle } from '@/components/ui/dialog'
import { Checkbox } from '@/components/ui/checkbox'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { submitOrder, getUserSubmission, getSessionUser } from '@/app/actions'
import { PlusCircle, MinusCircle, CheckCircle2 } from 'lucide-react'
export function OrderFormModal({ order, onSuccess }: { order: any, onSuccess: () => void }) {
const [userId, setUserId] = useState<string>('')
const [items, setItems] = useState<Record<string, { selected: boolean, qty: number }>>({})
const [customItems, setCustomItems] = useState<Array<{ id: string, name: string, qty: number }>>([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState('')
useEffect(() => {
const init = async () => {
const user = await getSessionUser()
if (user?.id) {
setUserId(user.id)
loadExistingSubmission(user.id)
}
}
init()
}, [order.id])
const loadExistingSubmission = async (uid: string) => {
const sub = await getUserSubmission(order.id, uid)
if (sub) {
const newItems: any = { ...items }
const newCustoms: any[] = []
sub.items.forEach((item: any) => {
if (!item.is_custom) {
const stdItem = order.available_items.find((ai: any) => ai.name === item.name)
if (stdItem) {
newItems[stdItem.id] = { selected: true, qty: item.qty }
}
} else {
newCustoms.push({ id: Math.random().toString(), name: item.name, qty: item.qty })
}
})
setItems(newItems)
setCustomItems(newCustoms)
}
}
const handleStandardItemToggle = (itemId: string, checked: boolean) => {
setItems(prev => ({
...prev,
[itemId]: { selected: checked, qty: checked ? 1 : 0 }
}))
}
const handleStandardItemQty = (itemId: string, qty: number) => {
if (qty < 1) {
handleStandardItemToggle(itemId, false)
return
}
setItems(prev => ({
...prev,
[itemId]: { selected: true, qty }
}))
}
const addCustomItem = () => {
setCustomItems([...customItems, { id: Math.random().toString(), name: '', qty: 1 }])
}
const updateCustomItem = (id: string, field: 'name' | 'qty', value: any) => {
setCustomItems(customItems.map(c => c.id === id ? { ...c, [field]: value } : c))
}
const removeCustomItem = (id: string) => {
setCustomItems(customItems.filter(c => c.id !== id))
}
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError('')
setLoading(true)
const payloadItems: any[] = []
order.available_items.forEach((ai: any) => {
const state = items[ai.id]
if (state?.selected && state.qty > 0) {
payloadItems.push({ name: ai.name, qty: state.qty, is_custom: false })
}
})
customItems.forEach(ci => {
if (ci.name.trim() && ci.qty > 0) {
payloadItems.push({ name: ci.name.trim(), qty: ci.qty, is_custom: true })
}
})
if (payloadItems.length === 0) {
setError('Harap pilih minimal 1 item atau tambahkan item lainnya.')
setLoading(false)
return
}
const res = await submitOrder({
order_id: order.id,
user_id: userId,
items: payloadItems
})
if (res.success) {
onSuccess()
} else {
setError(res.error || 'Gagal menyimpan pesanan.')
}
setLoading(false)
}
return (
<DialogContent className="sm:max-w-[520px] p-0 overflow-hidden rounded-3xl border-slate-200/90 dark:border-slate-800 shadow-2xl max-h-[90vh] flex flex-col">
<div className="bg-gradient-to-br from-[#1B2CC1] to-[#121E85] p-6 text-white">
<span className="text-[11px] font-bold uppercase tracking-wider text-blue-200">Form Titipan</span>
<DialogTitle className="text-2xl font-black tracking-tight text-white mt-1">
{order.title}
</DialogTitle>
<p className="text-xs text-blue-100 mt-1">
Pilih menu yang tersedia di bawah atau tambahkan item khusus jika diizinkan.
</p>
</div>
<form onSubmit={handleSubmit} className="p-6 overflow-y-auto space-y-6 flex-1 bg-white dark:bg-slate-900">
{/* Standard Items */}
<div className="space-y-3">
<Label className="text-xs font-bold uppercase tracking-wider text-slate-400">
Daftar Menu Tersedia
</Label>
<div className="space-y-2">
{order.available_items.map((item: any) => {
const isSelected = items[item.id]?.selected || false
const qty = items[item.id]?.qty || 0
return (
<div
key={item.id}
className={cn(
"flex items-center justify-between p-3.5 rounded-xl border transition-all",
isSelected
? "border-[#1B2CC1] bg-[#1B2CC1]/5 dark:bg-blue-950/20 shadow-sm"
: "border-slate-200/80 dark:border-slate-800 hover:bg-slate-50 dark:hover:bg-slate-800/50"
)}
>
<div className="flex items-center gap-3 flex-1 min-w-0">
<Checkbox
id={`item-${item.id}`}
checked={isSelected}
onCheckedChange={(c) => handleStandardItemToggle(item.id, c as boolean)}
className="rounded-lg data-[state=checked]:bg-[#1B2CC1] data-[state=checked]:border-[#1B2CC1]"
/>
<Label htmlFor={`item-${item.id}`} className="text-sm font-bold text-slate-800 dark:text-slate-200 cursor-pointer truncate">
{item.name}
</Label>
</div>
{isSelected && (
<div className="flex items-center gap-1 bg-white dark:bg-slate-800 border border-slate-200 dark:border-slate-700 rounded-lg p-0.5 shadow-sm">
<Button
type="button"
variant="ghost"
size="icon"
className="h-7 w-7 text-slate-500 hover:text-slate-800"
onClick={() => handleStandardItemQty(item.id, qty - 1)}
>
<MinusCircle className="h-4 w-4" />
</Button>
<span className="w-7 text-center text-xs font-extrabold text-[#1B2CC1] dark:text-blue-400">{qty}</span>
<Button
type="button"
variant="ghost"
size="icon"
className="h-7 w-7 text-slate-500 hover:text-slate-800"
onClick={() => handleStandardItemQty(item.id, qty + 1)}
>
<PlusCircle className="h-4 w-4" />
</Button>
</div>
)}
</div>
)
})}
{order.available_items.length === 0 && (
<p className="text-xs text-slate-400 text-center py-3 bg-slate-50 dark:bg-slate-800/40 rounded-xl border border-dashed border-slate-200 dark:border-slate-800">
Tidak ada menu standar yang ditentukan.
</p>
)}
</div>
</div>
{/* Custom Items */}
{order.allow_custom && (
<div className="space-y-3">
<div className="flex items-center justify-between">
<Label className="text-xs font-bold uppercase tracking-wider text-slate-400">
Item Tambahan (Kustom)
</Label>
<Button
type="button"
variant="outline"
size="sm"
onClick={addCustomItem}
className="h-8 text-xs font-bold rounded-lg border-dashed border-[#1B2CC1]/40 text-[#1B2CC1] hover:bg-[#1B2CC1]/10 gap-1.5"
>
<PlusCircle className="h-3.5 w-3.5" /> Tambah Kustom
</Button>
</div>
{customItems.length > 0 ? (
<div className="space-y-2.5">
{customItems.map((ci, idx) => (
<div key={ci.id} className="flex gap-2 items-center p-3 rounded-xl border border-slate-200/90 dark:border-slate-800 bg-slate-50/70 dark:bg-slate-800/40">
<span className="text-xs font-bold text-slate-400 w-4">{idx + 1}.</span>
<Input
placeholder="Nama Menu / Catatan Khusus"
value={ci.name}
onChange={(e) => updateCustomItem(ci.id, 'name', e.target.value)}
className="flex-1 h-9 rounded-lg bg-white dark:bg-slate-900 text-xs font-medium"
/>
<Input
type="number"
min="1"
value={ci.qty}
onChange={(e) => updateCustomItem(ci.id, 'qty', parseInt(e.target.value) || 1)}
className="w-16 h-9 rounded-lg bg-white dark:bg-slate-900 text-center font-bold text-xs"
/>
<Button
type="button"
variant="ghost"
size="icon"
onClick={() => removeCustomItem(ci.id)}
className="h-8 w-8 text-red-500 hover:text-red-700 hover:bg-red-50 dark:hover:bg-red-950/30 rounded-lg"
>
<MinusCircle className="h-4 w-4" />
</Button>
</div>
))}
</div>
) : (
<div
onClick={addCustomItem}
className="p-4 border-2 border-dashed border-slate-200 dark:border-slate-800 hover:border-[#1B2CC1]/50 rounded-2xl text-center bg-slate-50/50 dark:bg-slate-800/30 cursor-pointer transition-all group"
>
<PlusCircle className="w-5 h-5 text-slate-400 group-hover:text-[#1B2CC1] mx-auto mb-1 transition-colors" />
<p className="text-xs font-bold text-slate-600 dark:text-slate-300">Klik untuk Tambah Item Custom</p>
<p className="text-[11px] text-slate-400 mt-0.5">Ingin titip menu lain? Masukkan nama dan kuantitasnya di sini.</p>
</div>
)}
</div>
)}
{error && (
<div className="p-3 bg-red-50 dark:bg-red-950/30 border border-red-200 dark:border-red-900 rounded-xl text-xs text-red-600 dark:text-red-400 font-semibold">
{error}
</div>
)}
<div className="flex justify-end gap-3 pt-2 border-t border-slate-100 dark:border-slate-800">
<Button
type="submit"
disabled={loading}
className="w-full h-11 rounded-xl bg-[#1B2CC1] hover:bg-[#15229E] text-white font-bold shadow-md shadow-[#1B2CC1]/25"
>
{loading ? 'Menyimpan Titipan...' : 'Kirim Titip Pesanan'}
</Button>
</div>
</form>
</DialogContent>
)
}