Skip to content

Commit

Permalink
fix: Refetch submission data when submitting submission (#4469)
Browse files Browse the repository at this point in the history
* fix: Refetch submission data when submitting submission

* fix: Use mutation for createSubmission and invalidate cache
  • Loading branch information
usame-algan authored Nov 4, 2024
1 parent 8397b23 commit fd29be1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
24 changes: 17 additions & 7 deletions src/features/targetedOutreach/components/OutreachPopup/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useCreateSubmissionMutation, useGetSubmissionQuery } from '@/store/api/gateway'
import { skipToken } from '@reduxjs/toolkit/query'
import { useEffect, type ReactElement } from 'react'
import { Avatar, Box, Button, Chip, IconButton, Link, Paper, Stack, ThemeProvider, Typography } from '@mui/material'
import { Close } from '@mui/icons-material'
Expand All @@ -14,8 +16,6 @@ import SafeThemeProvider from '@/components/theme/SafeThemeProvider'
import useChainId from '@/hooks/useChainId'
import useSafeAddress from '@/hooks/useSafeAddress'
import useWallet from '@/hooks/wallets/useWallet'
import { createSubmission } from '@safe-global/safe-client-gateway-sdk'
import useSubmission from '@/features/targetedOutreach/hooks/useSubmission'

const OutreachPopup = (): ReactElement | null => {
const dispatch = useAppDispatch()
Expand All @@ -24,7 +24,17 @@ const OutreachPopup = (): ReactElement | null => {
const currentChainId = useChainId()
const safeAddress = useSafeAddress()
const wallet = useWallet()
const submission = useSubmission()
const [createSubmission] = useCreateSubmissionMutation()
const { data: submission } = useGetSubmissionQuery(
!wallet || !safeAddress
? skipToken
: {
outreachId: ACTIVE_OUTREACH.id,
chainId: currentChainId,
safeAddress,
signerAddress: wallet?.address,
},
)

const [askAgainLaterTimestamp, setAskAgainLaterTimestamp] = useSessionStorage<number>(OUTREACH_SS_KEY)

Expand Down Expand Up @@ -54,10 +64,10 @@ const OutreachPopup = (): ReactElement | null => {
const handleOpenSurvey = async () => {
if (wallet) {
await createSubmission({
params: {
path: { outreachId: ACTIVE_OUTREACH.id, chainId: currentChainId, safeAddress, signerAddress: wallet.address },
},
body: { completed: true },
outreachId: ACTIVE_OUTREACH.id,
chainId: currentChainId,
safeAddress,
signerAddress: wallet.address,
})
}
dispatch(closeOutreachBanner())
Expand Down
27 changes: 0 additions & 27 deletions src/features/targetedOutreach/hooks/useSubmission.tsx

This file was deleted.

21 changes: 20 additions & 1 deletion src/store/api/gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { asError } from '@/services/exceptions/utils'
import { getDelegates } from '@safe-global/safe-gateway-typescript-sdk'
import type { DelegateResponse } from '@safe-global/safe-gateway-typescript-sdk/dist/types/delegates'
import { safeOverviewEndpoints } from './safeOverviews'
import { getSubmission } from '@safe-global/safe-client-gateway-sdk'
import { createSubmission, getSubmission } from '@safe-global/safe-client-gateway-sdk'

async function buildQueryFn<T>(fn: () => Promise<T>) {
try {
Expand All @@ -18,6 +18,7 @@ async function buildQueryFn<T>(fn: () => Promise<T>) {
export const gatewayApi = createApi({
reducerPath: 'gatewayApi',
baseQuery: fakeBaseQuery<Error>(),
tagTypes: ['Submissions'],
endpoints: (builder) => ({
getTransactionDetails: builder.query<TransactionDetails, { chainId: string; txId: string }>({
queryFn({ chainId, txId }) {
Expand All @@ -43,6 +44,23 @@ export const gatewayApi = createApi({
getSubmission({ params: { path: { outreachId, chainId, safeAddress, signerAddress } } }),
)
},
providesTags: ['Submissions'],
}),
createSubmission: builder.mutation<
createSubmission,
{ outreachId: number; chainId: string; safeAddress: string; signerAddress: string }
>({
queryFn({ outreachId, chainId, safeAddress, signerAddress }) {
return buildQueryFn(() =>
createSubmission({
params: {
path: { outreachId, chainId, safeAddress, signerAddress },
},
body: { completed: true },
}),
)
},
invalidatesTags: ['Submissions'],
}),
...safeOverviewEndpoints(builder),
}),
Expand All @@ -54,6 +72,7 @@ export const {
useLazyGetTransactionDetailsQuery,
useGetDelegatesQuery,
useGetSubmissionQuery,
useCreateSubmissionMutation,
useGetSafeOverviewQuery,
useGetMultipleSafeOverviewsQuery,
} = gatewayApi
2 changes: 1 addition & 1 deletion src/store/api/gateway/safeOverviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type MultiOverviewQueryParams = {
safes: SafeItem[]
}

export const safeOverviewEndpoints = (builder: EndpointBuilder<any, never, 'gatewayApi'>) => ({
export const safeOverviewEndpoints = (builder: EndpointBuilder<any, 'Submissions', 'gatewayApi'>) => ({
getSafeOverview: builder.query<SafeOverview | null, { safeAddress: string; walletAddress?: string; chainId: string }>(
{
async queryFn({ safeAddress, walletAddress, chainId }, { getState }) {
Expand Down

0 comments on commit fd29be1

Please sign in to comment.