-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* State refactor: assets * Cleanup * More cleanup * Fix trustline button alignment
- Loading branch information
Showing
14 changed files
with
227 additions
and
283 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { ApiAsset, AppError } from "types"; | ||
|
||
type Asset = { | ||
assetCode: string; | ||
assetIssuer: string; | ||
}; | ||
|
||
export const useAssetsAdd = ({ | ||
onSuccess, | ||
}: { | ||
onSuccess: (addedAsset: ApiAsset) => void; | ||
}) => { | ||
const mutation = useMutation({ | ||
mutationFn: ({ assetCode, assetIssuer }: Asset) => { | ||
return fetchApi(`${API_URL}/assets`, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
code: assetCode, | ||
issuer: assetIssuer, | ||
}), | ||
}); | ||
}, | ||
cacheTime: 0, | ||
onSuccess, | ||
}); | ||
|
||
return { | ||
...mutation, | ||
error: mutation.error as AppError, | ||
data: mutation.data as ApiAsset, | ||
mutateAsync: async ({ assetCode, assetIssuer }: Asset) => { | ||
try { | ||
await mutation.mutateAsync({ assetCode, assetIssuer }); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
}, | ||
}; | ||
}; |
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,20 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { ApiAsset, AppError } from "types"; | ||
|
||
export const useAssetsByWallet = (walletId: string) => { | ||
const query = useQuery<ApiAsset[], AppError>({ | ||
queryKey: ["assets", "wallet", walletId], | ||
queryFn: async () => { | ||
if (!walletId) { | ||
return; | ||
} | ||
|
||
return await fetchApi(`${API_URL}/assets?wallet=${walletId}`); | ||
}, | ||
enabled: Boolean(walletId), | ||
}); | ||
|
||
return query; | ||
}; |
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,33 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { ApiAsset, AppError } from "types"; | ||
|
||
export const useAssetsDelete = ({ | ||
onSuccess, | ||
}: { | ||
onSuccess: (deletedAsset: ApiAsset) => void; | ||
}) => { | ||
const mutation = useMutation({ | ||
mutationFn: (assetId: string) => { | ||
return fetchApi(`${API_URL}/assets/${assetId}`, { | ||
method: "DELETE", | ||
}); | ||
}, | ||
cacheTime: 0, | ||
onSuccess, | ||
}); | ||
|
||
return { | ||
...mutation, | ||
error: mutation.error as AppError, | ||
data: mutation.data as ApiAsset, | ||
mutateAsync: async (assetId: string) => { | ||
try { | ||
await mutation.mutateAsync(assetId); | ||
} catch (e) { | ||
// do nothing | ||
} | ||
}, | ||
}; | ||
}; |
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,42 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { API_URL } from "constants/settings"; | ||
import { fetchApi } from "helpers/fetchApi"; | ||
import { ApiAsset, AppError, StellarAccountBalance } from "types"; | ||
|
||
type Trustline = { | ||
id: string | null; | ||
code: string; | ||
issuer: string; | ||
balance: string; | ||
isNative: boolean; | ||
}; | ||
|
||
export const useBalanceTrustline = ( | ||
balances?: StellarAccountBalance[] | undefined, | ||
) => { | ||
const query = useQuery<Trustline[] | undefined, AppError>({ | ||
queryKey: ["trustlines", { balances }], | ||
queryFn: async () => { | ||
const response = await fetchApi(`${API_URL}/assets`); | ||
|
||
return balances?.map((b) => { | ||
const id = | ||
response?.find( | ||
(a: ApiAsset) => | ||
a.code === b?.assetCode && a.issuer === b?.assetIssuer, | ||
)?.id || null; | ||
|
||
return { | ||
id, | ||
code: b?.assetCode || "XLM", | ||
issuer: b?.assetIssuer || "native", | ||
balance: b.balance, | ||
isNative: Boolean(!b.assetCode && !b.assetIssuer), | ||
}; | ||
}); | ||
}, | ||
enabled: Boolean(balances), | ||
}); | ||
|
||
return query; | ||
}; |
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.