From bba3282def034152ccd0d895b4b88ae3b8c771bd Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 1 Dec 2017 15:33:30 +0000 Subject: [PATCH] Add a basic journal list view. --- src/App.tsx | 14 +++-------- src/JournalList.tsx | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/api/EteSync.tsx | 6 ++++- 3 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 src/JournalList.tsx diff --git a/src/App.tsx b/src/App.tsx index 30d7516..4e9bede 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,24 +2,16 @@ import * as React from 'react'; import { Router } from 'react-router'; import './App.css'; +import { JournalList } from './JournalList'; + import createBrowserHistory from 'history/createBrowserHistory'; const customHistory = createBrowserHistory(); -const logo = require('./logo.svg'); - class App extends React.Component { render() { return ( -
-
- logo -

Welcome to React

-
-

- To get started, edit src/App.tsx and save to reload. -

-
+
); } diff --git a/src/JournalList.tsx b/src/JournalList.tsx new file mode 100644 index 0000000..f34b324 --- /dev/null +++ b/src/JournalList.tsx @@ -0,0 +1,57 @@ +import * as React from 'react'; + +import * as EteSync from './api/EteSync'; + +const SERVICE_API = 'http://localhost:8000'; +const USER = 'test@localhost'; +const PASSWORD = 'SomePassword'; +const derived = EteSync.deriveKey(USER, PASSWORD); + +export class JournalList extends React.Component { + state: { + journals: Array, + }; + + constructor(props: any) { + super(props); + this.state = { + journals: [], + }; + } + + componentDidMount() { + let authenticator = new EteSync.Authenticator(SERVICE_API); + + authenticator.getAuthToken(USER, PASSWORD).then((authToken) => { + const credentials = new EteSync.Credentials(USER, authToken); + + let journalManager = new EteSync.JournalManager(credentials, SERVICE_API); + journalManager.list().then((journals) => { + journals = journals.filter((x) => ( + // Skip shared journals for now. + !x.key + )); + this.setState({ journals }); + }); + }); + } + + render() { + const journals = this.state.journals.map((journal, idx) => { + let cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version); + let info = journal.getInfo(cryptoManager); + return (
  • {info.displayName}: {info.type} ({journal.uid})
  • ); + }); + + return ( +
    +
    +

    Welcome to React

    +
    +
      + {journals} +
    +
    + ); + } +} diff --git a/src/api/EteSync.tsx b/src/api/EteSync.tsx index b52ea8f..001ba0d 100644 --- a/src/api/EteSync.tsx +++ b/src/api/EteSync.tsx @@ -7,7 +7,7 @@ import * as fetch from 'isomorphic-fetch'; import { byte, base64, stringToByteArray } from './Helpers'; import { CryptoManager, HMAC_SIZE_BYTES } from './Crypto'; -export { CryptoManager } from './Crypto'; +export { CryptoManager, deriveKey } from './Crypto'; class ExtendableError extends Error { constructor(message: any) { @@ -121,6 +121,10 @@ export class Journal extends BaseJournal { this._json.version = version; } + get key(): base64 | undefined { + return this._json.key; + } + get version(): number { return this._json.version; }