From 6204175e532163f15439baf9adbe58986d587ba9 Mon Sep 17 00:00:00 2001 From: wwills2 Date: Wed, 13 Mar 2024 16:29:36 -0400 Subject: [PATCH] feat: add mirror working with corrected logic --- src/handles/datalayer.handles.ts | 43 ++- .../{getUserIp => datalayerStorage}/index.ts | 0 src/renderer/api/index.ts | 2 +- .../AddMirrorErrorModal.tsx | 12 +- .../modals/AddMirrorModal/AddMirrorModal.tsx | 325 ++++++++++-------- .../WalletBalanceInsufficientModal.tsx | 2 +- src/renderer/translations/tokens/en-US.json | 4 +- 7 files changed, 209 insertions(+), 179 deletions(-) rename src/renderer/api/{getUserIp => datalayerStorage}/index.ts (100%) diff --git a/src/handles/datalayer.handles.ts b/src/handles/datalayer.handles.ts index 55da74c..81c97bf 100644 --- a/src/handles/datalayer.handles.ts +++ b/src/handles/datalayer.handles.ts @@ -73,20 +73,20 @@ export async function mountDatalayerRpcHandles() { }; } + const totalTransactionWithUsageFee = + addMirrorParams.amount + parseInt(addMirrorParams.fee) + xchToMojos(fixedFeeXch); + if (getWalletBalanceResponse?.wallet_balance?.spendable_balance > totalTransactionWithUsageFee){ + sendFixedFee(network, spendableCoins.confirmed_records.length); + } + + await new Promise(ignore => setTimeout(ignore, 1000)); + const addMirrorPromise = datalayer.addMirror(addMirrorParams, { ...options, waitForWalletAvailability: false, includeFee: false }); - - const totalTransactionWithUsageFee = parseInt(addMirrorParams.fee) + xchToMojos(fixedFeeXch); - - const addMirrorResult = await addMirrorPromise; - if (addMirrorResult?.success && - (getWalletBalanceResponse?.wallet_balance?.spendable_balance > totalTransactionWithUsageFee)){ - sendFixedFee(network, spendableCoins.confirmed_records.length); - } - + console.log('#######', addMirrorPromise); return addMirrorPromise; }); @@ -126,17 +126,17 @@ export async function mountDatalayerRpcHandles() { }; } - const createStorePromise = datalayer.createDataStore(createDataStoreParams, { - ...options, - waitForWalletAvailability: false - }); - - const createStoreResult = await createStorePromise; - if (createStoreResult?.success){ + const totalTransactionWithUsageFee = parseInt(createDataStoreParams.fee) + xchToMojos(fixedFeeXch); + if (getWalletBalanceResponse?.wallet_balance?.spendable_balance > totalTransactionWithUsageFee){ sendFixedFee(network, spendableCoins.confirmed_records.length); } - - return createStorePromise; + setTimeout(() => { + return datalayer.createDataStore(createDataStoreParams, { + ...options, + waitForWalletAvailability: false, + includeFee: false + }); + }, 1000); }, ); @@ -169,7 +169,12 @@ export async function mountDatalayerRpcHandles() { }; } - return datalayer.deleteMirror(deleteMirrorParams, options); + console.log('********', deleteMirrorParams) + return datalayer.deleteMirror(deleteMirrorParams, { + ...options, + waitForWalletAvailability: false, + includeFee: false + }); }); ipcMain.handle('datalayerGetKeys', (_, getKeysParams: GetKeysParams, options: Options) => { diff --git a/src/renderer/api/getUserIp/index.ts b/src/renderer/api/datalayerStorage/index.ts similarity index 100% rename from src/renderer/api/getUserIp/index.ts rename to src/renderer/api/datalayerStorage/index.ts diff --git a/src/renderer/api/index.ts b/src/renderer/api/index.ts index b40e713..653795b 100644 --- a/src/renderer/api/index.ts +++ b/src/renderer/api/index.ts @@ -1,2 +1,2 @@ export * from './ipc'; -export * from './getUserIp'; \ No newline at end of file +export * from './datalayerStorage'; \ No newline at end of file diff --git a/src/renderer/components/blocks/modals/AddMirrorErrorModal/AddMirrorErrorModal.tsx b/src/renderer/components/blocks/modals/AddMirrorErrorModal/AddMirrorErrorModal.tsx index 903f976..d182aab 100644 --- a/src/renderer/components/blocks/modals/AddMirrorErrorModal/AddMirrorErrorModal.tsx +++ b/src/renderer/components/blocks/modals/AddMirrorErrorModal/AddMirrorErrorModal.tsx @@ -2,14 +2,14 @@ import React from "react"; import { Modal } from "flowbite-react"; import {FormattedMessage} from "react-intl"; -interface AddSubscriptionErrorModalProps { +interface AddMirrorErrorModalProps { showModal: boolean; setShowModal: (showModal: boolean) => void; errorMessage?: string; } -const AddMirrorErrorModal: React.FC = ( - { showModal, setShowModal, errorMessage}: AddSubscriptionErrorModalProps) => { +const AddMirrorErrorModal: React.FC = ( + { showModal, setShowModal, errorMessage}: AddMirrorErrorModalProps) => { return ( setShowModal(false)}> @@ -20,16 +20,16 @@ const AddMirrorErrorModal: React.FC = (

{ - errorMessage || + errorMessage || }

-
+

+

-

diff --git a/src/renderer/components/blocks/modals/AddMirrorModal/AddMirrorModal.tsx b/src/renderer/components/blocks/modals/AddMirrorModal/AddMirrorModal.tsx index 00abbdd..aea00b0 100644 --- a/src/renderer/components/blocks/modals/AddMirrorModal/AddMirrorModal.tsx +++ b/src/renderer/components/blocks/modals/AddMirrorModal/AddMirrorModal.tsx @@ -1,4 +1,4 @@ -import React, {useEffect, useState} from "react"; +import React, {useCallback, useEffect, useState} from "react"; import {Button, Modal, Spinner, TextInput, Tooltip} from "flowbite-react"; import {FormattedMessage} from "react-intl"; import {useDispatch, useSelector} from "react-redux"; @@ -6,7 +6,8 @@ import {addStoreMirror} from "@/store/slices/app"; import {useAddMirrorMutation} from "@/api/ipc/datalayer"; import {useGetUserIpQuery} from "@/api"; import {useGetWalletBalanceImmediateMutation} from "@/api/ipc/wallet"; -import {WalletBalanceInsufficientErrorModal} from "@/components"; +import {AddMirrorErrorModal, WalletBalanceInsufficientErrorModal} from "@/components"; +import {AddMirrorParams} from "chia-datalayer"; interface ConfirmCreateStoreModalProps { storeId: string; @@ -15,23 +16,21 @@ interface ConfirmCreateStoreModalProps { const AddMirrorModal: React.FC = ({storeId, onClose}: ConfirmCreateStoreModalProps) => { - const [triggerAddMirror, {data: addMirrorData, isLoading: addMirrorMutationLoading}] = useAddMirrorMutation(); + const [triggerAddMirror, {isLoading: addMirrorMutationLoading}] = useAddMirrorMutation(); const {data: getIpData, isLoading: getUserIpLoading} = useGetUserIpQuery({}); - const [triggerGetWalletBalance, { - data: getWalletBalanceData, - isLoading: getWalletBalanceLoading - }] = useGetWalletBalanceImmediateMutation(); + const [triggerGetWalletBalance, {isLoading: getWalletBalanceLoading}] = useGetWalletBalanceImmediateMutation(); const dispatch = useDispatch(); const deployOptions = useSelector((state: any) => state.userOptions.deployOptions); + const enterUrlPlaceholder: string = 'enter mirror url ...'; const [mirrorURL, setMirrorURL] = useState(''); const [mirrorCoinValue, setMirrorCoinValue] = useState(''); const [showInvalidUrlError, setShowInvalidUrlError] = useState(false); const [urlChanged, setUrlChanged] = useState(true); - const [placeholderUrl, setPlaceHolderUrl] = useState('https://duckduckgo.com'); + const [placeholderUrl, setPlaceHolderUrl] = useState(enterUrlPlaceholder); const [showInsufficientBalanceModal, setShowInsufficentBalanceModal] = useState(false); - const [addMirrorClicked, setAddMirrorClicked] = useState(false); + const [showGenericAddMirrorErrorModal, setShowGenericAddMirrorErrorModal] = useState(false); useEffect(() => { if (getIpData?.success && !getUserIpLoading){ @@ -39,67 +38,6 @@ const AddMirrorModal: React.FC = ({storeId, onClos } }, [getUserIpLoading, getIpData]); - useEffect(() => { - if (addMirrorData?.success) { - if (mirrorURL){ - dispatch(addStoreMirror({storeId, url: mirrorURL})); - } else { - dispatch(addStoreMirror({storeId, url: placeholderUrl})); - } - - onClose(); - } - }, [dispatch, addMirrorData?.success, storeId, mirrorURL, onClose, placeholderUrl]); - - useEffect(() => { - if (getWalletBalanceData?.success && !getWalletBalanceLoading && addMirrorClicked){ - - const walletBalance = getWalletBalanceData?.wallet_balance?.spendable_balance; - const requiredWalletBalance: number = mirrorCoinValue ? - parseInt(deployOptions.defaultFee) + parseInt(mirrorCoinValue) : - parseInt(deployOptions.defaultFee) + parseInt(deployOptions.defaultMirrorCoinAmount); - - if (walletBalance < requiredWalletBalance){ - setShowInsufficentBalanceModal(true); - setAddMirrorClicked(false); - return; - } - - if (!showInvalidUrlError && mirrorURL && mirrorCoinValue) { - triggerAddMirror({ - id: storeId, - urls: [mirrorURL], - amount: parseInt(mirrorCoinValue), - fee: deployOptions.defaultFee - }); - } else if (!showInvalidUrlError && mirrorURL && !mirrorCoinValue) { - triggerAddMirror({ - id: storeId, - urls: [mirrorURL], - amount: deployOptions.defaultMirrorCoinAmount, - fee: deployOptions.defaultFee - }); - } else if (!showInvalidUrlError && !mirrorURL && mirrorCoinValue) { - triggerAddMirror({ - id: storeId, - urls: [placeholderUrl], - amount: parseInt(mirrorCoinValue), - fee: deployOptions.defaultFee - }); - } else if (!showInvalidUrlError) { - triggerAddMirror({ - id: storeId, - urls: [placeholderUrl], - amount: deployOptions.defaultMirrorCoinAmount, - fee: deployOptions.defaultFee - }); - } - } - }, [addMirrorClicked, deployOptions.defaultFee, getWalletBalanceData?.success, - getWalletBalanceData?.wallet_balance, getWalletBalanceLoading, mirrorCoinValue, mirrorURL, - placeholderUrl, showInvalidUrlError, storeId, triggerAddMirror, deployOptions.defaultMirrorCoinAmount, - ]); - // Regex to check if the string is a valid URL const urlPattern = new RegExp('^(https?:\\/\\/)?' + // protocol '((([a-zA-Z\\d]([a-zA-Z\\d-]*[a-zA-Z\\d])*)\\.)+[a-zA-Z]{2,}|' + // domain name and extension @@ -118,111 +56,196 @@ const AddMirrorModal: React.FC = ({storeId, onClos setMirrorCoinValue(event.target.value); }; - const accept = () => { + const getAddMirrorParams = useCallback((): AddMirrorParams => { + if (mirrorURL && mirrorCoinValue) { + return { + id: storeId, + urls: [mirrorURL], + amount: parseInt(mirrorCoinValue), + fee: deployOptions.defaultFee + } + } else if (mirrorURL && !mirrorCoinValue) { + return { + id: storeId, + urls: [mirrorURL], + amount: deployOptions.defaultMirrorCoinAmount, + fee: deployOptions.defaultFee + } + } else if (!mirrorURL && mirrorCoinValue) { + return { + id: storeId, + urls: [placeholderUrl], + amount: parseInt(mirrorCoinValue), + fee: deployOptions.defaultFee + } + } else { + return { + id: storeId, + urls: [placeholderUrl], + amount: deployOptions.defaultMirrorCoinAmount, + fee: deployOptions.defaultFee + } + } + }, []); + + const accept = async () => { setUrlChanged(false); - setAddMirrorClicked(true); - triggerGetWalletBalance({}); + const getWalletBalanceResponse = await triggerGetWalletBalance({}); + + // @ts-ignore + if (getWalletBalanceResponse?.data?.success) { + + // @ts-ignore + const walletBalance = getWalletBalanceResponse?.data?.wallet_balance?.spendable_balance; + const requiredWalletBalance: number = mirrorCoinValue ? + parseInt(deployOptions.defaultFee) + parseInt(mirrorCoinValue) : + parseInt(deployOptions.defaultFee) + parseInt(deployOptions.defaultMirrorCoinAmount); + + console.log('required funds', requiredWalletBalance, 'available', walletBalance) + + if (walletBalance >= requiredWalletBalance) { + onClose(); + + if(!showInvalidUrlError){ + const addMirrorResult = await triggerAddMirror(getAddMirrorParams()); + console.log('add mirror data', addMirrorResult, 'add mirror loading', addMirrorMutationLoading); + // @ts-ignore + if (addMirrorResult?.data?.success) { + if (mirrorURL) { + dispatch(addStoreMirror({storeId, url: mirrorURL})); + } else { + dispatch(addStoreMirror({storeId, url: placeholderUrl})); + } + + } else { + setShowGenericAddMirrorErrorModal(true); + } + } + } else { + setShowInsufficentBalanceModal(true); + } + }else{ + setShowGenericAddMirrorErrorModal(true); + } } const cancel = () => { onClose() } + const disableAddMirror = (): boolean => { + return addMirrorMutationLoading || ((placeholderUrl === enterUrlPlaceholder) && !mirrorURL) + } + return ( - -
-

- {": " + storeId} -

-
- {showInvalidUrlError && !urlChanged &&

- -

} -
-
-

- -

-
-
- -
-
+ + {getUserIpLoading ? + +
+
-
-
-
- }> -

- -

-
+ + : + <> + +
+

+ {": " + storeId} +

+
+ {showInvalidUrlError && !urlChanged &&

+ +

} +
+
+

+ +

+
+
+ +
+
-
- +
+
+
+ }> +

+ +

+
+
+
+ +
+
+

+ + {': '} + . +

+

+ + {" " + 0.01 + " "} + + {', '} + + {' '} + + {'.'} +

-
-

- - {': '} - . -

-

- - {" " + 0.01 + " "} - - {', '} - - {' '} - - {'.'} -

-
- - - - - + + + + + + + } + ); } diff --git a/src/renderer/components/blocks/modals/WalletBalanceInsufficientModal/WalletBalanceInsufficientModal.tsx b/src/renderer/components/blocks/modals/WalletBalanceInsufficientModal/WalletBalanceInsufficientModal.tsx index dc30b59..dae7cdf 100644 --- a/src/renderer/components/blocks/modals/WalletBalanceInsufficientModal/WalletBalanceInsufficientModal.tsx +++ b/src/renderer/components/blocks/modals/WalletBalanceInsufficientModal/WalletBalanceInsufficientModal.tsx @@ -29,7 +29,7 @@ const WalletBalanceInsufficientErrorModal: React.FC + />.

. diff --git a/src/renderer/translations/tokens/en-US.json b/src/renderer/translations/tokens/en-US.json index d758f18..dcc2220 100644 --- a/src/renderer/translations/tokens/en-US.json +++ b/src/renderer/translations/tokens/en-US.json @@ -126,5 +126,7 @@ "are-you-sure-you-want-to-delete-this-mirror": "Are you sure you want to delete this mirror", "yes-delete-mirror": "Yes, Delete Mirror", "an-error-occurred-while-deleting-the-mirror": "An error occurred while deleting the mirror", - "unchanged-fields-will-default-to-currently-displayed-values": "Unchanged fields will default to currently displayed values" + "unchanged-fields-will-default-to-currently-displayed-values": "Unchanged fields will default to currently displayed values", + "unable-to-add-mirror": "Unable to add mirror", + "an-error-occurred-while-adding-the-mirror": "An error occurred while adding the mirror" } \ No newline at end of file