Skip to content

Commit

Permalink
fix: endorsement modal (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: andyv09 <andraz.vrecko@gmail.com>
Co-authored-by: martines3000 <domajnko.martin@gmail.com>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent 86c9faf commit d15e93b
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 109 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 34 additions & 52 deletions packages/dapp/src/components/EndorseForm/EndorsementModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import Link from 'next/link';
import { TwitterIcon, TwitterShareButton } from 'react-share';
import { MemoizedImage } from '@/components/MemoizedImage';
import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { Card } from '@/components/ui/card';
import { PlatformType, formatHandle, startsWithVowel } from '@/utils';
import { Skeleton } from '../ui/skeleton';
import { EndorseeSkeleton } from '../Endorsee';
import { useEndorsementStore } from '@/stores';

type EndorsementModalProps = {
Expand All @@ -29,29 +26,41 @@ export const EndorsementModal = ({
shareLink,
endorsee,
}: EndorsementModalProps) => {
// TODO[Martin]: Add network name, so we can support multiple networks
// const shareLink = `${APP_URL}/endorsement/${endorsementId}`;
const { displayValue, endorsementType, platform } = useEndorsementStore(
(state) => ({
displayValue: state.displayValue,
endorsementType: state.endorsementType,
platform: state.platform,
})
);

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-[625px] p-4">
{shareLink && (
<>
<DialogHeader>
<DialogTitle>Endorsement</DialogTitle>
<DialogDescription>Thank you for endorsing</DialogDescription>
<DialogTitle>Success</DialogTitle>
<DialogDescription>
Thank you for endorsing {displayValue}!
</DialogDescription>
</DialogHeader>
<Card className="p-4 flex max-sm:flex-col max-sm:pt-4 max-sm:gap-y-4 justify-center items-center">
{endorsee}
</Card>

<div className="p-4 bg-contain bg-no-repeat gap-y-2 md:w-[600px] md:h-[316px] bg-share-bg flex justify-start items-center">
<div className="flex flex-col gap-y-4 ml-4">
<div className="flex items-center justify-center">
{endorsee}
</div>
<div className="md:text-xl font-medium">
{endorsementType === 'Based energy 🔵' ? 'for' : 'as a'}
</div>
<div className="md:text-3xl text-md font-semibold text-primary-500">
{endorsementType}
</div>
</div>
</div>
<div className="mt-4">
<div className="flex max-sm:flex-col gap-x-2 items-center">
<div className="flex max-sm:flex-col gap-x-2 font-medium items-center">
<div>Share this moment on</div>
<div className="flex items-center gap-x-2 max-sm:pt-2">
<TwitterShareButton
Expand Down Expand Up @@ -103,52 +112,25 @@ export const EndorsementModal = ({
height={32}
/>
</Link>
<div>
or{' '}
<Button
size="sm"
variant="outline"
onClick={() => navigator.clipboard.writeText(shareLink)}
>
Copy Link
</Button>
</div>
</div>
</div>
<div className="mt-4 flex items-center gap-x-2">
<Input value={shareLink} />
<Button
size="lg"
variant="outline"
onClick={() => navigator.clipboard.writeText(shareLink)}
>
Copy Link
</Button>
</div>
</div>
</>
)}
{!shareLink && <EndorsementModalSkeleton />}
<DialogFooter>
<Button onClick={() => setOpen(false)}>Continue Endorsing</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
};

export const EndorsementModalSkeleton = () => {
return (
<>
<DialogHeader>
<DialogTitle>Endorsement</DialogTitle>
<DialogDescription>Thank you for endorsing</DialogDescription>
</DialogHeader>
<Card className="p-4 flex max-sm:flex-col max-sm:pt-4 max-sm:gap-y-4 justify-center items-center">
<EndorseeSkeleton />
</Card>
<div className="mt-4">
<div className="flex max-sm:flex-col gap-x-2 items-center">
<Skeleton className="h-6 w-[165px]" />
<div className="flex items-center gap-x-2 max-sm:pt-2">
<Skeleton className="h-8 w-8 rounded-full" />
<Skeleton className="h-8 w-8 rounded-full" />
<Skeleton className="h-8 w-8 rounded-full" />
</div>
</div>

<div className="mt-4 flex items-center gap-x-2">
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-[132px]" />
</div>
</div>
</>
);
};
126 changes: 77 additions & 49 deletions packages/dapp/src/components/EndorseForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
encodeFunctionData,
parseEventLogs,
} from 'viem';
import { ReloadIcon } from '@radix-ui/react-icons';
import { useEndorsementStore } from '@/stores';
import { toast } from 'sonner';
import { CheckCircle2, CircleAlert } from 'lucide-react';
Expand All @@ -38,7 +37,9 @@ import {
import { cn } from '@/lib/utils';
import { ConnectButtonCustom } from '@/components/ConnectButtonCustom';
import { EndorsementModal } from './EndorsementModal';
import { APP_URL } from '@/utils';
import { APP_URL, formatAddress } from '@/utils';
import { waitForTransactionReceipt } from '@wagmi/core';
import { Spinner } from '../ui/spinner';

type EndorseeProps = {
endorsee: React.ReactNode;
Expand Down Expand Up @@ -76,7 +77,6 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
>();
const [previousErrorInsufficientFunds, setPreviousErrorInsufficientFunds] =
useState<boolean>(false);

// Hooks
const { address: connectedAccount, chainId } = useAccount();

Expand All @@ -99,18 +99,17 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
[displayValue, comment, endorsementType, address]
);

const {
writeContractAsync,
isPending,
data: transactionHash,
} = useWriteContract({
const { writeContractAsync, data: transactionHash } = useWriteContract({
config: config,
});

const { data: txReceipt, isLoading: isReceiptLoading } =
useWaitForTransactionReceipt({
hash: transactionHash,
});
const {
data: txReceipt,
isSuccess: isReceiptSuccess,
isLoading: isReceiptLoading,
} = useWaitForTransactionReceipt({
hash: transactionHash,
});

const endorsementData = useMemo(() => {
if (!txReceipt) return null;
Expand Down Expand Up @@ -194,7 +193,7 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
}, [isGasEstimateFetching, gasEstimateError]);

// Functions
const handleEnorsment = async () => {
const handleEndorsment = async () => {
if (
!address ||
!displayValue ||
Expand All @@ -215,36 +214,8 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
args: [address, endorsementType, comment, _displayValue],
value: _value,
})
.then((txHash) => {
.then(() => {
partialClear();
toast(
<div className="flex gap-x-2">
<div className="flex items-center justify-center">
<CheckCircle2 className="w-6 h-6 text-green-500" />
</div>
<div className="flex flex-col gap-y-1">
<h3 className="font-semibold justify-start items-center">
Transaction submitted
</h3>
<div className="flex justify-start items-center text-xs">
Hash:
<Button
variant="link"
size="sm"
className="p-0 pl-1 text-xs h-4"
onClick={() =>
window.open(`${EXPLORERS[chainId]}/tx/${txHash}`, '_blank')
}
>
{`${txHash.slice(0, 4)}...${txHash.slice(-4)}`}
</Button>
</div>
</div>
</div>,
{
duration: 4000,
}
);
})
.catch((error: WriteContractErrorType) => {
if (error.name === 'TransactionExecutionError') {
Expand Down Expand Up @@ -282,8 +253,62 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
};

useEffect(() => {
if (isReceiptLoading) setEndorsementModalOpen(true);
}, [isReceiptLoading]);
if (isReceiptSuccess) setEndorsementModalOpen(true);
}, [isReceiptSuccess]);

useEffect(() => {
if (transactionHash === undefined) return;
const transactionReceipt = waitForTransactionReceipt(config, {
hash: transactionHash,
});

toast.promise(transactionReceipt, {
loading: (
<div className="flex gap-x-2">
<div className="flex items-center justify-center">
<div role="status">
<Spinner />
<span className="sr-only">Loading...</span>
</div>
</div>
<div className="flex flex-col gap-y-1">
<h3 className="font-semibold justify-start items-center">
Transaction submitted
</h3>
<div className="flex justify-start items-center text-xs">
Hash:
<Button
variant="link"
size="sm"
className="p-0 pl-1 text-xs h-4"
onClick={() =>
window.open(
`${EXPLORERS[chainId!]}/tx/${transactionHash}`,
'_blank'
)
}
>
{formatAddress(transactionHash)}
</Button>
</div>
</div>
</div>
),
success: (
<div className="flex gap-x-2">
<div className="flex items-center justify-center">
<CheckCircle2 className="w-6 h-6 text-green-500" />
</div>
<div className="flex flex-col gap-y-1">
<h3 className="font-semibold justify-start items-center">
Your endorsement of {displayValue} was successful!{' '}
</h3>
</div>
</div>
),
error: 'Error',
});
}, [transactionHash]);

return (
<>
Expand Down Expand Up @@ -311,7 +336,7 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
<Button
size="lg"
className={cn('w-full text-xl py-6', 'disabled:bg-gray-300')}
onMouseDown={handleEnorsment}
onMouseDown={handleEndorsment}
disabled={
!intro &&
(areRequestsPending ||
Expand All @@ -321,8 +346,11 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
isGasEstimateFetching)
}
>
{isPending && <ReloadIcon className="mr-2 h-4 w-4 animate-spin" />}
{previousErrorInsufficientFunds ? 'Insufficient funds' : 'Endorse'}
{!isReceiptLoading &&
(previousErrorInsufficientFunds
? 'Insufficient funds'
: 'Endorse')}
{isReceiptLoading && <Spinner />}
</Button>
)}
<FeeDisplay
Expand All @@ -334,8 +362,8 @@ export const EndorseForm = ({ endorsee }: EndorseeProps) => {
open={endorsementModalOpen}
setOpen={setEndorsementModalOpen}
shareLink={
displayValue && platform && endorsementData?.uid
? `${APP_URL}/profile/${displayValue}?platform=${platform}&tab=explorer&endorsementId=${endorsementData?.uid}`
displayValue && platform && endorsementData
? `${APP_URL}/profile/${displayValue}?platform=${platform}&tab=explorer&endorsementId=${endorsementData.uid}`
: null
}
endorsee={endorsee}
Expand Down
7 changes: 4 additions & 3 deletions packages/dapp/src/components/Endorsee/EndorseeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ export function EndorseeDialog() {
}) => {
// enter
if (e.keyCode === 13) {
const _value = suggestions[activeIndex]
? suggestions[activeIndex].label
const index = activeIndex === -1 ? 0 : activeIndex;
const _value = suggestions[index]
? suggestions[index].label
: query.replaceAll('。', '.');
selectUser(suggestions[activeIndex].key, _value);
selectUser(suggestions[index].key, _value);
}
// escape
if (e.keyCode === 27) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ export const InteractiveSocialGraph = ({
});
}}
onLinkClick={(link) => {
console.log(link);
handleEndorsementSelection(link.easUid, (link.target as any).id);
}}
linkLabel={(link) => {
Expand Down
7 changes: 4 additions & 3 deletions packages/dapp/src/components/Navbar/Searchbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ export const Searchbar = () => {
}) => {
// enter
if (e.keyCode === 13) {
const _value = suggestions[activeIndex]
? suggestions[activeIndex].label
const index = activeIndex === -1 ? 0 : activeIndex;
const _value = suggestions[index]
? suggestions[index].label
: query.replaceAll('。', '.');
selectUser(suggestions[activeIndex].key, _value);
selectUser(suggestions[index].key, _value);
}
// escape
if (e.keyCode === 27) {
Expand Down
25 changes: 25 additions & 0 deletions packages/dapp/src/components/ui/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

export const Spinner = () => {
return (
<div role="status">
<svg
aria-hidden="true"
className="w-6 h-6 ml-2 animate-spin text-primary fill-white"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
};
Loading

0 comments on commit d15e93b

Please sign in to comment.