Skip to content

Commit

Permalink
Merge pull request #7 from MyTonSwap/develop
Browse files Browse the repository at this point in the history
Added loading when user select tokens while it fetching the token and updated to v2.0.12
  • Loading branch information
Ho3einWave authored Nov 19, 2024
2 parents f77ac59 + c15e458 commit db858a0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/components/Swap/Swap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
color: var(--primary-color);
}

.animate-loading {
animation: spin 1s linear infinite;
}

@keyframes spin {
0% {
transform: rotate(0deg);
Expand Down
6 changes: 6 additions & 0 deletions lib/components/SwapButton/SwapButton.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.mytonswap-app {
.loading-button {
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
}
.modal-container {
width: 100%;
height: 100%;
Expand Down
8 changes: 8 additions & 0 deletions lib/components/SwapButton/SwapButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useOptionsStore } from "../../store/options.store";
import { useTranslation } from "react-i18next";
import { cn } from "../../utils/cn";
import { useRef } from "react";
import { CgSpinnerTwo } from "react-icons/cg";

const SwapButton = () => {
const { t } = useTranslation();
Expand All @@ -36,9 +37,16 @@ const SwapButton = () => {
swapModal,
receive_token,
setModalState,
isSelectingToken,
} = useSwapStore();

const getSwapText = () => {
if (isSelectingToken)
return (
<span className="loading-button">
<CgSpinnerTwo className="animate-loading" /> Loading ...
</span>
);
if (!tonConnectInstance?.wallet) return t("button_text.connect_wallet");
if (!receive_token || !pay_token)
return t("button_text.choose_a_token");
Expand Down
3 changes: 3 additions & 0 deletions lib/components/SwapCard/CardButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MdKeyboardArrowDown } from "react-icons/md";
import "./CardButton.scss";
import { useOptionsStore } from "../../store/options.store";
import { useTranslation } from "react-i18next";
import { useSwapStore } from "../../store/swap.store";
type CardButtonProps = {
isLoading: boolean;
onClick: () => void;
Expand All @@ -18,7 +19,9 @@ const CardButton: FC<CardButtonProps & PropsWithChildren> = ({
}) => {
const { t } = useTranslation();
const { options } = useOptionsStore();
const { isSelectingToken } = useSwapStore();
const isDisabled = (() => {
if (isSelectingToken) return true;
if (type === "pay" && options.lock_pay_token) return true;
if (type === "receive" && options.lock_receive_token) return true;
return false;
Expand Down
6 changes: 6 additions & 0 deletions lib/store/swap.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type SwapStates = {
pay_amount: bigint;
assets: Asset[] | null;
isLoading: boolean;
isSelectingToken: boolean;
isFindingBestRoute: boolean;
bestRoute: BestRoute | null;
onePayRoute: BestRoute | null;
Expand Down Expand Up @@ -76,6 +77,7 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
receive_token: null,
receive_rate: null,
isLoading: true,
isSelectingToken: false,
isFindingBestRoute: false,
bestRoute: null,
onePayRoute: null,
Expand Down Expand Up @@ -182,6 +184,7 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
set(() => ({ isLoading }));
},
setPayToken: async (token) => {
set(() => ({ isSelectingToken: true }));
useEventsStore.getState().onTokenSelect({ type: "pay", asset: token });
const { client } = get();
if (!token) return;
Expand All @@ -205,6 +208,7 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
receive_rate: null,
receive_token: null,
bestRoute: null,
isSelectingToken: false,
}));
},
async setPayAmount(amount) {
Expand Down Expand Up @@ -234,6 +238,7 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
set(() => ({ bestRoute, isFindingBestRoute: false }));
},
setReceiveToken: async (token) => {
set(() => ({ isSelectingToken: true }));
useEventsStore
.getState()
.onTokenSelect({ type: "receive", asset: token });
Expand All @@ -256,6 +261,7 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
receive_token: token,
receive_rate: tokenRate ?? null,
isFindingBestRoute: true,
isSelectingToken: false,
}));
const onePayRouteResult = await catchError(() =>
client.router.findBestRoute(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mytonswap/widget",
"description": "MyTonSwap Widget - Easy to use swap widget for React on TON Blockchain",
"version": "2.0.11",
"version": "2.0.12",
"type": "module",
"author": {
"name": "MyTonSwap",
Expand Down

0 comments on commit db858a0

Please sign in to comment.