From 54d4a1efe0a55a80ed594f14698da16dfded8c47 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 30 Nov 2021 17:29:33 -0300 Subject: add a taler action from the history page --- .../src/popup/AddNewActionView.stories.tsx | 33 +++++++++++ .../src/popup/AddNewActionView.tsx | 68 ++++++++++++++++++++++ .../src/popup/History.tsx | 57 ++++++++++++------ .../src/popup/TalerActionFound.tsx | 48 +-------------- 4 files changed, 140 insertions(+), 66 deletions(-) create mode 100644 packages/taler-wallet-webextension/src/popup/AddNewActionView.stories.tsx create mode 100644 packages/taler-wallet-webextension/src/popup/AddNewActionView.tsx (limited to 'packages/taler-wallet-webextension/src/popup') diff --git a/packages/taler-wallet-webextension/src/popup/AddNewActionView.stories.tsx b/packages/taler-wallet-webextension/src/popup/AddNewActionView.stories.tsx new file mode 100644 index 000000000..6ee56ef77 --- /dev/null +++ b/packages/taler-wallet-webextension/src/popup/AddNewActionView.stories.tsx @@ -0,0 +1,33 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { createExample } from "../test-utils"; +import { AddNewActionView as TestedComponent } from "./AddNewActionView"; + +export default { + title: "popup/add new action", + component: TestedComponent, + argTypes: { + setDeviceName: () => Promise.resolve(), + }, +}; + +export const Initial = createExample(TestedComponent, {}); diff --git a/packages/taler-wallet-webextension/src/popup/AddNewActionView.tsx b/packages/taler-wallet-webextension/src/popup/AddNewActionView.tsx new file mode 100644 index 000000000..876b1a83c --- /dev/null +++ b/packages/taler-wallet-webextension/src/popup/AddNewActionView.tsx @@ -0,0 +1,68 @@ +import { classifyTalerUri, TalerUriType } from "@gnu-taler/taler-util"; +import { Fragment, h, VNode } from "preact"; +import { useState } from "preact/hooks"; +import { + Button, + ButtonSuccess, + InputWithLabel, +} from "../components/styled/index"; +import { actionForTalerUri } from "../utils/index"; + +export interface Props { + onCancel: () => void; +} + +function buttonLabelByTalerType(type: TalerUriType): string { + switch (type) { + case TalerUriType.TalerNotifyReserve: + return "Open reserve page"; + case TalerUriType.TalerPay: + return "Open pay page"; + case TalerUriType.TalerRefund: + return "Open refund page"; + case TalerUriType.TalerTip: + return "Open tip page"; + case TalerUriType.TalerWithdraw: + return "Open withdraw page"; + } + return ""; +} + +export function AddNewActionView({ onCancel }: Props): VNode { + const [url, setUrl] = useState(""); + const uriType = classifyTalerUri(url); + + return ( + +
+ + +
+ setUrl(e.currentTarget.value)} + /> +
+
+
+
+ + {uriType !== TalerUriType.Unknown && ( + { + // eslint-disable-next-line no-undef + chrome.tabs.create({ url: actionForTalerUri(uriType, url) }); + }} + > + {buttonLabelByTalerType(uriType)} + + )} +
+
+ ); +} diff --git a/packages/taler-wallet-webextension/src/popup/History.tsx b/packages/taler-wallet-webextension/src/popup/History.tsx index b23b4781f..f897299d8 100644 --- a/packages/taler-wallet-webextension/src/popup/History.tsx +++ b/packages/taler-wallet-webextension/src/popup/History.tsx @@ -23,10 +23,11 @@ import { } from "@gnu-taler/taler-util"; import { Fragment, h, VNode } from "preact"; import { useEffect, useState } from "preact/hooks"; -import { PopupBox } from "../components/styled"; +import { ButtonPrimary } from "../components/styled/index"; import { TransactionItem } from "../components/TransactionItem"; import { useAsyncAsHook } from "../hooks/useAsyncAsHook"; import * as wxApi from "../wxApi"; +import { AddNewActionView } from "./AddNewActionView"; export function HistoryPage(): VNode { const [transactions, setTransactions] = useState< @@ -45,6 +46,12 @@ export function HistoryPage(): VNode { fetchData(); }, []); + const [addingAction, setAddingAction] = useState(false); + + if (addingAction) { + return setAddingAction(false)} />; + } + if (!transactions) { return
Loading ...
; } @@ -53,6 +60,7 @@ export function HistoryPage(): VNode { setAddingAction(true)} /> ); } @@ -65,31 +73,42 @@ function amountToString(c: AmountString): string { export function HistoryView({ list, balances, + onAddNewAction, }: { list: Transaction[]; balances: Balance[]; + onAddNewAction: () => void; }): VNode { const multiCurrency = balances.length > 1; return ( - {balances.length > 0 && ( -
- {multiCurrency ? ( -
- Balance:{" "} -
    - {balances.map((b, i) => ( -
  • {b.available}
  • - ))} -
-
- ) : ( -
- Balance: {amountToString(balances[0].available)} -
- )} -
- )} +
+ {balances.length > 0 ? ( + + {multiCurrency ? ( +
+ Balance:{" "} +
    + {balances.map((b, i) => ( +
  • {b.available}
  • + ))} +
+
+ ) : ( +
+ Balance: {amountToString(balances[0].available)} +
+ )} +
+ ) : ( +
+ )} +
+ + + + +
+
{list.length === 0 ? (

diff --git a/packages/taler-wallet-webextension/src/popup/TalerActionFound.tsx b/packages/taler-wallet-webextension/src/popup/TalerActionFound.tsx index b2220e37b..40e9111fb 100644 --- a/packages/taler-wallet-webextension/src/popup/TalerActionFound.tsx +++ b/packages/taler-wallet-webextension/src/popup/TalerActionFound.tsx @@ -22,6 +22,7 @@ import { classifyTalerUri, TalerUriType } from "@gnu-taler/taler-util"; import { Fragment, h } from "preact"; import { ButtonPrimary, ButtonSuccess } from "../components/styled/index"; +import { actionForTalerUri } from "../utils/index"; export interface Props { url: string; @@ -108,50 +109,3 @@ export function TalerActionFound({ url, onDismiss }: Props) { ); } - -function actionForTalerUri( - uriType: TalerUriType, - talerUri: string, -): string | undefined { - switch (uriType) { - case TalerUriType.TalerWithdraw: - return makeExtensionUrlWithParams("static/wallet.html#/withdraw", { - talerWithdrawUri: talerUri, - }); - case TalerUriType.TalerPay: - return makeExtensionUrlWithParams("static/wallet.html#/pay", { - talerPayUri: talerUri, - }); - case TalerUriType.TalerTip: - return makeExtensionUrlWithParams("static/wallet.html#/tip", { - talerTipUri: talerUri, - }); - case TalerUriType.TalerRefund: - return makeExtensionUrlWithParams("static/wallet.html#/refund", { - talerRefundUri: talerUri, - }); - case TalerUriType.TalerNotifyReserve: - // FIXME: implement - break; - default: - console.warn( - "Response with HTTP 402 has Taler header, but header value is not a taler:// URI.", - ); - break; - } - return undefined; -} - -function makeExtensionUrlWithParams( - url: string, - params?: { [name: string]: string | undefined }, -): string { - const innerUrl = new URL(chrome.extension.getURL("/" + url)); - if (params) { - const hParams = Object.keys(params) - .map((k) => `${k}=${params[k]}`) - .join("&"); - innerUrl.hash = innerUrl.hash + "?" + hParams; - } - return innerUrl.href; -} -- cgit v1.2.3