From c30d753a52dc65694c618b303f181288da6fdee1 Mon Sep 17 00:00:00 2001 From: "a.bouhuolia" Date: Sun, 5 Jun 2022 14:32:50 +0200 Subject: [PATCH] chore(MarketOffers): Add submit hooks. --- .../MarketTransactionsTable.tsx | 2 +- .../MarketTransactionsTableCell.tsx | 8 +-- .../haveno/useSetCryptoPaymentAccount.ts | 54 +++++++++++++++ .../renderer/src/hooks/haveno/useSetOffer.ts | 67 +++++++++++++++++++ 4 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 packages/renderer/src/hooks/haveno/useSetCryptoPaymentAccount.ts create mode 100644 packages/renderer/src/hooks/haveno/useSetOffer.ts diff --git a/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTable.tsx b/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTable.tsx index 2830f86..d5e66e0 100644 --- a/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTable.tsx +++ b/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTable.tsx @@ -16,6 +16,7 @@ import { createStyles } from "@mantine/core"; import { createTable } from "@tanstack/react-table"; +import { useIntl } from "react-intl"; import { Table } from "@molecules/Table"; import type { MarketTransaction } from "./_types"; import { @@ -26,7 +27,6 @@ import { MarketTransactionsAccountAgeCell, MarketTransactionsPaymentCell, } from "./MarketTransactionsTableCell"; -import { useIntl } from "react-intl"; import { LangKeys } from "@constants/lang"; const table = createTable().setRowType(); diff --git a/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTableCell.tsx b/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTableCell.tsx index 2e2fe69..4c6adb9 100644 --- a/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTableCell.tsx +++ b/packages/renderer/src/components/molecules/MarketTransactionsTable/MarketTransactionsTableCell.tsx @@ -14,13 +14,13 @@ // limitations under the License. // ============================================================================= +import { useIntl } from "react-intl"; +import { Group } from "@mantine/core"; import { Currency } from "@atoms/Currency"; import { BodyText } from "@atoms/Typography"; -import { Group, Text } from "@mantine/core"; import type { MarketTransaction } from "./_types"; import { MarketTransactionPaymentMethod } from "./_types"; import { ReactComponent as CheckCircle } from "@assets/check-circle.svg"; -import { useIntl } from "react-intl"; import { LangKeys } from "@constants/lang"; export function MarketTransactionsAccountAgeCell({ @@ -29,7 +29,7 @@ export function MarketTransactionsAccountAgeCell({ row?: MarketTransaction; }) { return ( - + 65 Days @@ -42,7 +42,7 @@ export function MarketTransactionsPriceCell({ row?: MarketTransaction; }) { return ( - + { + return client.createCryptoPaymentAccount( + variables.accountName, + variables.assetCode, + variables.address + ); + }, + { + onSuccess: () => { + queryClient.invalidateQueries(QueryKeys.PaymentAccounts); + }, + onError: (err: Error) => { + console.dir(err); + showNotification({ + color: "red", + message: err.message || "", + title: "Something went wrong", + }); + }, + } + ); +} diff --git a/packages/renderer/src/hooks/haveno/useSetOffer.ts b/packages/renderer/src/hooks/haveno/useSetOffer.ts new file mode 100644 index 0000000..bc66ea5 --- /dev/null +++ b/packages/renderer/src/hooks/haveno/useSetOffer.ts @@ -0,0 +1,67 @@ +// ============================================================================= +// Copyright 2022 Haveno +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ============================================================================= + +import { useMutation, useQueryClient } from "react-query"; +import { showNotification } from "@mantine/notifications"; +import { QueryKeys } from "@constants/query-keys"; +import { useHavenoClient } from "./useHavenoClient"; + +interface SetOfferVariables { + direction: string; + amount: bigint; + assetCode: string; + paymentAccountId: string; + buyerSecurityDeposit: number; + price?: number; + marketPriceMargin?: number; + triggerPrice?: number; + minAmount?: bigint; +} + +export function useSetOffer() { + const client = useHavenoClient(); + const queryClient = useQueryClient(); + + return useMutation( + async (variables: SetOfferVariables) => { + return client.postOffer( + variables.direction, + variables.amount, + variables.assetCode, + variables.paymentAccountId, + variables.buyerSecurityDeposit, + variables.price, + variables.marketPriceMargin, + variables.triggerPrice, + variables.minAmount + ); + }, + { + onSuccess: () => { + queryClient.invalidateQueries(QueryKeys.MarketsOffers); + queryClient.invalidateQueries(QueryKeys.MyOffers); + }, + onError: (err: Error) => { + console.dir(err); + showNotification({ + color: "red", + message: err.message || "", + title: "Something went wrong", + }); + }, + } + ); +}