incr/resources/js/components/Pricing/UpdatePriceForm.tsx

94 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-07-10 17:37:30 +02:00
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import InputError from '@/components/InputError';
import { useForm } from '@inertiajs/react';
import { LoaderCircle } from 'lucide-react';
import { FormEventHandler } from 'react';
interface PriceUpdateFormData {
date: string;
price: string;
[key: string]: string;
}
interface UpdatePriceFormProps {
currentPrice?: number;
className?: string;
2025-07-13 01:07:16 +02:00
onSuccess?: () => void;
2025-07-10 17:37:30 +02:00
}
2025-07-13 01:07:16 +02:00
export default function UpdatePriceForm({ currentPrice, className, onSuccess }: UpdatePriceFormProps) {
2025-07-10 17:37:30 +02:00
const { data, setData, post, processing, errors } = useForm<PriceUpdateFormData>({
date: new Date().toISOString().split('T')[0], // Today's date in YYYY-MM-DD format
price: currentPrice?.toString() || '',
});
const submit: FormEventHandler = (e) => {
e.preventDefault();
post(route('pricing.update'), {
onSuccess: () => {
// Keep the date, reset only price if needed
// User might want to update same day multiple times
2025-07-13 01:07:16 +02:00
if (onSuccess) onSuccess();
2025-07-10 17:37:30 +02:00
},
});
};
return (
<Card className={className}>
<CardHeader>
<CardTitle>Update Asset Price</CardTitle>
{currentPrice && (
<p className="text-sm text-neutral-600 dark:text-neutral-400">
Current price: {currentPrice.toFixed(4)}
</p>
)}
</CardHeader>
<CardContent>
<form onSubmit={submit} className="space-y-4">
<div>
<Label htmlFor="date">Price Date</Label>
<Input
id="date"
type="date"
value={data.date}
onChange={(e) => setData('date', e.target.value)}
max={new Date().toISOString().split('T')[0]}
/>
<InputError message={errors.date} />
</div>
<div>
<Label htmlFor="price">Asset Price ()</Label>
<Input
id="price"
type="number"
step="0.0001"
min="0"
placeholder="123.4567"
value={data.price}
onChange={(e) => setData('price', e.target.value)}
/>
<p className="text-xs text-neutral-500 mt-1">
Price per unit/share of the asset
</p>
<InputError message={errors.price} />
</div>
<Button
type="submit"
disabled={processing}
className="w-full"
>
{processing && <LoaderCircle className="mr-2 h-4 w-4 animate-spin" />}
Update Price
</Button>
</form>
</CardContent>
</Card>
);
}