generated from scaffold-eth/scaffold-eth-2
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d574a29
commit 61ecfaf
Showing
7 changed files
with
183 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { forwardRef, useRef } from "react"; | ||
import { useReviewGrant } from "../hooks/useReviewGrant"; | ||
import { GrantData } from "~~/services/database/schema"; | ||
import { PROPOSAL_STATUS } from "~~/utils/grants"; | ||
|
||
type ActionModalProps = { | ||
grant: GrantData; | ||
initialTxLink?: string; | ||
}; | ||
|
||
export const ActionModal = forwardRef<HTMLDialogElement, ActionModalProps>(({ grant, initialTxLink }, ref) => { | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const { handleReviewGrant, isLoading } = useReviewGrant(grant); | ||
|
||
const acceptStatus = grant.status === PROPOSAL_STATUS.PROPOSED ? PROPOSAL_STATUS.APPROVED : PROPOSAL_STATUS.COMPLETED; | ||
const acceptLabel = grant.status === PROPOSAL_STATUS.PROPOSED ? "Approve" : "Complete"; | ||
return ( | ||
<dialog id="action_modal" className="modal" ref={ref}> | ||
<div className="modal-box flex flex-col space-y-3"> | ||
<form method="dialog"> | ||
{/* if there is a button in form, it will close the modal */} | ||
<button className="btn btn-sm btn-circle btn-ghost absolute right-2 top-2">✕</button> | ||
</form> | ||
<p className="font-bold text-lg m-0">{acceptLabel} this grant</p> | ||
<input | ||
type="text" | ||
ref={inputRef} | ||
defaultValue={initialTxLink ?? ""} | ||
placeholder="Transaction hash" | ||
className="input input-bordered" | ||
/> | ||
<button | ||
className={`btn btn-sm btn-success ${isLoading ? "opacity-50" : ""}`} | ||
onClick={() => handleReviewGrant(acceptStatus, inputRef.current?.value)} | ||
disabled={isLoading} | ||
> | ||
{isLoading && <span className="loading loading-spinner"></span>} | ||
{acceptLabel} | ||
</button> | ||
</div> | ||
</dialog> | ||
); | ||
}); | ||
|
||
ActionModal.displayName = "ActionModal"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { useSWRConfig } from "swr"; | ||
import useSWRMutation from "swr/mutation"; | ||
import { useAccount, useSignTypedData } from "wagmi"; | ||
import { GrantData } from "~~/services/database/schema"; | ||
import { EIP_712_DOMAIN, EIP_712_TYPES__REVIEW_GRANT } from "~~/utils/eip712"; | ||
import { ProposalStatusType } from "~~/utils/grants"; | ||
import { notification } from "~~/utils/scaffold-eth"; | ||
import { postMutationFetcher } from "~~/utils/swr"; | ||
|
||
type ReqBody = { | ||
signer: string; | ||
signature: `0x${string}`; | ||
action: ProposalStatusType; | ||
txHash: string; | ||
}; | ||
|
||
export const useReviewGrant = (grant: GrantData) => { | ||
const { address } = useAccount(); | ||
const { signTypedDataAsync, isLoading: isSigningMessage } = useSignTypedData(); | ||
const { trigger: postReviewGrant, isMutating: isPostingNewGrant } = useSWRMutation( | ||
`/api/grants/${grant.id}/review`, | ||
postMutationFetcher<ReqBody>, | ||
); | ||
const { mutate } = useSWRConfig(); | ||
|
||
const isLoading = isSigningMessage || isPostingNewGrant; | ||
|
||
const handleReviewGrant = async (action: ProposalStatusType, txnHash = "") => { | ||
if (!address) { | ||
notification.error("Please connect your wallet"); | ||
return; | ||
} | ||
|
||
let signature; | ||
try { | ||
signature = await signTypedDataAsync({ | ||
domain: EIP_712_DOMAIN, | ||
types: EIP_712_TYPES__REVIEW_GRANT, | ||
primaryType: "Message", | ||
message: { | ||
grantId: grant.id, | ||
action: action, | ||
txHash: txnHash, | ||
}, | ||
}); | ||
} catch (e) { | ||
console.error("Error signing message", e); | ||
notification.error("Error signing message"); | ||
return; | ||
} | ||
|
||
let notificationId; | ||
try { | ||
notificationId = notification.loading("Submitting review"); | ||
await postReviewGrant({ signer: address, signature, action, txHash: txnHash }); | ||
await mutate("/api/grants/review"); | ||
notification.remove(notificationId); | ||
notification.success(`Grant reviewed: ${action}`); | ||
} catch (error) { | ||
notification.error("Error reviewing grant"); | ||
} finally { | ||
if (notificationId) notification.remove(notificationId); | ||
} | ||
}; | ||
|
||
return { handleReviewGrant, isLoading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters