Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Edit wallet address in User Profile #630

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions src/modals/EditWalletAddress.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import {
Box,
Button,
CircledIcon,
ModalWrapper,
toast,
useModal
} from '@impact-market/ui';
import { SubmitHandler, useForm, useFormState } from 'react-hook-form';
import { useLoanManager } from '@impact-market/utils';
import { usePrismicData } from '../libs/Prismic/components/PrismicDataProvider';
import { useState } from 'react';
import Input from '../components/Input';
import RichText from '../libs/Prismic/components/RichText';
import processTransactionError from 'src/utils/processTransactionError';
import useTranslations from 'src/libs/Prismic/hooks/useTranslations';

const EditWalletAddress = () => {
const { modals } = usePrismicData();
const { t } = useTranslations();
const {
editWalletAddressTitle,
editWalletAddressDescription,
editWalletAddressPlaceholder,
editWalletAddressSuccess,
editWalletAddressError,
editWalletAddressHint
} = modals?.data;

const [error, setError] = useState(false);
const { handleClose, address } = useModal();
const { control, handleSubmit } = useForm();
const { isSubmitting } = useFormState({
control
});

const { changeUserAddress } = useLoanManager();

const handleCancel = (event: any) => {
event.preventDefault();
handleClose();
};

const onSubmit: SubmitHandler<any> = async (data) => {
if (data?.address.toLowerCase() === address.toLowerCase()) {
setError(true);
} else {
setError(false);

try {
const { status } = await changeUserAddress(
address,
data?.address
);

if (status) {
toast.success(editWalletAddressSuccess);
handleClose();
} else {
toast.error(editWalletAddressError);
}
} catch (error) {
console.log(error);
toast.error(editWalletAddressError);

processTransactionError(error, 'change_address');
}
}
};

return (
<ModalWrapper maxW={'484px'} padding={2} w="100%">
<form onSubmit={handleSubmit(onSubmit)}>
<Box
flex
fDirection={{ sm: 'column', xs: 'column' }}
style={{
alignItems: 'center',
justifyContent: 'center'
}}
>
<Box
flex
fDirection={{ sm: 'column', xs: 'column' }}
style={{
justifyContent: 'start'
}}
w="100%"
>
<CircledIcon icon="edit" medium />
<RichText
content={editWalletAddressTitle}
g900
large
mt={1}
semibold
mb={0.5}
/>
<RichText
content={editWalletAddressDescription}
g500
small
mb={1}
/>
<Input
placeholder={editWalletAddressPlaceholder}
wrapperProps={{
w: '100%'
}}
rules={{
required: true
}}
control={control}
name="address"
withError={error}
hint={error && editWalletAddressHint}
/>
</Box>
<Box
flex
fDirection={{ sm: 'row', xs: 'column' }}
style={{
alignItems: 'start',
gap: '1rem',
justifyContent: 'center'
}}
>
<Button
gray
fluid={'xs'}
mt={{ sm: 1.5, xs: 1.5 }}
onClick={handleCancel}
>
{t('dismiss')}
</Button>
<Button
fluid={'xs'}
mt={{ sm: 1.5, xs: 0 }}
isLoading={isSubmitting}
>
{t('save')}
</Button>
</Box>
</Box>
</form>
</ModalWrapper>
);
};

export default EditWalletAddress;
3 changes: 3 additions & 0 deletions src/modals/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const modals = {
contribute: dynamic(() => import('./Contribute'), { ssr: false }),
createStory: dynamic(() => import('./Stories/CreateStory'), { ssr: false }),
deleteStory: dynamic(() => import('./Stories/DeleteStory'), { ssr: false }),
editWalletAddress: dynamic(() => import('./EditWalletAddress'), {
ssr: false
}),
laeFailedLesson: dynamic(() => import('./LearnAndEarn/WrongAnswer'), {
ssr: false
}),
Expand Down
9 changes: 9 additions & 0 deletions src/views/User/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Text,
TextLink,
ViewContainer,
openModal,
toast
} from '@impact-market/ui';
import { formatAddress } from '../../utils/formatAddress';
Expand Down Expand Up @@ -142,6 +143,14 @@ const User: React.FC<{ isLoading?: boolean }> = (props) => {
icon: 'copy',
onClick: () => copyToClipboard(),
title: t('copyAddress')
},
{
icon: 'edit',
onClick: () =>
openModal('editWalletAddress', {
address: user?.address
}),
title: 'Edit wallet address'
}
]}
title={formatAddress(user?.address, [6, 4])}
Expand Down