2022-07-28 05:13:42 -07:00
|
|
|
import { ModalProps } from '../../types/modal'
|
|
|
|
import Modal from '../shared/Modal'
|
2022-09-12 08:53:57 -07:00
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-07-28 05:13:42 -07:00
|
|
|
import { notify } from '../../utils/notifications'
|
|
|
|
import Button from '../shared/Button'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-02-27 23:20:11 -08:00
|
|
|
import { ChangeEvent, useCallback, useState } from 'react'
|
2022-07-28 05:13:42 -07:00
|
|
|
import BounceLoader from '../shared/BounceLoader'
|
|
|
|
import Input from '../forms/Input'
|
|
|
|
import Label from '../forms/Label'
|
2022-11-18 09:09:39 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
2023-02-27 23:20:11 -08:00
|
|
|
import { isMangoError } from 'types'
|
2022-07-28 05:13:42 -07:00
|
|
|
|
|
|
|
const AccountNameModal = ({ isOpen, onClose }: ModalProps) => {
|
|
|
|
const { t } = useTranslation('common')
|
2022-11-18 09:09:39 -08:00
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-07-28 05:13:42 -07:00
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const [name, setName] = useState(mangoAccount?.name || '')
|
|
|
|
|
2023-02-27 23:20:11 -08:00
|
|
|
const handleUpdateccountName = useCallback(async () => {
|
2022-07-28 05:13:42 -07:00
|
|
|
const client = mangoStore.getState().client
|
|
|
|
const group = mangoStore.getState().group
|
2022-08-08 14:45:56 -07:00
|
|
|
const actions = mangoStore.getState().actions
|
2022-07-28 05:13:42 -07:00
|
|
|
if (!mangoAccount || !group) return
|
|
|
|
setLoading(true)
|
|
|
|
try {
|
2023-08-12 11:40:09 -07:00
|
|
|
const { signature: tx, slot } = await client.editMangoAccount(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
name,
|
|
|
|
)
|
2022-08-08 14:45:56 -07:00
|
|
|
|
|
|
|
setLoading(false)
|
|
|
|
onClose()
|
|
|
|
notify({
|
|
|
|
title: t('account-update-success'),
|
|
|
|
type: 'success',
|
|
|
|
txid: tx,
|
|
|
|
})
|
2023-08-12 11:40:09 -07:00
|
|
|
await actions.reloadMangoAccount(slot)
|
2023-02-27 23:20:11 -08:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2022-07-28 05:13:42 -07:00
|
|
|
setLoading(false)
|
2023-02-27 23:20:11 -08:00
|
|
|
if (!isMangoError(e)) return
|
2022-07-28 05:13:42 -07:00
|
|
|
notify({
|
|
|
|
title: t('account-update-failed'),
|
2022-12-02 15:47:08 -08:00
|
|
|
txid: e?.txid,
|
2022-07-28 05:13:42 -07:00
|
|
|
type: 'error',
|
|
|
|
})
|
|
|
|
}
|
2023-02-27 23:20:11 -08:00
|
|
|
}, [mangoAccount, name, t, onClose])
|
2022-07-28 05:13:42 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal isOpen={isOpen} onClose={onClose}>
|
|
|
|
<div className="h-64">
|
|
|
|
{loading ? (
|
|
|
|
<BounceLoader loadingMessage={t('updating-account-name')} />
|
|
|
|
) : (
|
|
|
|
<div className="flex h-full flex-col justify-between">
|
|
|
|
<div className="pb-4">
|
|
|
|
<h2 className="mb-1">{t('edit-account')}</h2>
|
|
|
|
<p className="mb-4">{t('account-name-desc')}</p>
|
|
|
|
<Label text={t('account-name')} />
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
name="name"
|
|
|
|
id="name"
|
2022-11-18 01:53:45 -08:00
|
|
|
placeholder="e.g. Sweet Caroline"
|
2022-07-28 05:13:42 -07:00
|
|
|
value={name}
|
|
|
|
onChange={(e: ChangeEvent<HTMLInputElement>) =>
|
|
|
|
setName(e.target.value)
|
|
|
|
}
|
2023-01-25 01:19:12 -08:00
|
|
|
maxLength={30}
|
2022-07-28 05:13:42 -07:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Button
|
|
|
|
className="w-full"
|
|
|
|
onClick={handleUpdateccountName}
|
|
|
|
size="large"
|
|
|
|
>
|
|
|
|
{t('update')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AccountNameModal
|