CreateLocationPage.tsx
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useState, type FormEvent } from 'react'
import { useNavigate } from 'react-router-dom'
import { inventory } from '@/api/client'
import { PageHeader } from '@/components/PageHeader'
import { ErrorBox } from '@/components/ErrorBox'
const LOCATION_TYPES = ['WAREHOUSE', 'BIN', 'VIRTUAL'] as const
export function CreateLocationPage() {
const navigate = useNavigate()
const [code, setCode] = useState('')
const [name, setName] = useState('')
const [type, setType] = useState<string>('WAREHOUSE')
const [submitting, setSubmitting] = useState(false)
const [error, setError] = useState<Error | null>(null)
const onSubmit = async (e: FormEvent) => {
e.preventDefault()
setError(null)
setSubmitting(true)
try {
await inventory.createLocation({ code, name, type })
navigate('/locations')
} catch (err: unknown) {
setError(err instanceof Error ? err : new Error(String(err)))
} finally {
setSubmitting(false)
}
}
return (
<div>
<PageHeader
title="New Location"
subtitle="Add a warehouse, bin, or virtual location for inventory tracking."
actions={<button className="btn-secondary" onClick={() => navigate('/locations')}>Cancel</button>}
/>
<form onSubmit={onSubmit} className="card p-6 space-y-4 max-w-lg">
<div>
<label className="block text-sm font-medium text-slate-700">Location code</label>
<input type="text" required value={code} onChange={(e) => setCode(e.target.value)}
placeholder="WH-NEW" className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm" />
</div>
<div>
<label className="block text-sm font-medium text-slate-700">Name</label>
<input type="text" required value={name} onChange={(e) => setName(e.target.value)}
placeholder="New Warehouse" className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm" />
</div>
<div>
<label className="block text-sm font-medium text-slate-700">Type</label>
<select value={type} onChange={(e) => setType(e.target.value)}
className="mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm">
{LOCATION_TYPES.map((t) => <option key={t} value={t}>{t}</option>)}
</select>
</div>
{error && <ErrorBox error={error} />}
<button type="submit" className="btn-primary" disabled={submitting}>
{submitting ? 'Creating...' : 'Create Location'}
</button>
</form>
</div>
)
}