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

fixed expiry function and the way it renders on UI in human-readable … #34

Merged
merged 1 commit into from
Dec 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
13 changes: 12 additions & 1 deletion packages/site/src/pages/SignedOrderScreen/SignedOrderScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IoMdOpen } from 'react-icons/io';
import { SwapButton } from '../../components';
// import { compressFullOrderURL } from '../../utils/compressFullOrderERC20';
import { useSwapStore } from '../../stores';
import { convertExpiryToHumanReadableFormat } from '../../utils/convertExpiryToHumanReadableFormat';
import {
ActionButtonsBox,
FromToTokenBox,
Expand Down Expand Up @@ -34,6 +35,8 @@ export const SignedOrderScreen = () => {
// }
// };

const expirationDate = convertExpiryToHumanReadableFormat(expiry);

return (
<SignedOrderScreenContainer>
<StyledH3>Order</StyledH3>
Expand Down Expand Up @@ -93,7 +96,15 @@ export const SignedOrderScreen = () => {
<HorizontalBox>
<VerbSpan>For: {takerAddress ?? 'Anyone'}</VerbSpan>
</HorizontalBox>
<HorizontalBox>Expires in {expiry}</HorizontalBox>
<HorizontalBox>
{expirationDate ? (
<>
<VerbSpan>Expires in </VerbSpan>&nbsp; {expirationDate}
</>
) : (
<VerbSpan>Expired</VerbSpan>
)}
</HorizontalBox>
</SpecifiedTakerAndExpiryBox>
<HorizontalBox>
<VerbSpan>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const SpecifiedTakerAndExpiryBox = styled.div`
justify-content: space-between;
width: 100%;
background: #13203c;
border: #34425c solid 1.5px;
border: #34425c solid 1px;
border-radius: 12px;
padding: 1rem;
`;
Expand Down
9 changes: 6 additions & 3 deletions packages/site/src/pages/SwapWidget/SwapWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const SwapWidget = () => {
setTakerAddress,
setDurationLength,
setDurationUnits,
setExpiry,
} = useSwapStore();

// Handle token selection from modal
Expand Down Expand Up @@ -109,6 +110,7 @@ export const SwapWidget = () => {
onTextChange={(value) => {
if (!Number.isNaN(value)) {
setDurationLength(value);
setExpiry(value, durationUnits);
}
}}
/>
Expand All @@ -119,9 +121,10 @@ export const SwapWidget = () => {
placeholder="HOURS"
value={durationUnits}
items={hoursItems}
onSelectChange={(value: string) =>
setDurationUnits(value as DurationUnits)
}
onSelectChange={(value: string) => {
setDurationUnits(value as DurationUnits);
setExpiry(durationLength, value as DurationUnits);
}}
/>
</SelectWrapper>
</HeaderInputContainer>
Expand Down
26 changes: 26 additions & 0 deletions packages/site/src/utils/convertExpiryToHumanReadableFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const convertExpiryToHumanReadableFormat = (
expiry: number,
): string | undefined => {
const currentTimestamp = Math.floor(Date.now() / 1000);
const remainingTimeInSeconds = expiry - currentTimestamp;

if (remainingTimeInSeconds <= 0) {
return undefined;
}

const minutes = Math.floor(remainingTimeInSeconds / 60) % 60;
const hours = Math.floor(remainingTimeInSeconds / (60 * 60)) % 24;
const days = Math.floor(remainingTimeInSeconds / (60 * 60 * 24));

if (remainingTimeInSeconds < 60 * 60) {
return `${minutes} Min`;
}
if (remainingTimeInSeconds < 60 * 60 * 24) {
return `${hours} Hour${hours === 1 ? '' : 's'}${
minutes > 0 ? ` ${minutes} Min${minutes === 1 ? '' : 's'}` : ''
}`;
}
return `${days} Day${days === 1 ? '' : 's'}${
hours > 0 ? ` ${hours} Hour${hours === 1 ? '' : 's'}` : ''
}`;
};
Loading