summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/components/TransactionItem.tsx
blob: 8ddfd0e321c300ba6ea3d2e14bf388bd435a5de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { AmountString, Timestamp, Transaction, TransactionType } from '@gnu-taler/taler-util';
import { format, formatDistance } from 'date-fns';
import { h } from 'preact';
import imageBank from '../../static/img/ri-bank-line.svg';
import imageHandHeart from '../../static/img/ri-hand-heart-line.svg';
import imageRefresh from '../../static/img/ri-refresh-line.svg';
import imageRefund from '../../static/img/ri-refund-2-line.svg';
import imageShoppingCart from '../../static/img/ri-shopping-cart-line.svg';
import { Pages } from "../NavigationBar";
import { Column, ExtraLargeText, HistoryRow, SmallLightText, LargeText, LightText } from './styled/index';

export function TransactionItem(props: { tx: Transaction, multiCurrency: boolean }): JSX.Element {
  const tx = props.tx;
  switch (tx.type) {
    case TransactionType.Withdrawal:
      return (
        <TransactionLayout
          id={tx.transactionId}
          amount={tx.amountEffective}
          debitCreditIndicator={"credit"}
          title={new URL(tx.exchangeBaseUrl).hostname}
          timestamp={tx.timestamp}
          iconPath={imageBank}
          pending={tx.pending}
          multiCurrency={props.multiCurrency}
        ></TransactionLayout>
      );
    case TransactionType.Payment:
      return (
        <TransactionLayout
          id={tx.transactionId}
          amount={tx.amountEffective}
          debitCreditIndicator={"debit"}
          title={tx.info.merchant.name}
          timestamp={tx.timestamp}
          iconPath={imageShoppingCart}
          pending={tx.pending}
          multiCurrency={props.multiCurrency}
        ></TransactionLayout>
      );
    case TransactionType.Refund:
      return (
        <TransactionLayout
          id={tx.transactionId}
          amount={tx.amountEffective}
          debitCreditIndicator={"credit"}
          title={tx.info.merchant.name}
          timestamp={tx.timestamp}
          iconPath={imageRefund}
          pending={tx.pending}
          multiCurrency={props.multiCurrency}
        ></TransactionLayout>
      );
    case TransactionType.Tip:
      return (
        <TransactionLayout
          id={tx.transactionId}
          amount={tx.amountEffective}
          debitCreditIndicator={"credit"}
          title={new URL(tx.merchantBaseUrl).hostname}
          timestamp={tx.timestamp}
          iconPath={imageHandHeart}
          pending={tx.pending}
          multiCurrency={props.multiCurrency}
        ></TransactionLayout>
      );
    case TransactionType.Refresh:
      return (
        <TransactionLayout
          id={tx.transactionId}
          amount={tx.amountEffective}
          debitCreditIndicator={"credit"}
          title={new URL(tx.exchangeBaseUrl).hostname}
          timestamp={tx.timestamp}
          iconPath={imageRefresh}
          pending={tx.pending}
          multiCurrency={props.multiCurrency}
        ></TransactionLayout>
      );
    case TransactionType.Deposit:
      return (
        <TransactionLayout
          id={tx.transactionId}
          amount={tx.amountEffective}
          debitCreditIndicator={"debit"}
          title={tx.targetPaytoUri}
          timestamp={tx.timestamp}
          iconPath={imageRefresh}
          pending={tx.pending}
          multiCurrency={props.multiCurrency}
        ></TransactionLayout>
      );
  }
}

function TransactionLayout(props: TransactionLayoutProps): JSX.Element {
  const date = new Date(props.timestamp.t_ms);
  const dateStr = format(date, 'dd MMM, hh:mm')

  return (
    <HistoryRow href={Pages.transaction.replace(':tid', props.id)}>
      <img src={props.iconPath} />
      <Column>
        <LargeText>
          <span>{props.title}</span>
        </LargeText>
        {props.pending &&
          <LightText style={{marginTop: 5, marginBottom: 5}}>Waiting for confirmation</LightText>
        }
        <SmallLightText>{dateStr}</SmallLightText>
      </Column>
      <TransactionAmount
        pending={props.pending}
        amount={props.amount}
        multiCurrency={props.multiCurrency}
        debitCreditIndicator={props.debitCreditIndicator}
      />
    </HistoryRow>
  );
}

interface TransactionLayoutProps {
  debitCreditIndicator: "debit" | "credit" | "unknown";
  amount: AmountString | "unknown";
  timestamp: Timestamp;
  title: string;
  id: string;
  iconPath: string;
  pending: boolean;
  multiCurrency: boolean;
}

interface TransactionAmountProps {
  debitCreditIndicator: "debit" | "credit" | "unknown";
  amount: AmountString | "unknown";
  pending: boolean;
  multiCurrency: boolean;
}

function TransactionAmount(props: TransactionAmountProps): JSX.Element {
  const [currency, amount] = props.amount.split(":");
  let sign: string;
  switch (props.debitCreditIndicator) {
    case "credit":
      sign = "+";
      break;
    case "debit":
      sign = "-";
      break;
    case "unknown":
      sign = "";
  }
  return (
    <Column style={{
      textAlign: 'center',
      color:
        props.pending ? "gray" :
          (sign === '+' ? 'darkgreen' :
            (sign === '-' ? 'darkred' :
              undefined))
    }}>
      <ExtraLargeText>
        {sign}
        {amount}
      </ExtraLargeText>
      {props.multiCurrency && <div>{currency}</div>}
      {props.pending && <div>PENDING</div>}
    </Column>
  );
}