Skip to content

Commit

Permalink
fix: create an effect to update stackbox dates each minute that passed
Browse files Browse the repository at this point in the history
  • Loading branch information
ElRodrigote committed Jul 8, 2024
1 parent 2954286 commit 5c212d5
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions packages/app/components/stackbox/Stackbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
Token,
frequencySeconds,
} from "@/models";
import { getDatePlusMinsOffset } from "@/utils";

interface SelectTokenButtonProps {
label: string;
Expand All @@ -56,10 +57,9 @@ interface SelectTokenButtonProps {
className?: string;
onAnimationEnd: AnimationEventHandler<HTMLElement>;
}

const START_TIME_MINUTES_OFFSET = 10;
const getDateNowPlus10Mins = () =>
new Date().setMinutes(new Date().getMinutes() + START_TIME_MINUTES_OFFSET);
const ONE_MINUTE = 1;
const TEN_MINUTES = 10;
const ONE_MINUTE_IN_MS = 60000;

const frequencyOptions = [
{ option: FREQUENCY_OPTIONS.hour, name: "Hour" },
Expand Down Expand Up @@ -190,6 +190,26 @@ export const Stackbox = () => {
tokenListWithBalances,
]);

/**
* startDate and endDate handler when the user
* is idle for 1 minute or more
*/
useEffect(() => {
function oneMinuteInterval() {
setStartDateTime(getDatePlusMinsOffset(startDateTime, TEN_MINUTES));
setEndDateTime(getDatePlusMinsOffset(endDateTime, TEN_MINUTES));
}

oneMinuteInterval();

const id = setInterval(oneMinuteInterval, ONE_MINUTE_IN_MS);

return () => {
clearInterval(id);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const openTokenPicker = (isFromToken = true) => {
setIsPickingFromToken(isFromToken);
openModal(ModalId.TOKEN_PICKER);
Expand Down Expand Up @@ -484,14 +504,15 @@ export const Stackbox = () => {
>
<BodyText size={2}>Until</BodyText>
<DatePicker
dateTime={endDateTime}
dateTime={getDatePlusMinsOffset(endDateTime, ONE_MINUTE)}
setDateTime={(date: Date) => {
deselectStrategy();
setEndDateTime(date);
}}
timeCaption="End time"
className="w-full"
fromDate={startDateTime}
isEndDate
/>
</div>
</div>
Expand Down Expand Up @@ -585,7 +606,9 @@ export const Stackbox = () => {
amount={tokenAmount}
frequency={frequency}
startTime={
isPastStartDate ? new Date(getDateNowPlus10Mins()) : startDateTime
isPastStartDate
? new Date(getDatePlusMinsOffset(new Date(), TEN_MINUTES))
: startDateTime
}
endTime={endDateTime}
isOpen={isModalOpen(ModalId.CONFIRM_STACK)}
Expand Down

0 comments on commit 5c212d5

Please sign in to comment.