Skip to content

Commit

Permalink
FEAT: Invalidate data on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
rogaldh committed Mar 28, 2024
1 parent 113c44e commit be57c6a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/entities/token/use-mint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function useMint(
const { connection } = useConnection()

const { data, error } = useQuery({
enabled: Boolean(address),
placeholderData: opts?.placeholderData,
queryFn: async (): Promise<spl.Mint | undefined> => {
if (!address) return undefined
Expand All @@ -28,7 +29,6 @@ export function useMint(
return mint
}
},
enabled: Boolean(address),
queryKey: ["useMint", address],
refetchInterval: opts?.refetchInterval,
refetchIntervalInBackground: opts?.refetchIntervalInBackground,
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/entities/token/use-token-amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ function useTokenAmount<S extends State>(state: S, action: any): State {
case "changeDestination": {
return { ...state, destination: action.payload.destination }
}
case "clear": {
return {}
}
default:
return state
}
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/entities/token/use-token-balance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export function useTokenBalance(
const publicKey = wallet?.adapter.publicKey

const { data: balance, error } = useQuery({
enabled: Boolean(address && publicKey),
placeholderData: opts?.placeholderData,
queryFn: async (): Promise<string> => {
// no token selected yet
Expand Down Expand Up @@ -63,8 +64,8 @@ export function useTokenBalance(
}
return "0"
},
queryKey: ["useTokenBalance", address, publicKey],
refetchInterval: opts?.refetchInterval ?? 12000,
queryKey: ["useTokenBalance", String(address), String(publicKey)],
refetchInterval: opts?.refetchInterval ?? 6000,
refetchIntervalInBackground: opts?.refetchIntervalInBackground,
})

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/entities/use-token-upgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export function useTokenUpgrade() {

const mutation = useMutation({
mutationFn,
onSuccess: (_data, variables) => {
onSuccess: () => {
client.invalidateQueries({
queryKey: ["useTokenBalance", variables.originalAddress],
predicate: (query) => query.queryKey[0] === "useTokenBalance",
})
},
})
Expand Down
6 changes: 6 additions & 0 deletions packages/ui/src/widgets/token-upgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function TokenUpgradeBase({
},
{
onSuccess: (signature) => {
setAction({ type: "clear" })
onUpgradeEnd?.({ signature })
},
onError: (error: Error) => {
Expand All @@ -89,6 +90,7 @@ export function TokenUpgradeBase({
onUpgradeEnd,
onUpgradeError,
onUpgradeStart,
setAction,
tokenAddress,
tokenExtAddress,
tokenUpgradeProgramId,
Expand All @@ -110,6 +112,9 @@ export function TokenUpgradeBase({
className,
"flex min-w-80 flex-col overflow-hidden rounded-lg bg-white shadow",
)}
onSubmit={(e) => {
e.preventDefault()
}}
>
<div className="px-4 py-4 sm:p-6">
<Container>
Expand All @@ -123,6 +128,7 @@ export function TokenUpgradeBase({
placeholder={ph}
step={step}
symbol={symbol}
value={amount}
/>
</Form.Field>
<Form.Field className="pb-4 pt-3.5" name="destination">
Expand Down
9 changes: 8 additions & 1 deletion packages/ui/src/widgets/token-upgrade/amount.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useRef, useState } from "react"
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"
import * as Form from "@radix-ui/react-form"
import { cva, VariantProps } from "class-variance-authority"
import clsx from "clsx"
Expand Down Expand Up @@ -44,6 +44,7 @@ export default function Amount({
placeholder = "0",
step = 1,
symbol,
value,
...props
}: AmountProps) {
let variants = {}
Expand All @@ -70,6 +71,12 @@ export default function Amount({
[onAmountMaxChange, inpRef],
)

useEffect(() => {
if (inpRef.current && inpRef.current.value !== String(value)) {
inpRef.current.value = String(value || "")
}
}, [value])

if (disabled) variants = { variant: "disabled" }
if (hasBalance) variants = { variant: "active" }

Expand Down

0 comments on commit be57c6a

Please sign in to comment.