frontend fully integreated with the backend

This commit is contained in:
Kalana B. Thilakarathna
2026-08-25 23:47:33 +05:30
parent 1bee5f91c6
commit 83d3272784
151 changed files with 10724 additions and 573 deletions
@@ -0,0 +1,154 @@
'use client'
import { useEffect } from 'react'
import { useForm, zodResolver, z } from '@/lib/form'
import { FormField } from '@/components/shared/form-field'
import { Input } from '@/components/ui/input'
import { Checkbox } from '@/components/ui/checkbox'
import { Button } from '@/components/ui/button'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from '@/components/ui/dialog'
import { FieldGroup } from '@/components/ui/field'
import { toast } from '@/components/ui/toast'
import { suppliers as suppliersApi, getApiError } from '@/lib/api/index'
import type { SupplierResponse } from '@/types/api'
const schema = z.object({
name: z.string().min(1, 'Name is required'),
contactInfo: z.string().optional(),
isActive: z.boolean(),
})
type FormValues = z.infer<typeof schema>
interface SupplierSheetProps {
open: boolean
onClose: () => void
supplier: SupplierResponse | null
onSuccess: () => void
}
export function SupplierSheet({ open, onClose, supplier, onSuccess }: SupplierSheetProps) {
const isEdit = !!supplier
const form = useForm<FormValues>({
resolver: zodResolver(schema),
defaultValues: { name: '', contactInfo: '', isActive: true },
})
useEffect(() => {
if (open) {
form.reset(
supplier
? { name: supplier.name, contactInfo: supplier.contactInfo ?? '', isActive: supplier.isActive }
: { name: '', contactInfo: '', isActive: true }
)
}
}, [open, supplier])
async function onSubmit(values: FormValues) {
try {
const body = {
...values,
contactInfo: values.contactInfo || null,
}
if (isEdit) {
await suppliersApi.update(supplier!.id, body)
toast.add({ title: 'Supplier updated', type: 'success' })
} else {
await suppliersApi.create(body)
toast.add({ title: 'Supplier created', type: 'success' })
}
onSuccess()
onClose()
} catch (err) {
toast.add({ title: 'Failed to save', description: getApiError(err), type: 'error' })
}
}
return (
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
<DialogContent className="overflow-y-auto sm:max-w-lg">
<DialogHeader>
<DialogTitle>{isEdit ? 'Edit Supplier' : 'Add Supplier'}</DialogTitle>
<DialogDescription>
{isEdit ? 'Update the supplier details below.' : 'Add a new supplier account.'}
</DialogDescription>
</DialogHeader>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col gap-6">
<FieldGroup>
<FormField
control={form.control}
name="name"
label="Supplier Name"
required
render={({ field, invalid }) => (
<Input
id="name"
name={field.name}
ref={field.ref}
value={field.value as string}
onChange={field.onChange}
onBlur={field.onBlur}
placeholder="e.g. ABC Distributors"
aria-invalid={invalid}
/>
)}
/>
<FormField
control={form.control}
name="contactInfo"
label="Contact Info"
render={({ field, invalid }) => (
<Input
id="contactInfo"
name={field.name}
ref={field.ref}
value={field.value as string}
onChange={field.onChange}
onBlur={field.onBlur}
placeholder="Phone / email / address"
aria-invalid={invalid}
/>
)}
/>
<FormField
control={form.control}
name="isActive"
render={({ field }) => (
<div className="flex items-center gap-2">
<Checkbox
id="isActive"
checked={field.value as boolean}
onCheckedChange={field.onChange}
/>
<label htmlFor="isActive" className="text-sm font-medium cursor-pointer">
Active
</label>
</div>
)}
/>
</FieldGroup>
<DialogFooter>
<Button type="button" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? 'Saving…' : isEdit ? 'Save Changes' : 'Create Supplier'}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
)
}
@@ -0,0 +1,147 @@
'use client'
import { useState, useEffect, useCallback, useMemo } from 'react'
import { HugeiconsIcon } from '@hugeicons/react'
import { Add01Icon } from '@hugeicons/core-free-icons'
import { DataTable } from '@/components/shared/data-table'
import type { TableConfig } from '@/components/shared/data-table'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { toast } from '@/components/ui/toast'
import { suppliers as suppliersApi, getApiError, getPaginatedItems } from '@/lib/api/index'
import type { SupplierResponse } from '@/types/api'
import { SupplierSheet } from './supplier-sheet'
export function SuppliersClient() {
const [data, setData] = useState<SupplierResponse[]>([])
const [loading, setLoading] = useState(true)
const [total, setTotal] = useState(0)
const [page, setPage] = useState(1)
const [pageSize, setPageSize] = useState(20)
const [sheetOpen, setSheetOpen] = useState(false)
const [editing, setEditing] = useState<SupplierResponse | null>(null)
const fetchData = useCallback(async (p: number, ps: number) => {
setLoading(true)
try {
const res = await suppliersApi.list({ page: p, pageSize: ps })
setData(getPaginatedItems(res))
setTotal(res.total)
} catch (err) {
toast.add({ title: 'Failed to load suppliers', description: getApiError(err), type: 'error' })
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
fetchData(page, pageSize)
}, [fetchData, page, pageSize])
function openCreate() {
setEditing(null)
setSheetOpen(true)
}
function openEdit(supplier: SupplierResponse) {
setEditing(supplier)
setSheetOpen(true)
}
const tableConfig = useMemo<TableConfig<SupplierResponse>>(() => ({
rowKey: 'id',
serverSide: true,
stickyHeader: true,
emptyMessage: 'No suppliers yet. Add one to get started.',
pagination: { enabled: true, pageSize, pageSizeOptions: [10, 20, 50] },
columns: [
{
key: 'name',
title: 'Supplier',
fields: [{ key: 'name', primary: true }],
sortable: true,
minWidth: 200,
},
{
key: 'contactInfo',
title: 'Contact',
fields: ['contactInfo'],
minWidth: 180,
cellRender: (row) => (
<span className="text-sm">
{row.contactInfo ?? '—'}
</span>
),
},
{
key: 'status',
title: 'Status',
fields: ['isActive'],
width: 120,
cellRender: (row) => (
<Badge variant={row.isActive ? 'default' : 'secondary'}>
{row.isActive ? 'Active' : 'Inactive'}
</Badge>
),
},
{
key: 'createdAt',
title: 'Created',
fields: [{
key: 'createdAt',
transform: (v) => new Date(v as string).toLocaleDateString('en-GB', {
day: '2-digit', month: 'short', year: 'numeric',
}),
}],
width: 140,
},
],
rowActions: {
actions: () => [
{
action: 'edit',
label: 'Edit supplier',
onClick: openEdit,
},
],
},
}), [pageSize])
return (
<>
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-semibold text-foreground">Suppliers</h1>
<p className="text-sm text-muted-foreground mt-0.5">
Supplier accounts used for raising purchase orders
</p>
</div>
<Button onClick={openCreate}>
<HugeiconsIcon icon={Add01Icon} size={16} />
Add Supplier
</Button>
</div>
<DataTable
config={tableConfig}
data={data}
loading={loading}
totalCount={total}
onServerSideChange={({ page: p }) => {
setPage(p.page)
setPageSize(p.pageSize)
}}
/>
<SupplierSheet
open={sheetOpen}
onClose={() => setSheetOpen(false)}
supplier={editing}
onSuccess={() => fetchData(page, pageSize)}
/>
</>
)
}
@@ -0,0 +1,5 @@
import { SuppliersClient } from './_components/suppliers-client'
export default function SuppliersPage() {
return <SuppliersClient />
}