Skip to content

Commit

Permalink
chore: improve modals ui
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Mar 7, 2024
1 parent c08361c commit 934bd0b
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 48 deletions.
11 changes: 9 additions & 2 deletions src/features/core/components/common-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { PropsWithChildren } from "react";
import { useEffect } from "react";
import Modal from "react-modal";

Expand Down Expand Up @@ -37,7 +38,7 @@ const CommonModal = ({ children, ...props }: Props) => {
bottom: "auto",
boxShadow: "0px 0px 50px 0px #FFFFFF40",
left: "50%",
padding: "45px",
padding: "48px",
right: "auto",
top: "50%",
transform: "translate(-50%, -50%)",
Expand All @@ -49,7 +50,7 @@ const CommonModal = ({ children, ...props }: Props) => {
>
<div className="relative w-full">
<button
className="absolute right-[-16px] top-[-16px] w-[36px] cursor-pointer"
className="absolute right-[-30px] top-[-30px] w-[36px] cursor-pointer"
dangerouslySetInnerHTML={{ __html: cross }}
onClick={props.onRequestClose}
/>
Expand All @@ -59,4 +60,10 @@ const CommonModal = ({ children, ...props }: Props) => {
);
};

export const ModalDescription = ({ children }: PropsWithChildren) => (
<div className="mb-[16px] text-[16px] leading-[24px] text-typo-100 opacity-50">
{children}
</div>
);

export default CommonModal;
17 changes: 7 additions & 10 deletions src/features/staking/components/delegation-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { StakingContextType, StakingState } from "../context/state";
import { useValidatorLogo } from "../hooks";
import { coinIsPositive } from "../lib/core/coins";
import { menu, pointer } from "../lib/core/icons";
import { cancelUnbonding } from "../lib/core/tx";
import {
formatCoin,
formatCommission,
Expand Down Expand Up @@ -204,7 +203,7 @@ const UnbondingRow = ({
unbonding,
validator,
}: UnbondingRowProps) => {
const { account, client, staking } = stakingRef;
const { staking } = stakingRef;

const logo = useValidatorLogo(validator?.description.identity);
const validatorAddress = unbonding.validator;
Expand Down Expand Up @@ -244,14 +243,12 @@ const UnbondingRow = ({
<ButtonPill
disabled={disabled}
onClick={() => {
if (!client) return;

const addresses = {
delegator: account.bech32Address || "",
validator: unbonding.validator,
};

cancelUnbonding(addresses, unbonding, client);
staking.dispatch(
setModalOpened({
content: { unbonding },
type: "cancel-staking",
}),
);
}}
variant="danger"
>
Expand Down
121 changes: 121 additions & 0 deletions src/features/staking/components/modals/cancel-unstaking.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { memo, useState } from "react";
import { toast } from "react-toastify";

import { Button, HeroText } from "@/features/core/components/base";
import CommonModal, {
ModalDescription,
} from "@/features/core/components/common-modal";

import { cancelUnstakingAction } from "../../context/actions";
import { useStaking } from "../../context/hooks";
import { setModalOpened } from "../../context/reducer";
import type { StakeAddresses } from "../../lib/core/tx";

type Step = "completed" | "confirm";

const initialStep: Step = "confirm";

const CancelUnstakingModal = () => {
const stakingRef = useStaking();
const [currentStep, setStep] = useState<Step>(initialStep);
const [isLoading, setIsLoading] = useState(false);

const { account, client, staking } = stakingRef;

const { modal } = staking.state;

const isOpen = modal?.type === "cancel-staking";

if (!isOpen) return null;

const { unbonding } = modal?.content || {};

if (!unbonding) return null;

const content = (() => {
if (currentStep === "confirm") {
return (
<div className="w-full text-center">
<div className="mb-[35px] text-center uppercase">
<HeroText>Are you sure?</HeroText>
</div>
<ModalDescription>
We are currently completing the unstaking process.
<br />
Are you sure you want to cancel the process?
</ModalDescription>
<Button
className="mt-[25px]"
isLoading={isLoading}
onClick={() => {
if (!client) return;

setIsLoading(true);

const addresses: StakeAddresses = {
delegator: account.bech32Address,
validator: unbonding.validator,
};

cancelUnstakingAction(addresses, unbonding, client, staking)
.then((fetchFn) => {
fetchFn();
setStep("completed");
})
.catch(() => {
toast(
"There was an unexpected error canceling your unstaking. Please try again later.",
{
type: "error",
},
);
})
.finally(() => {
setIsLoading(false);
});
}}
>
CONFIRM
</Button>
</div>
);
}

return (
<>
<div className="text-center">
<div className="mb-[35px] text-center uppercase">
<HeroText>Success!</HeroText>
</div>
<ModalDescription>
You have successfully cancel the unstaking process, and you are now
contributing to the security of the XION network again.
</ModalDescription>
</div>
<Button
className="mt-[25px]"
onClick={() => {
stakingRef.staking.dispatch(setModalOpened(null));
}}
>
CLOSE
</Button>
</>
);
})();

return (
<CommonModal
isOpen={isOpen}
onRequestClose={() => {
if (isLoading) return;

stakingRef.staking.dispatch(setModalOpened(null));
}}
>
<div className="min-w-[390px]">{content}</div>
</CommonModal>
);
};

export default memo(CancelUnstakingModal);
21 changes: 13 additions & 8 deletions src/features/staking/components/modals/rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { memo, useEffect, useRef, useState } from "react";
import { toast } from "react-toastify";

import { Button, HeroText } from "@/features/core/components/base";
import CommonModal from "@/features/core/components/common-modal";
import CommonModal, {
ModalDescription,
} from "@/features/core/components/common-modal";

import { fetchUserDataAction } from "../../context/actions";
import { useStaking } from "../../context/hooks";
Expand All @@ -20,6 +22,8 @@ const claimRewardsLoop = async (
const { client, staking } = stakingRef;
const { modal } = staking.state;

if (modal?.type !== "rewards") return;

const { delegations } = modal?.content || {};

if (!client || !delegations?.length) return;
Expand Down Expand Up @@ -76,28 +80,29 @@ const RewardsModal = () => {
if (currentStep === "loading") {
return (
<div className="w-full text-center">
<div className="mb-[16px] uppercase">
<div className="mb-[35px] text-center uppercase">
<HeroText>CLAIMING</HeroText>
</div>
<div className="mb-[16px] text-typo-100">
<ModalDescription>
Wait until your rewards are withdrawn.
</div>
<Button isLoading />
</ModalDescription>
<Button className="mt-[25px]" isLoading />
</div>
);
}

return (
<>
<div className="text-center">
<div className="mb-[16px] uppercase">
<div className="mb-[35px] text-center uppercase">
<HeroText>Success!</HeroText>
</div>
<div className="mb-[16px] text-typo-100">
<ModalDescription>
You have claimed your staking rewards successfully.
</div>
</ModalDescription>
</div>
<Button
className="mt-[25px]"
onClick={() => {
stakingRef.staking.dispatch(setModalOpened(null));
}}
Expand Down
18 changes: 10 additions & 8 deletions src/features/staking/components/modals/staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
InputBox,
OpenInput,
} from "@/features/core/components/base";
import CommonModal from "@/features/core/components/common-modal";
import CommonModal, {
ModalDescription,
} from "@/features/core/components/common-modal";

import { stakeValidatorAction } from "../../context/actions";
import { useStaking } from "../../context/hooks";
Expand Down Expand Up @@ -104,14 +106,14 @@ const StakingModal = () => {
return (
<>
<div className="text-center">
<div className="mb-[16px]">
<div className="mb-[35px] text-center uppercase">
<HeroText>SUCCESS!</HeroText>
</div>
<div className="text-typo-100">
<ModalDescription>
You have successfully staked on{" "}
{validator.description.moniker}. Thank you for contributing
in securing the XION network.
</div>
</ModalDescription>
</div>
{getStakingSummary()}
<Button
Expand All @@ -130,13 +132,13 @@ const StakingModal = () => {
return (
<>
<div className="text-center">
<div className="mb-[16px]">
<div className="mb-[35px] text-center uppercase">
<HeroText>REVIEW</HeroText>
</div>
<div className="text-typo-100">
<ModalDescription>
Get ready to stake your XION token with{" "}
{validator.description.moniker}. Press 'Confirm' to proceed.
</div>
</ModalDescription>
</div>
{getStakingSummary()}
<Button
Expand Down Expand Up @@ -208,7 +210,7 @@ const StakingModal = () => {

return (
<>
<div className="text-center uppercase">
<div className="mb-[35px] text-center uppercase">
<HeroText>Delegate To {validator.description.moniker}</HeroText>
</div>
{availableTokens &&
Expand Down
18 changes: 10 additions & 8 deletions src/features/staking/components/modals/unstaking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
InputBox,
OpenInput,
} from "@/features/core/components/base";
import CommonModal from "@/features/core/components/common-modal";
import CommonModal, {
ModalDescription,
} from "@/features/core/components/common-modal";

import { unstakeValidatorAction } from "../../context/actions";
import { useStaking } from "../../context/hooks";
Expand Down Expand Up @@ -132,14 +134,14 @@ const UnstakingModal = () => {
return (
<>
<div className="text-center">
<div className="mb-[16px]">
<div className="mb-[35px] text-center uppercase">
<HeroText>SUCCESS!</HeroText>
</div>
<div className="text-typo-100">
<ModalDescription>
You have successfully unstaked from{" "}
{validator.description.moniker}. It takes {unbondingDays}{" "}
days to complete the unstaking process
</div>
</ModalDescription>
</div>
{getUnstakingSummary()}
<Button
Expand All @@ -158,14 +160,14 @@ const UnstakingModal = () => {
return (
<>
<div className="text-center">
<div className="mb-[16px]">
<div className="mb-[35px] text-center uppercase">
<HeroText>REVIEW</HeroText>
</div>
<div className="text-typo-100">
<ModalDescription>
Unstaking your XION Token means you'll stop earning rewards.
Remember, it takes {unbondingDays} days to complete the
unstaking process.
</div>
</ModalDescription>
</div>
{getUnstakingSummary()}
<Button
Expand Down Expand Up @@ -210,7 +212,7 @@ const UnstakingModal = () => {

return (
<>
<div className="text-center uppercase">
<div className="mb-[35px] text-center uppercase">
<HeroText>
Unstake From {validator.description.moniker}
</HeroText>
Expand Down
2 changes: 2 additions & 0 deletions src/features/staking/components/staking-modals.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CancelUnstakingModal from "./modals/cancel-unstaking";
import RewardsModal from "./modals/rewards";
import StakingModal from "./modals/staking";
import UnstakingModal from "./modals/unstaking";
Expand All @@ -7,6 +8,7 @@ const StakingModals = () => (
<StakingModal />
<UnstakingModal />
<RewardsModal />
<CancelUnstakingModal />
</>
);

Expand Down
Loading

0 comments on commit 934bd0b

Please sign in to comment.