-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added revoke and accept hooks (#16)
- Loading branch information
1 parent
351c8bb
commit 1aa0a51
Showing
8 changed files
with
651 additions
and
442 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
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 |
---|---|---|
@@ -1,10 +1,12 @@ | ||
export * from './useAcceptSubname'; | ||
export * from './useAccountInvitations'; | ||
export * from './useAccountSubnames'; | ||
export * from './useAddSubname'; | ||
export * from './useCommunitySubnames'; | ||
export * from './useIsSubnameAvailable'; | ||
export * from './useMounted'; | ||
export * from './useRevokeSubname'; | ||
export * from './useSearchSubnames'; | ||
export * from './useSubname'; | ||
export * from './useSubnameSignature'; | ||
export * from './useAddSubname' | ||
export * from './useUpdateSubname' | ||
export * from './useUpdateSubname'; |
95 changes: 95 additions & 0 deletions
95
packages/@justaname.id/react/src/lib/hooks/useAcceptSubname.ts
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,95 @@ | ||
'use client'; | ||
|
||
import { Address, TextRecord, SubnameAcceptResponse } from '@justaname.id/sdk'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useJustaName } from '../providers'; | ||
import { useAccountSubnames } from './useAccountSubnames'; | ||
import { useMountedAccount } from './useMountedAccount'; | ||
import { useSubnameSignature } from './useSubnameSignature'; | ||
|
||
export interface BaseAcceptSubnameRequest { | ||
username: string; | ||
|
||
ensDomain: string; | ||
|
||
chainId: number; | ||
|
||
addresses?: Address[]; | ||
|
||
text?: TextRecord[]; | ||
|
||
contentHash?: string; | ||
} | ||
|
||
/** | ||
* Interface defining the parameters needed to accept a subname. | ||
* | ||
* @typedef UseAcceptSubname | ||
* @type {object} | ||
* @property {function} acceptSubname - The function to accept a subname. | ||
* @property {boolean} acceptSubnamePending - Indicates if the mutation is currently pending. | ||
* @template T - The type of additional parameters that can be passed to the accept subname mutation, extending the base request. | ||
*/ | ||
export interface UseAcceptSubname<T = any> { | ||
acceptSubname: ( | ||
params: T & BaseAcceptSubnameRequest | ||
) => Promise<SubnameAcceptResponse>; | ||
acceptSubnamePending: boolean; | ||
} | ||
/** | ||
* Custom hook for performing a mutation to accept a subname. | ||
* | ||
* @template T - The type of additional parameters that can be passed to the accept subname mutation, extending the base request. | ||
* @returns {UseAcceptSubname} An object containing the `acceptSubname` async function to initiate the subname accept, and a boolean `acceptSubnamePending` indicating the mutation's pending state. | ||
*/ | ||
export const useAcceptSubname = <T = any>(): UseAcceptSubname<T> => { | ||
const { backendUrl, routes } = useJustaName(); | ||
const { address } = useMountedAccount(); | ||
const { getSignature } = useSubnameSignature({ | ||
backendUrl, | ||
requestChallengeRoute: routes.requestChallengeRoute, | ||
}); | ||
const { refetchSubnames } = useAccountSubnames(); | ||
|
||
const mutate = useMutation< | ||
SubnameAcceptResponse, | ||
Error, | ||
T & BaseAcceptSubnameRequest | ||
>({ | ||
mutationFn: async (params: T & BaseAcceptSubnameRequest) => { | ||
if (!address) { | ||
throw new Error('No address found'); | ||
} | ||
|
||
const signature = await getSignature(); | ||
|
||
const response = await fetch(backendUrl + routes.acceptSubnameRoute, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
...params, | ||
signature: signature.signature, | ||
address: address, | ||
message: signature.message, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
|
||
const data: SubnameAcceptResponse = await response.json(); | ||
refetchSubnames(); | ||
return data; | ||
}, | ||
}); | ||
|
||
return { | ||
acceptSubname: mutate.mutateAsync as ( | ||
params: T & BaseAcceptSubnameRequest | ||
) => Promise<SubnameAcceptResponse>, | ||
acceptSubnamePending: mutate.isPending, | ||
}; | ||
}; |
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
89 changes: 89 additions & 0 deletions
89
packages/@justaname.id/react/src/lib/hooks/useRevokeSubname.ts
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,89 @@ | ||
'use client'; | ||
|
||
import { SubnameRevokeResponse } from '@justaname.id/sdk'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useJustaName } from '../providers'; | ||
import { useAccountSubnames } from './useAccountSubnames'; | ||
import { useMountedAccount } from './useMountedAccount'; | ||
import { useSubnameSignature } from './useSubnameSignature'; | ||
|
||
export interface BaseRevokeSubnameRequest { | ||
ensDomain: string; | ||
|
||
username: string; | ||
|
||
chainId: number; | ||
} | ||
|
||
/** | ||
* Interface defining the parameters needed to revoke a subname. | ||
* | ||
* @typedef UseRevokeSubname | ||
* @type {object} | ||
* @property {function} revokeSubname - The function to revoke a subname. | ||
* @property {boolean} revokeSubnamePending - Indicates if the mutation is currently pending. | ||
* @template T - The type of additional parameters that can be passed to the revoke subname mutation, extending the base request. | ||
*/ | ||
export interface UseRevokeSubname<T = any> { | ||
revokeSubname: ( | ||
params: T & BaseRevokeSubnameRequest | ||
) => Promise<SubnameRevokeResponse>; | ||
revokeSubnamePending: boolean; | ||
} | ||
/** | ||
* Custom hook for performing a mutation to revoke a subname. | ||
* | ||
* @template T - The type of additional parameters that can be passed to the revoke subname mutation, extending the base request. | ||
* @returns {UseRevokeSubname} An object containing the `revokeSubname` async function to initiate the subname revoke, and a boolean `revokeSubnamePending` indicating the mutation's pending state. | ||
*/ | ||
export const useRevokeSubname = <T = any>(): UseRevokeSubname<T> => { | ||
const { backendUrl, routes } = useJustaName(); | ||
const { address } = useMountedAccount(); | ||
const { getSignature } = useSubnameSignature({ | ||
backendUrl, | ||
requestChallengeRoute: routes.requestChallengeRoute, | ||
}); | ||
const { refetchSubnames } = useAccountSubnames(); | ||
|
||
const mutate = useMutation< | ||
SubnameRevokeResponse, | ||
Error, | ||
T & BaseRevokeSubnameRequest | ||
>({ | ||
mutationFn: async (params: T & BaseRevokeSubnameRequest) => { | ||
if (!address) { | ||
throw new Error('No address found'); | ||
} | ||
|
||
const signature = await getSignature(); | ||
|
||
const response = await fetch(backendUrl + routes.revokeSubnameRoute, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
...params, | ||
signature: signature.signature, | ||
address: address, | ||
message: signature.message, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('Network response was not ok'); | ||
} | ||
|
||
const data: SubnameRevokeResponse = await response.json(); | ||
refetchSubnames(); | ||
return data; | ||
}, | ||
}); | ||
|
||
return { | ||
revokeSubname: mutate.mutateAsync as ( | ||
params: T & BaseRevokeSubnameRequest | ||
) => Promise<SubnameRevokeResponse>, | ||
revokeSubnamePending: mutate.isPending, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from './lib/justaname'; | ||
export * from './lib/features'; | ||
export * from './lib/types'; | ||
export * from './lib/api/routes' | ||
export * from './lib/api/routes'; |
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
Oops, something went wrong.