From 8bc91139f43ba2b665a3ed3499639525846b2c18 Mon Sep 17 00:00:00 2001 From: saml33 Date: Tue, 2 Aug 2022 13:43:17 +1000 Subject: [PATCH] add create account modal --- components/Layout.tsx | 133 +++++++++++--------- components/modals/AccountNameModal.tsx | 2 +- components/modals/CreateNewAccountModal.tsx | 76 +++++++++++ components/shared/Button.tsx | 2 +- public/locales/en/common.json | 6 + public/locales/es/common.json | 6 + public/locales/zh/common.json | 6 + public/locales/zh_tw/common.json | 6 + 8 files changed, 176 insertions(+), 61 deletions(-) create mode 100644 components/modals/CreateNewAccountModal.tsx diff --git a/components/Layout.tsx b/components/Layout.tsx index 279a835d..de331560 100644 --- a/components/Layout.tsx +++ b/components/Layout.tsx @@ -5,6 +5,7 @@ import { CheckCircleIcon, ChevronDownIcon, ChevronRightIcon, + PlusCircleIcon, } from '@heroicons/react/solid' import { useViewport } from '../hooks/useViewport' import { breakpoints } from '../utils/theme' @@ -21,6 +22,7 @@ import ConnectedMenu from './wallet/ConnectedMenu' import WalletIcon from './icons/WalletIcon' import BounceLoader from './shared/BounceLoader' import { LinkButton } from './shared/Button' +import CreateNewAccountModal from './modals/CreateNewAccountModal' export const IS_ONBOARDED_KEY = 'isOnboarded' @@ -150,67 +152,80 @@ const MangoAccountsList = ({ mangoAccount: MangoAccount }) => { const mangoAccounts = mangoStore((s) => s.mangoAccounts) + const [showNewAccountModal, setShowNewAccountModal] = useState(false) return ( - - {({ open }) => ( - <> - -
-
-

{mangoAccount.name}

-
- -

- {mangoAccount.getHealthRatio(HealthType.init).toNumber()}% -

+ <> + + {({ open }) => ( + <> + +
+
+

{mangoAccount.name}

+
+ +

+ {mangoAccount.getHealthRatio(HealthType.init).toNumber()}% +

+
-
- - - - - {mangoAccounts.length ? ( - mangoAccounts.map((acc) => ( -
- - - New Account - -
- )) - ) : ( -

Loading...

- )} -
-
- - )} - + + + + + {mangoAccounts.length ? ( + mangoAccounts.map((acc) => ( +
+ + } + onClick={() => setShowNewAccountModal(true)} + > + New Account + +
+ )) + ) : ( +

Loading...

+ )} +
+
+ + )} + + {showNewAccountModal ? ( + setShowNewAccountModal(false)} + /> + ) : null} + ) } diff --git a/components/modals/AccountNameModal.tsx b/components/modals/AccountNameModal.tsx index c5984739..883543fd 100644 --- a/components/modals/AccountNameModal.tsx +++ b/components/modals/AccountNameModal.tsx @@ -27,7 +27,7 @@ const AccountNameModal = ({ isOpen, onClose }: ModalProps) => { setLoading(false) onClose() notify({ - title: t('account-updated'), + title: t('account-update-success'), type: 'success', txid: tx, }) diff --git a/components/modals/CreateNewAccountModal.tsx b/components/modals/CreateNewAccountModal.tsx new file mode 100644 index 00000000..3510b558 --- /dev/null +++ b/components/modals/CreateNewAccountModal.tsx @@ -0,0 +1,76 @@ +import { ModalProps } from '../../types/modal' +import Modal from '../shared/Modal' +import mangoStore from '../../store/state' +import { notify } from '../../utils/notifications' +import Button from '../shared/Button' +import { useTranslation } from 'next-i18next' +import { ChangeEvent, useState } from 'react' +import BounceLoader from '../shared/BounceLoader' +import Input from '../forms/Input' +import Label from '../forms/Label' + +const CreateNewAccountModal = ({ isOpen, onClose }: ModalProps) => { + const { t } = useTranslation('common') + const mangoAccount = mangoStore((s) => s.mangoAccount.current) + const [loading, setLoading] = useState(false) + const [name, setName] = useState(mangoAccount?.name || '') + + // This doesn't work yet... + const handleNewAccount = async () => { + const client = mangoStore.getState().client + const group = mangoStore.getState().group + if (!mangoAccount || !group) return + setLoading(true) + try { + const tx = await client.createMangoAccount(group, 0, name) + if (tx) { + setLoading(false) + onClose() + notify({ + title: t('new-account-success'), + type: 'success', + txid: tx, + }) + } + } catch (e) { + setLoading(false) + notify({ + title: t('new-account-failed'), + type: 'error', + }) + console.log(e) + } + } + + return ( + +
+ {loading ? ( + + ) : ( +
+
+

{t('new-account')}

+
+ +
+ )} +
+
+ ) +} + +export default CreateNewAccountModal diff --git a/components/shared/Button.tsx b/components/shared/Button.tsx index e070251c..db98c10a 100644 --- a/components/shared/Button.tsx +++ b/components/shared/Button.tsx @@ -108,7 +108,7 @@ export const LinkButton: FunctionComponent = ({ {...props} > {icon} - {children} + {children} ) } diff --git a/public/locales/en/common.json b/public/locales/en/common.json index c0d7266f..4919398f 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -2,6 +2,8 @@ "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", + "account-update-failed": "Failed to update account", + "account-update-success": "Account updated successfully", "account-value": "Account Value", "available": "Available", "available-balance": "Available Balance", @@ -19,6 +21,7 @@ "collateral-value": "Collateral Value", "connect": "Connect", "connect-helper": "Connect to get started", + "create-account": "Create Account", "daily-volume": "24h Volume", "deposit": "Deposit", "deposit-value": "Deposit Value", @@ -36,6 +39,9 @@ "margin": "Margin", "max": "Max", "more": "More", + "new-account": "New Account", + "new-account-failed": "Failed to create account", + "new-account-success": "Your new account is ready 😎", "portfolio": "Portfolio", "price": "Price", "rate": "Rate (APR)", diff --git a/public/locales/es/common.json b/public/locales/es/common.json index c0112edd..43d648b8 100644 --- a/public/locales/es/common.json +++ b/public/locales/es/common.json @@ -2,6 +2,8 @@ "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", + "account-update-failed": "Failed to update account", + "account-update-success": "Account updated successfully", "account-value": "Account Value", "available": "Available", "available-balance": "Available Balance", @@ -19,6 +21,7 @@ "collateral-value": "Collateral Value", "connect": "Connect", "connect-helper": "Connect to get started", + "create-account": "Create Account", "daily-volume": "24h Volume", "deposit": "Deposit", "deposit-value": "Deposit Value", @@ -36,6 +39,9 @@ "margin": "Margin", "max": "Max", "more": "More", + "new-account": "New Account", + "new-account-failed": "Failed to create account", + "new-account-success": "Your new account is ready 😎", "portfolio": "Portfolio", "price": "Price", "rate": "Rate (APR)", diff --git a/public/locales/zh/common.json b/public/locales/zh/common.json index 9af945cf..480b8a4a 100644 --- a/public/locales/zh/common.json +++ b/public/locales/zh/common.json @@ -2,6 +2,8 @@ "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", + "account-update-failed": "Failed to update account", + "account-update-success": "Account updated successfully", "account-value": "Account Value", "available": "Available", "available-balance": "Available Balance", @@ -19,6 +21,7 @@ "collateral-value": "Collateral Value", "connect": "Connect", "connect-helper": "Connect to get started", + "create-account": "Create Account", "daily-volume": "24h Volume", "deposit": "Deposit", "deposit-value": "Deposit Value", @@ -36,6 +39,9 @@ "margin": "Margin", "max": "Max", "more": "More", + "new-account": "New Account", + "new-account-failed": "Failed to create account", + "new-account-success": "Your new account is ready 😎", "portfolio": "Portfolio", "price": "Price", "rate": "Rate (APR)", diff --git a/public/locales/zh_tw/common.json b/public/locales/zh_tw/common.json index d0b961fd..a8750575 100644 --- a/public/locales/zh_tw/common.json +++ b/public/locales/zh_tw/common.json @@ -2,6 +2,8 @@ "account-closed": "Account Closed 👋", "account-name": "Account Name", "account-name-desc": "Organize your accounts by giving them useful names", + "account-update-failed": "Failed to update account", + "account-update-success": "Account updated successfully", "account-value": "Account Value", "available": "Available", "available-balance": "Available Balance", @@ -19,6 +21,7 @@ "collateral-value": "Collateral Value", "connect": "Connect", "connect-helper": "Connect to get started", + "create-account": "Create Account", "daily-volume": "24h Volume", "deposit": "Deposit", "deposit-value": "Deposit Value", @@ -36,6 +39,9 @@ "margin": "Margin", "max": "Max", "more": "More", + "new-account": "New Account", + "new-account-failed": "Failed to create account", + "new-account-success": "Your new account is ready 😎", "portfolio": "Portfolio", "price": "Price", "rate": "Rate (APR)",