From eb84d5747aac0de781d64fb9cdbf2da13006d85e Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sun, 13 Nov 2016 10:17:39 +0100 Subject: fix small react issues --- lib/wallet/db.ts | 4 ++-- lib/wallet/query.ts | 16 ++++++++++++++++ lib/wallet/renderHtml.tsx | 10 +++++----- lib/wallet/wallet.ts | 37 +++++++++++++++++++++++++++++++++++++ lib/wallet/wxMessaging.ts | 11 +++++++++++ 5 files changed, 71 insertions(+), 7 deletions(-) (limited to 'lib/wallet') diff --git a/lib/wallet/db.ts b/lib/wallet/db.ts index d4ae1cd48..9cffc164c 100644 --- a/lib/wallet/db.ts +++ b/lib/wallet/db.ts @@ -26,7 +26,7 @@ import {IExchangeInfo} from "./types"; */ const DB_NAME = "taler"; -const DB_VERSION = 10; +const DB_VERSION = 11; import {Stores} from "./wallet"; import {Store, Index} from "./query"; @@ -114,4 +114,4 @@ export function exportDb(db: IDBDatabase): Promise { export function deleteDb() { indexedDB.deleteDatabase(DB_NAME); -} \ No newline at end of file +} diff --git a/lib/wallet/query.ts b/lib/wallet/query.ts index c172bbeb7..08e270ea6 100644 --- a/lib/wallet/query.ts +++ b/lib/wallet/query.ts @@ -423,6 +423,22 @@ export class QueryRoot implements PromiseLike { } + putWithResult(store: Store, val: T): Promise { + const {resolve, promise} = openPromise(); + let doPutWithResult = (tx: IDBTransaction) => { + let req = tx.objectStore(store.name).put(val); + req.onsuccess = () => { + resolve(req.result); + } + this.scheduleFinish(); + }; + this.addWork(doPutWithResult, store.name, true); + return Promise.resolve() + .then(() => this.finish()) + .then(() => promise); + } + + mutate(store: Store, key: any, f: (v: T) => T): QueryRoot { let doPut = (tx: IDBTransaction) => { let reqGet = tx.objectStore(store.name).get(key); diff --git a/lib/wallet/renderHtml.tsx b/lib/wallet/renderHtml.tsx index 6afe32b74..940d5c425 100644 --- a/lib/wallet/renderHtml.tsx +++ b/lib/wallet/renderHtml.tsx @@ -34,15 +34,15 @@ export function renderContract(contract: Contract): JSX.Element { return (
-

{ - i18n.parts`${merchantName} - wants to enter a contract over ${amount} - with you.`} +

+ The merchant {merchantName} + wants to enter a contract over {amount}{" "} + with you.

{i18n`You are about to purchase:`}

    {contract.products.map( - (p: any) => (
  • {`${p.description}: ${prettyAmount(p.price)}`}
  • )) + (p: any, i: number) => (
  • {`${p.description}: ${prettyAmount(p.price)}`}
  • )) }
diff --git a/lib/wallet/wallet.ts b/lib/wallet/wallet.ts index 47d42132d..9fb6e5a27 100644 --- a/lib/wallet/wallet.ts +++ b/lib/wallet/wallet.ts @@ -142,6 +142,15 @@ export class Offer { @Checkable.String H_contract: string; + @Checkable.Number + offer_time: number; + + /** + * Serial ID when the offer is stored in the wallet DB. + */ + @Checkable.Optional(Checkable.Number) + id?: number; + static checked: (obj: any) => Offer; } @@ -297,6 +306,15 @@ export namespace Stores { timestampIndex = new Index(this, "timestamp", "timestamp"); } + class OffersStore extends Store { + constructor() { + super("offers", { + keyPath: "id", + autoIncrement: true + }); + } + } + class TransactionsStore extends Store { constructor() { super("transactions", {keyPath: "contractHash"}); @@ -314,6 +332,7 @@ export namespace Stores { export let coins: CoinsStore = new CoinsStore(); export let refresh: Store = new Store("refresh", {keyPath: "meltCoinPub"}); export let history: HistoryStore = new HistoryStore(); + export let offers: OffersStore = new OffersStore(); export let precoins: Store = new Store("precoins", {keyPath: "coinPub"}); } @@ -585,6 +604,18 @@ export class Wallet { } + async saveOffer(offer: Offer): Promise { + console.log(`saving offer in wallet.ts`); + let id = await this.q().putWithResult(Stores.offers, offer); + this.notifier.notify(); + console.log(`saved offer with id ${id}`); + if (typeof id !== "number") { + throw Error("db schema wrong"); + } + return id; + } + + /** * Add a contract to the wallet and sign coins, * but do not send them yet. @@ -1525,6 +1556,12 @@ export class Wallet { return {history}; } + + async getOffer(offerId: number): Promise { + let offer = await this.q() .get(Stores.offers, offerId); + return offer; + } + async getExchanges(): Promise { return this.q() .iter(Stores.exchanges) diff --git a/lib/wallet/wxMessaging.ts b/lib/wallet/wxMessaging.ts index 1c3876772..07f16f24f 100644 --- a/lib/wallet/wxMessaging.ts +++ b/lib/wallet/wxMessaging.ts @@ -168,6 +168,14 @@ function makeHandlers(db: IDBDatabase, } return wallet.putHistory(detail.historyEntry); }, + ["save-offer"]: function (detail: any) { + let offer = detail.offer; + if (!offer) { + return Promise.resolve({ error: "offer missing" }); + } + console.log("handling safe-offer"); + return wallet.saveOffer(offer); + }, ["reserve-creation-info"]: function (detail, sender) { if (!detail.baseUrl || typeof detail.baseUrl !== "string") { return Promise.resolve({ error: "bad url" }); @@ -183,6 +191,9 @@ function makeHandlers(db: IDBDatabase, // TODO: limit history length return wallet.getHistory(); }, + ["get-offer"]: function (detail, sender) { + return wallet.getOffer(detail.offerId); + }, ["get-exchanges"]: function (detail, sender) { return wallet.getExchanges(); }, -- cgit v1.2.3