Skip to content

Commit

Permalink
feat: added revoke and accept hooks (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony23991 authored May 8, 2024
1 parent 351c8bb commit 1aa0a51
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 442 deletions.
3 changes: 2 additions & 1 deletion packages/@justaname.id/react/src/lib/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*/
export const defaultRoutes = {
addSubnameRoute: '/api/subnames/add',
revokeSubnameRoute: '/api/subnames/revoke',
acceptSubnameRoute: '/api/subnames/accept',
checkSubnameAvailabilityRoute: '/api/subnames/available',
requestChallengeRoute: '/api/request-challenge',
updateSubnameRoute: '/api/subnames/update',
}
};
6 changes: 4 additions & 2 deletions packages/@justaname.id/react/src/lib/hooks/index.ts
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 packages/@justaname.id/react/src/lib/hooks/useAcceptSubname.ts
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,
};
};
72 changes: 38 additions & 34 deletions packages/@justaname.id/react/src/lib/hooks/useAddSubname.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use client";
'use client';

import { useMutation } from '@tanstack/react-query';
import { useJustaName } from '../providers';
import { useMountedAccount } from './useMountedAccount';
import { useSubnameSignature } from './useSubnameSignature';
import { SubnameAcceptResponse } from '@justaname.id/sdk';
import { SubnameAddResponse } from '@justaname.id/sdk';
import { useAccountSubnames } from './useAccountSubnames';

export interface BaseAddSubnameRequest {
Expand All @@ -21,7 +21,9 @@ export interface BaseAddSubnameRequest {
* @template T - The type of additional parameters that can be passed to the claim subname mutation, extending the base request.
*/
export interface UseAddSubname<T = any> {
addSubname: (params: T & BaseAddSubnameRequest) => Promise<SubnameAcceptResponse>;
addSubname: (
params: T & BaseAddSubnameRequest
) => Promise<SubnameAddResponse>;
addSubnamePending: boolean;
}
/**
Expand All @@ -30,52 +32,54 @@ export interface UseAddSubname<T = any> {
* @template T - The type of additional parameters that can be passed to the claim subname mutation, extending the base request.
* @returns {UseAddSubname} An object containing the `addSubname` async function to initiate the subname claim, and a boolean `claimSubnamePending` indicating the mutation's pending state.
*/
export const useAddSubname = <T = any>() : UseAddSubname<T> => {
export const useAddSubname = <T = any>(): UseAddSubname<T> => {
const { backendUrl, routes } = useJustaName();
const { address } = useMountedAccount()
const { getSignature} = useSubnameSignature({
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature({
backendUrl,
requestChallengeRoute: routes.requestChallengeRoute
})
const { refetchSubnames } = useAccountSubnames()
requestChallengeRoute: routes.requestChallengeRoute,
});
const { refetchSubnames } = useAccountSubnames();

const mutate = useMutation<SubnameAcceptResponse, Error, T & BaseAddSubnameRequest>
({
mutationFn: async (
params: T & BaseAddSubnameRequest
) => {
const mutate = useMutation<
SubnameAddResponse,
Error,
T & BaseAddSubnameRequest
>({
mutationFn: async (params: T & BaseAddSubnameRequest) => {
if (!address) {
throw new Error('No address found');
}

const signature = await getSignature()
const signature = await getSignature();

const response = await fetch(
backendUrl + routes.addSubnameRoute, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: params.username,
signature: signature.signature,
address: address,
message: signature.message,
})
});
const response = await fetch(backendUrl + routes.addSubnameRoute, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: params.username,
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()
const data: SubnameAddResponse = await response.json();
refetchSubnames();
return data;
},
})
});

return {
addSubname: mutate.mutateAsync as (params: T & BaseAddSubnameRequest) => Promise<SubnameAcceptResponse>,
addSubname: mutate.mutateAsync as (
params: T & BaseAddSubnameRequest
) => Promise<SubnameAddResponse>,
addSubnamePending: mutate.isPending,
}
}
};
};
89 changes: 89 additions & 0 deletions packages/@justaname.id/react/src/lib/hooks/useRevokeSubname.ts
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,
};
};
2 changes: 1 addition & 1 deletion packages/@justaname.id/sdk/src/index.ts
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';
2 changes: 1 addition & 1 deletion packages/@justaname.id/sdk/src/lib/types/subnames/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export * from './reserve';
export * from './revoke';
export * from './search-subnames';
export * from './update';
export * from './accept'
export * from './accept';
Loading

0 comments on commit 1aa0a51

Please sign in to comment.