From 7dd8248aabfdcdbab12c15823cb3d0db3e2a5b90 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 1 Dec 2017 16:18:11 +0000 Subject: [PATCH] Added a hacky journal view. A lot of code duplication. --- src/App.tsx | 4 +-- src/JournalList.tsx | 23 +++++++++++++++- src/JournalView.tsx | 66 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 src/JournalView.tsx diff --git a/src/App.tsx b/src/App.tsx index ea00d29..5e8a1a7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,6 +4,7 @@ import { Switch, Route } from 'react-router'; import './App.css'; import { JournalList } from './JournalList'; +import { JournalView } from './JournalView'; import { RouteResolver, RouteKeysType } from './routes'; interface RouteItem { @@ -25,8 +26,7 @@ const navigationRoutes: RouteItem[] = [ path: 'journals._id', exact: true, name: 'Journal', - keys: {journalUid: 1}, - component: JournalList, + component: JournalView, }, ]; diff --git a/src/JournalList.tsx b/src/JournalList.tsx index f34b324..0a969f4 100644 --- a/src/JournalList.tsx +++ b/src/JournalList.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import { Link } from 'react-router-dom'; import * as EteSync from './api/EteSync'; @@ -7,6 +8,17 @@ const USER = 'test@localhost'; const PASSWORD = 'SomePassword'; const derived = EteSync.deriveKey(USER, PASSWORD); +import { RouteResolver } from './routes'; +const routeResolver = new RouteResolver({ + home: '', + journals: { + _base: 'journals', + _id: { + _base: ':journalUid', + }, + }, +}); + export class JournalList extends React.Component { state: { journals: Array, @@ -40,7 +52,16 @@ export class JournalList extends React.Component { 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 ( +
  • + + {info.displayName}: {info.type} ({journal.uid}) + +
  • + ); }); return ( diff --git a/src/JournalView.tsx b/src/JournalView.tsx new file mode 100644 index 0000000..61f1dbf --- /dev/null +++ b/src/JournalView.tsx @@ -0,0 +1,66 @@ +import * as React from 'react'; + +import * as EteSync from './api/EteSync'; + +const SERVICE_API = 'http://localhost:8000'; +const USER = 'hacj'; +const PASSWORD = 'hack'; +const derived = EteSync.deriveKey(USER, PASSWORD); + +export class JournalView extends React.Component { + static defaultProps = { + prevUid: null, + }; + + state: { + entries: Array, + }; + props: { + match: any, + prevUid: string | null, + }; + + constructor(props: any) { + super(props); + this.state = { + entries: [], + }; + } + + componentDidMount() { + const journal = this.props.match.params.journalUid; + let authenticator = new EteSync.Authenticator(SERVICE_API); + + authenticator.getAuthToken(USER, PASSWORD).then((authToken) => { + const credentials = new EteSync.Credentials(USER, authToken); + + let entryManager = new EteSync.EntryManager(credentials, SERVICE_API, journal); + entryManager.list(this.props.prevUid).then((entries) => { + this.setState({ entries }); + }); + }); + } + + render() { + const journal = this.props.match.params.journalUid; + let prevUid = this.props.prevUid; + const journals = this.state.entries.map((entry) => { + // FIXME: actually get the correct version! + let cryptoManager = new EteSync.CryptoManager(derived, journal, 1); + let syncEntry = entry.getSyncEntry(cryptoManager, prevUid); + prevUid = entry.uid; + return (
  • {syncEntry.type}: {syncEntry.content}
  • ); + }); + + return ( +
    +
    +

    Welcome to React

    +
    +
      + {journals} +
    +
    + ); + } +}