From f3c55d042f9628a0661bcbc9f330bfc8dd7bd7ad Mon Sep 17 00:00:00 2001 From: vidvidvid Date: Mon, 28 Oct 2024 20:50:35 +0100 Subject: [PATCH] extend veion dialog --- .../ui/app/_components/veion/ExtendVeion.tsx | 212 +++++++++++++++--- 1 file changed, 177 insertions(+), 35 deletions(-) diff --git a/packages/ui/app/_components/veion/ExtendVeion.tsx b/packages/ui/app/_components/veion/ExtendVeion.tsx index 50a5d454b..4053a256d 100644 --- a/packages/ui/app/_components/veion/ExtendVeion.tsx +++ b/packages/ui/app/_components/veion/ExtendVeion.tsx @@ -1,40 +1,100 @@ 'use client'; -import { useState, useMemo } from 'react'; - +import { useState, useEffect, useMemo } from 'react'; +import { format, addDays } from 'date-fns'; +import { Calendar as CalendarIcon } from 'lucide-react'; import { Button } from '@ui/components/ui/button'; +import { Calendar } from '@ui/components/ui/calendar'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@ui/components/ui/dialog'; +import { + Popover, + PopoverContent, + PopoverTrigger +} from '@ui/components/ui/popover'; import { Separator } from '@ui/components/ui/separator'; - +import { Slider } from '@ui/components/ui/slider'; import AutoLock from './AutoLock'; -import DateSlider from './LockDuration'; -import SliderComponent from '../popup/Slider'; +import CustomTooltip from '../CustomTooltip'; interface ExtendVeionProps { isOpen: boolean; onOpenChange: (open: boolean) => void; + currentVotingPower?: string; + currentLockDate?: string; + maxToken?: number; } -export default function ExtendVeion({ +const ExtendVeion = ({ isOpen, - onOpenChange -}: ExtendVeionProps) { - const [veIonAmount, setVeIonAmount] = useState(''); - const [utilization, setUtilization] = useState(0); - const [lockDuration, setLockDuration] = useState(''); + onOpenChange, + currentVotingPower = '20.00', + currentLockDate = '28 Aug 2023', + maxToken = 100 +}: ExtendVeionProps) => { const [autoLock, setAutoLock] = useState(false); - const maxtoken = 100; + const [lockDuration, setLockDuration] = useState('180'); + const [lockDate, setLockDate] = useState(() => + addDays(new Date(), 180) + ); + const [selectedDuration, setSelectedDuration] = useState(180); + const [isCalendarOpen, setIsCalendarOpen] = useState(false); + const [veIonAmount, setVeIonAmount] = useState('0'); + const [utilization, setUtilization] = useState(0); + + const dateRange = useMemo(() => { + const today = new Date(); + return { + minDate: addDays(today, 180), + maxDate: addDays(today, 730) + }; + }, []); - // eslint-disable-next-line no-console - console.log('lockDuration', lockDuration); - useMemo(() => { - setUtilization(Number(((+veIonAmount / maxtoken) * 100).toFixed(0)) ?? 0); - }, [veIonAmount]); + const durationLabels = { + 180: '180d', + 365: '1y', + 547: '1.5y', + 730: '2y' + }; + + useEffect(() => { + const newDate = addDays(new Date(), selectedDuration); + setLockDate(newDate); + setLockDuration(selectedDuration.toString()); + }, [selectedDuration]); + + useEffect(() => { + setUtilization(Number(((+veIonAmount / maxToken) * 100).toFixed(0)) ?? 0); + }, [veIonAmount, maxToken]); + + const handleDurationChange = (val: number[]) => { + const duration = val[0]; + setSelectedDuration(duration); + }; + + const handleDateSelect = (date: Date | undefined) => { + if (date) { + setLockDate(date); + const durationInDays = Math.round( + (date.getTime() - new Date().getTime()) / (1000 * 60 * 60 * 24) + ); + const clampedDuration = Math.max(180, Math.min(730, durationInDays)); + setSelectedDuration(clampedDuration); + setLockDuration(clampedDuration.toString()); + setIsCalendarOpen(false); + } + }; + + const handleUtilizationChange = (val: number[]) => { + const utilizationValue = val[0]; + setUtilization(utilizationValue); + const veionval = (utilizationValue / 100) * maxToken; + setVeIonAmount(veionval.toString()); + }; return (
- Voting Power: 20.00 veION - Locked Until: 28 Aug 2023 + + Voting Power: {currentVotingPower} veION + + Locked Until: {currentLockDate}
- { - if (!val) return; - const veionval = (val / 100) * maxtoken; - setVeIonAmount(veionval.toString()); - }} - /> - + {/* Amount Slider */} +
+
+

AMOUNT

+ +
+ +
+ {veIonAmount} / {maxToken} ION +
+
+ + {/* Lock Duration */} +
+
+

LOCK UNTIL

+ +
+
+
+ {format(lockDate, 'dd. MM. yyyy')} +
+ + + + + + + + +
+ +
+ {Object.entries(durationLabels).map(([days, label]) => ( + = Number(days) ? 'text-accent' : '' + } + > + {label} + + ))} +
+
+
-
- Voting Power - 0.00 → 120 +
+
+ VOTING POWER + +
+ + {currentVotingPower} → {veIonAmount} veION +
-
- Locked Until - 09 Sep 2023 → 09 Sep 2024 +
+ LOCKED Until + + {currentLockDate} → {format(lockDate, 'dd MMM yyyy')} +
@@ -84,4 +224,6 @@ export default function ExtendVeion({
); -} +}; + +export default ExtendVeion;