diff --git a/src/App.tsx b/src/App.tsx index 958c461..65d3793 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,34 +1,9 @@ import * as React from 'react'; import { HashRouter } from 'react-router-dom'; -import { Switch, Route } from 'react-router'; import './App.css'; -import { JournalList } from './JournalList'; -import { JournalView } from './JournalView'; -import { RouteResolver, RouteKeysType } from './routes'; - -interface RouteItem { - path: string; - exact?: boolean; - keys?: RouteKeysType; - name: string; - component: React.ComponentType; -} - -const navigationRoutes: RouteItem[] = [ - { - path: 'home', - exact: true, - name: 'Home', - component: JournalList, - }, - { - path: 'journals._id', - exact: true, - name: 'Journal', - component: JournalView, - }, -]; +import { EteSyncContext } from './EteSyncContext'; +import { RouteResolver } from './routes'; export const routeResolver = new RouteResolver({ home: '', @@ -44,16 +19,7 @@ class App extends React.Component { render() { return ( - - {navigationRoutes.map((route, index) => ( - - ))} - + ); } diff --git a/src/EteSyncContext.tsx b/src/EteSyncContext.tsx new file mode 100644 index 0000000..7985294 --- /dev/null +++ b/src/EteSyncContext.tsx @@ -0,0 +1,61 @@ +import * as React from 'react'; +import { Switch, Route } from 'react-router'; + +import { JournalList } from './JournalList'; +import { JournalView } from './JournalView'; + +import * as EteSync from './api/EteSync'; + +import { routeResolver } from './App'; + +const SERVICE_API = 'http://localhost:8000'; +const USER = 'me@etesync.com'; +const PASSWORD = ''; +const derived = EteSync.deriveKey(USER, PASSWORD); + +export interface EteSyncContextType { + serviceApiUrl: string; + credentials: EteSync.Credentials; + encryptionKey: string; +} + +export class EteSyncContext extends React.Component { + static state: EteSyncContextType; + + componentDidMount() { + let authenticator = new EteSync.Authenticator(SERVICE_API); + + authenticator.getAuthToken(USER, PASSWORD).then((authToken) => { + const credentials = new EteSync.Credentials(USER, authToken); + + const context = { + serviceApiUrl: SERVICE_API, + credentials, + encryptionKey: derived, + }; + + this.setState(context); + }); + } + + render() { + if (this.state === null) { + return (
loading
); + } + + return ( + + } + /> + } + /> + + ); + } +} diff --git a/src/JournalList.tsx b/src/JournalList.tsx index 9c42816..1aeab64 100644 --- a/src/JournalList.tsx +++ b/src/JournalList.tsx @@ -1,16 +1,16 @@ import * as React from 'react'; import { Link } from 'react-router-dom'; +import { EteSyncContextType } from './EteSyncContext'; 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); - import { routeResolver } from './App'; export class JournalList extends React.Component { + props: { + etesync: EteSyncContextType + }; + state: { journals: Array, }; @@ -23,23 +23,21 @@ export class JournalList extends React.Component { } componentDidMount() { - let authenticator = new EteSync.Authenticator(SERVICE_API); - - authenticator.getAuthToken(USER, PASSWORD).then((authToken) => { - const credentials = new EteSync.Credentials(USER, authToken); + const credentials = this.props.etesync.credentials; + const apiBase = this.props.etesync.serviceApiUrl; - 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 }); - }); + let journalManager = new EteSync.JournalManager(credentials, apiBase); + journalManager.list().then((journals) => { + journals = journals.filter((x) => ( + // Skip shared journals for now. + !x.key + )); + this.setState({ journals }); }); } render() { + const derived = this.props.etesync.encryptionKey; const journals = this.state.journals.map((journal, idx) => { let cryptoManager = new EteSync.CryptoManager(derived, journal.uid, journal.version); let info = journal.getInfo(cryptoManager); diff --git a/src/JournalView.tsx b/src/JournalView.tsx index 61f1dbf..4b73a1b 100644 --- a/src/JournalView.tsx +++ b/src/JournalView.tsx @@ -1,12 +1,8 @@ import * as React from 'react'; +import { EteSyncContextType } from './EteSyncContext'; 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, @@ -16,8 +12,9 @@ export class JournalView extends React.Component { entries: Array, }; props: { + etesync: EteSyncContextType match: any, - prevUid: string | null, + prevUid?: string | null, }; constructor(props: any) { @@ -28,22 +25,20 @@ export class JournalView extends React.Component { } componentDidMount() { + const credentials = this.props.etesync.credentials; + const apiBase = this.props.etesync.serviceApiUrl; 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 }); - }); + let entryManager = new EteSync.EntryManager(credentials, apiBase, journal); + entryManager.list(this.props.prevUid || null).then((entries) => { + this.setState({ entries }); }); } render() { + const derived = this.props.etesync.encryptionKey; const journal = this.props.match.params.journalUid; - let prevUid = this.props.prevUid; + let prevUid = this.props.prevUid || null; const journals = this.state.entries.map((entry) => { // FIXME: actually get the correct version! let cryptoManager = new EteSync.CryptoManager(derived, journal, 1);