From 447430747510c3a0b1353c0d69fa28e7d09a6e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Str=C4=85czek?= Date: Wed, 26 Jun 2024 16:33:35 +0200 Subject: [PATCH] Add remove pending allocation button --- src/components/cards/AppInfoCard.tsx | 33 +++++++++++++++++- src/hooks/useApplicationActions.ts | 52 +++++++++++++++++++++------- src/lib/apiClient.ts | 33 +++++++++++++++--- 3 files changed, 101 insertions(+), 17 deletions(-) diff --git a/src/components/cards/AppInfoCard.tsx b/src/components/cards/AppInfoCard.tsx index 7cbdde0..40113e2 100644 --- a/src/components/cards/AppInfoCard.tsx +++ b/src/components/cards/AppInfoCard.tsx @@ -17,9 +17,9 @@ import { } from '@/lib/utils' import { LDNActorType, + RefillUnit, type Allocation, type Application, - RefillUnit, } from '@/type' import { Dialog, @@ -88,6 +88,7 @@ const AppInfoCard: React.FC = ({ message, loadMoreAccounts, mutationRequestKyc, + mutationRemovePendingAllocation, } = useApplicationActions(initialApplication, repo, owner) const [buttonText, setButtonText] = useState('') const [modalMessage, setModalMessage] = useState(null) @@ -681,6 +682,20 @@ const AppInfoCard: React.FC = ({ setApiCalling(false) } + const handleRemovePendingAllocation = async (): Promise => { + setApiCalling(true) + const userName = session.data?.user?.githubUsername + if (!userName) return + try { + await mutationRemovePendingAllocation.mutateAsync({ + userName, + }) + } catch (error) { + handleMutationError(error as Error) + } + setApiCalling(false) + } + return ( <> = ({ )} + {application?.Lifecycle?.State === 'ReadyToSign' && ( +
+ +
+ )} {buttonText && (walletConnected || [ diff --git a/src/hooks/useApplicationActions.ts b/src/hooks/useApplicationActions.ts index 49adde7..ef2b1c0 100644 --- a/src/hooks/useApplicationActions.ts +++ b/src/hooks/useApplicationActions.ts @@ -1,21 +1,22 @@ -import { useState } from 'react' -import { - useQueryClient, - useMutation, - type UseMutationResult, -} from 'react-query' +import useWallet from '@/hooks/useWallet' import { - postApplicationTrigger, - postApplicationProposal, + postAdditionalInfoRequest, postApplicationApproval, - postApproveChanges, postApplicationDecline, - postAdditionalInfoRequest, + postApplicationProposal, + postApplicationTrigger, + postApproveChanges, + postRemoveAlloc, postRequestKyc, triggerSSA, } from '@/lib/apiClient' -import useWallet from '@/hooks/useWallet' -import { type RefillUnit, type Application } from '@/type' +import { type Application, type RefillUnit } from '@/type' +import { useState } from 'react' +import { + useMutation, + useQueryClient, + type UseMutationResult, +} from 'react-query' interface ApplicationActions { application: Application @@ -40,6 +41,12 @@ interface ApplicationActions { { userName: string }, unknown > + mutationRemovePendingAllocation: UseMutationResult< + Application | undefined, + unknown, + { userName: string }, + unknown + > mutationDecline: UseMutationResult< Application | undefined, unknown, @@ -230,6 +237,26 @@ const useApplicationActions = ( }, ) + const mutationRemovePendingAllocation = useMutation< + Application | undefined, + unknown, + { userName: string }, + unknown + >( + async ({ userName }) => { + return await postRemoveAlloc(initialApplication.ID, userName, repo, owner) + }, + { + onSuccess: (data) => { + setApiCalling(false) + if (data != null) updateCache(data) + }, + onError: () => { + setApiCalling(false) + }, + }, + ) + /** * Mutation function to handle the triggering of an application. * It makes an API call to trigger the application and updates the cache on success. @@ -489,6 +516,7 @@ const useApplicationActions = ( accounts, loadMoreAccounts, mutationTriggerSSA, + mutationRemovePendingAllocation, } } diff --git a/src/lib/apiClient.ts b/src/lib/apiClient.ts index 18c793d..bd1ac9b 100644 --- a/src/lib/apiClient.ts +++ b/src/lib/apiClient.ts @@ -1,13 +1,13 @@ import { - type LDNActorsResponse, - type Application, - type Allocator, type Allocation, + type Allocator, + type Application, + type LDNActorsResponse, type RefillUnit, } from '@/type' import axios from 'axios' -import { getCurrentDate } from './utils' import { getAccessToken } from './session' +import { getCurrentDate } from './utils' /** * Axios client instance with a predefined base URL for making API requests. @@ -265,6 +265,31 @@ export const postRequestKyc = async ( } } +export const postRemoveAlloc = async ( + id: string, + actor: string, + repo: string, + owner: string, +): Promise => { + try { + const { data } = await apiClient.post( + `verifier/application/remove_pending_allocation`, + { github_username: actor, repo, owner, id }, + { + params: { + github_username: actor, + repo, + owner, + id, + }, + }, + ) + return data + } catch (error) { + console.error(error) + } +} + /** * Triggers a LDN application based on its ID. *