Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timer #54

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions ui/components/sections/cartSection/cartSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,39 @@ import style from "./index.module.css";
import { manropeBold, manropeSemiBold } from "@/app/fonts";
import { CartContent } from "@/components/organisms/cartContent";
import { useStoreContext } from "@/store";
import { useEffect, useState } from "react";
import { getTimeDifference } from "@/helpers/timeHelper";
import { deleteName } from "@/app/actions/actions";
import { DATA_STATUS } from "@/comman/types";

const CartSection = () => {
const {
state: { bag },
state: {
bag: { reservationTime, domains },
},
actions: { deleteFromBag },
} = useStoreContext();
const domainReservationTime = bag?.reservationTime;

const [time, setTime] = useState<string>("");

useEffect(() => {
if (reservationTime) {
setInterval(() => {
const result = getTimeDifference(Number(reservationTime));
if (!result) {
domains.forEach(({ id }) => deleteFromBag(id));
}
setTime(result);
}, 1000);
}
}, [reservationTime]);

return (
<div className={classNames(style.wrapper, "container")}>
<div className={classNames(manropeBold.className, style.header)}>
My Cart
<span className={manropeSemiBold.className}>
Domain reservation 30:00
Domain reservation {time}
</span>
</div>
<CartContent />
Expand Down
29 changes: 23 additions & 6 deletions ui/helpers/timeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import dayjsPluginUTC from "dayjs-plugin-utc";

dayjs.extend(dayjsPluginUTC);

export const getTimeFromMillisecondsDynamic = (
millis,
fullMeasureNames,
isAgo
) => {
export const getTimeFromMillisecondsDynamic = (millis, fullMeasureNames) => {
console.log(millis);
console.log(fullMeasureNames);

const SECONDSMS = 1000;
const MINUTESMS = SECONDSMS * 60;
const HOURSMS = MINUTESMS * 60;
Expand All @@ -19,7 +18,7 @@ export const getTimeFromMillisecondsDynamic = (
const hoursSymbol = fullMeasureNames ? "hour" : "h";
const minutesSymbol = fullMeasureNames ? "minute" : "m";
const secondsSymbol = fullMeasureNames ? "second" : "s";
const ago = isAgo ? " ago" : "";
const ago = "";

let years, days, hours, minutes, seconds;

Expand Down Expand Up @@ -107,3 +106,21 @@ export const dayMonthYearTimeFormat = (timestamp) => {
const date = dayjs(new Date(timestamp)).format("DD.MM.YYYY UTC h:mm");
return date;
};

export const getTimeDifference = (timelocalstorage: number): string => {
const currentTime = Date.now();
const timeDifference = timelocalstorage + 30 * 60000 - currentTime;

if (timeDifference > 0) {
const minutes = Math.floor(timeDifference / 60000);
const seconds = Math.floor((timeDifference % 60000) / 1000);

if (minutes > 0) {
return `${minutes}m ${seconds}s`;
} else {
return `${seconds}s`;
}
} else {
return;
}
};
7 changes: 3 additions & 4 deletions ui/store/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";
import { bag, initReservationTime } from "@/comman/constants";
import { Bag } from "@/components/atoms/bag";
import { bag } from "@/comman/constants";
import { Modals } from "@/components/molecules/modals/modals.types";
import React, { useContext, useReducer } from "react";

Expand All @@ -11,7 +10,7 @@ type Domain = {
id: string;
};
type Bag = {
reservationTime: Date;
reservationTime: Date | number;
domains: Domain[];
};

Expand Down Expand Up @@ -108,7 +107,7 @@ export const reducer = (state: IState, action: StoreActions): IState => {
};
case "ADD_TO_BAG":
const dataBag = {
reservationTime: new Date(),
reservationTime: Date.now(),
domains: [...state.bag.domains, action.payload],
};
localStorage.setItem(bag, JSON.stringify(dataBag));
Expand Down
Loading