/* This file is part of GNU Taler (C) 2022 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 */ import * as utils from "@gnu-taler/taler-util"; import { AbsoluteTime } from "@gnu-taler/taler-util"; import { ProviderInfo, ProviderPaymentStatus, ProviderPaymentType, WalletApiOperation, } from "@gnu-taler/taler-wallet-core"; import { Fragment, h, VNode } from "preact"; import { ErrorMessage } from "../components/ErrorMessage.js"; import { Loading } from "../components/Loading.js"; import { LoadingError } from "../components/LoadingError.js"; import { PaymentStatus, SmallLightText } from "../components/styled/index.js"; import { Time } from "../components/Time.js"; import { useTranslationContext } from "../context/translation.js"; import { useAsyncAsHook } from "../hooks/useAsyncAsHook.js"; import { Button } from "../mui/Button.js"; import { wxApi } from "../wxApi.js"; interface Props { pid: string; onBack: () => Promise; } export function ProviderDetailPage({ pid: providerURL, onBack }: Props): VNode { const { i18n } = useTranslationContext(); async function getProviderInfo(): Promise { //create a first list of backup info by currency const status = await wxApi.wallet.call( WalletApiOperation.GetBackupInfo, {}, ); const providers = status.providers.filter( (p) => p.syncProviderBaseUrl === providerURL, ); return providers.length ? providers[0] : null; } const state = useAsyncAsHook(getProviderInfo); if (!state) { return ; } if (state.hasError) { return ( There was an error loading the provider detail for " {providerURL}" } error={state} /> ); } return ( wxApi.wallet .call(WalletApiOperation.RunBackupCycle, { providers: [providerURL], }) .then() } onDelete={() => wxApi.wallet .call(WalletApiOperation.RemoveBackupProvider, { provider: providerURL, }) .then(onBack) } onBack={onBack} onExtend={async () => { null; }} /> ); } export interface ViewProps { url: string; info: ProviderInfo | null; onDelete: () => Promise; onSync: () => Promise; onBack: () => Promise; onExtend: () => Promise; } export function ProviderView({ info, url, onDelete, onSync, onBack, onExtend, }: ViewProps): VNode { const { i18n } = useTranslationContext(); if (info === null) { return (

There is not known provider with url "{url}".

); } const lb = info.lastSuccessfulBackupTimestamp ? AbsoluteTime.fromTimestamp(info.lastSuccessfulBackupTimestamp) : undefined; const isPaid = info.paymentStatus.type === ProviderPaymentType.Paid || info.paymentStatus.type === ProviderPaymentType.TermsChanged; return (

{info.name}{" "} {info.syncProviderBaseUrl}

{isPaid ? "Paid" : "Unpaid"}

Last backup: {" "}

{info.terms && (

Provider fee: {" "} {info.terms && info.terms.annualFee}{" "} per year

)}

{descriptionByStatus(info.paymentStatus, i18n)}

{info.paymentStatus.type === ProviderPaymentType.TermsChanged && (

terms has changed, extending the service will imply accepting the new terms of service

  old -> new
fee {info.paymentStatus.oldTerms.annualFee} -> {info.paymentStatus.newTerms.annualFee}
storage {info.paymentStatus.oldTerms.storageLimitInMegabytes} -> {info.paymentStatus.newTerms.storageLimitInMegabytes}
)}
); } function Error({ info }: { info: ProviderInfo }): VNode { const { i18n } = useTranslationContext(); if (info.lastError) { return ( This provider has reported an error } description={info.lastError.hint} /> ); } if (info.backupProblem) { switch (info.backupProblem.type) { case "backup-conflicting-device": return ( There is conflict with another backup from{" "} {info.backupProblem.otherDeviceId} } /> ); case "backup-unreadable": return ( Backup is not readable} /> ); default: return ( Unknown backup problem: {JSON.stringify(info.backupProblem)} } /> ); } } return ; } function descriptionByStatus( status: ProviderPaymentStatus, i18n: typeof utils.i18n, ): VNode { switch (status.type) { case ProviderPaymentType.Paid: case ProviderPaymentType.TermsChanged: if (status.paidUntil.t_ms === "never") { return ( service paid ); } return ( Backup valid until: {" "} ); case ProviderPaymentType.Unpaid: case ProviderPaymentType.InsufficientBalance: case ProviderPaymentType.Pending: return ; } }