|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { useForm } from 'react-hook-form'; |
| 5 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 6 | +import { z } from 'zod'; |
| 7 | +import { Button } from '@/components/ui/button'; |
| 8 | +import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'; |
| 9 | +import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; |
| 10 | +import { Input } from '@/components/ui/input'; |
| 11 | +import { useToast } from '@/hooks/use-toast'; |
| 12 | +import { Loader2 } from 'lucide-react'; |
| 13 | +import { sellShares } from '@/lib/actions/companies'; |
| 14 | +import { useRouter } from 'next/navigation'; |
| 15 | + |
| 16 | +interface SellSharesDialogProps { |
| 17 | + companyId: number; |
| 18 | + companyName: string; |
| 19 | + sharePrice: number; |
| 20 | + sharesHeld: number; |
| 21 | + children: React.ReactNode; |
| 22 | +} |
| 23 | + |
| 24 | +const formSchema = z.object({ |
| 25 | + quantity: z.coerce.number().positive({ message: 'La quantité doit être supérieure à zéro.' }), |
| 26 | +}); |
| 27 | + |
| 28 | +export function SellSharesDialog({ companyId, companyName, sharePrice, sharesHeld, children }: SellSharesDialogProps) { |
| 29 | + const [open, setOpen] = useState(false); |
| 30 | + const { toast } = useToast(); |
| 31 | + const router = useRouter(); |
| 32 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 33 | + resolver: zodResolver(formSchema), |
| 34 | + defaultValues: { |
| 35 | + quantity: undefined, |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + const quantity = form.watch('quantity') || 0; |
| 40 | + const proceeds = quantity * sharePrice; |
| 41 | + |
| 42 | + async function onSubmit(values: z.infer<typeof formSchema>) { |
| 43 | + if (values.quantity > sharesHeld) { |
| 44 | + form.setError('quantity', { message: `Vous ne pouvez pas vendre plus que vos ${sharesHeld.toFixed(4)} parts.` }); |
| 45 | + return; |
| 46 | + } |
| 47 | + const result = await sellShares(companyId, values.quantity); |
| 48 | + if (result.error) { |
| 49 | + toast({ variant: 'destructive', title: 'Erreur', description: result.error }); |
| 50 | + } else if (result.success) { |
| 51 | + toast({ title: 'Succès', description: result.success }); |
| 52 | + router.refresh(); |
| 53 | + setOpen(false); |
| 54 | + form.reset(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <Dialog open={open} onOpenChange={(isOpen) => { |
| 60 | + setOpen(isOpen) |
| 61 | + if (!isOpen) form.reset(); |
| 62 | + }}> |
| 63 | + <DialogTrigger asChild>{children}</DialogTrigger> |
| 64 | + <DialogContent> |
| 65 | + <DialogHeader> |
| 66 | + <DialogTitle>Vendre des Parts de {companyName}</DialogTitle> |
| 67 | + <DialogDescription> |
| 68 | + Prix de rachat par part : ${sharePrice.toFixed(2)}. Parts détenues : {sharesHeld.toLocaleString(undefined, {maximumFractionDigits: 4})} |
| 69 | + </DialogDescription> |
| 70 | + </DialogHeader> |
| 71 | + <Form {...form}> |
| 72 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6"> |
| 73 | + <FormField |
| 74 | + control={form.control} |
| 75 | + name="quantity" |
| 76 | + render={({ field }) => ( |
| 77 | + <FormItem> |
| 78 | + <FormLabel>Quantité à vendre</FormLabel> |
| 79 | + <div className="relative"> |
| 80 | + <FormControl> |
| 81 | + <Input |
| 82 | + type="number" |
| 83 | + step="any" |
| 84 | + placeholder="0.0000" |
| 85 | + {...field} |
| 86 | + value={field.value ?? ''} |
| 87 | + onChange={e => field.onChange(e.target.value === '' ? undefined : e.target.valueAsNumber)} /> |
| 88 | + </FormControl> |
| 89 | + <Button type="button" variant="ghost" size="sm" className="absolute right-1 top-1/2 -translate-y-1/2 h-7" onClick={() => form.setValue('quantity', sharesHeld, { shouldValidate: true })}> |
| 90 | + Max |
| 91 | + </Button> |
| 92 | + </div> |
| 93 | + <FormMessage /> |
| 94 | + </FormItem> |
| 95 | + )} |
| 96 | + /> |
| 97 | + |
| 98 | + <div className="text-sm text-muted-foreground"> |
| 99 | + {quantity > 0 ? `Vous recevrez ≈ ${proceeds.toFixed(2)}$ de la trésorerie de l'entreprise.` : 'Entrez une quantité à vendre.'} |
| 100 | + </div> |
| 101 | + |
| 102 | + <DialogFooter> |
| 103 | + <Button type="button" variant="secondary" onClick={() => setOpen(false)}>Annuler</Button> |
| 104 | + <Button type="submit" disabled={form.formState.isSubmitting || quantity > sharesHeld || !form.formState.isValid}> |
| 105 | + {form.formState.isSubmitting && <Loader2 className="mr-2 h-4 w-4 animate-spin" />} |
| 106 | + Vendre pour ${proceeds > 0 ? proceeds.toFixed(2) : '0.00'} |
| 107 | + </Button> |
| 108 | + </DialogFooter> |
| 109 | + </form> |
| 110 | + </Form> |
| 111 | + </DialogContent> |
| 112 | + </Dialog> |
| 113 | + ) |
| 114 | +} |
0 commit comments