Skip to content

Commit

Permalink
fix: remove weird cache
Browse files Browse the repository at this point in the history
  • Loading branch information
kdoran committed Apr 10, 2020
1 parent 013e63c commit f9e8320
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 49 deletions.
68 changes: 32 additions & 36 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Nav from './components/Nav'
import Login from './components/Login'
import Loading from './components/Loading'
import withParams from './containers/withParams'
import cache from './utils/cache'
import { parseUrl } from './utils/utils'
import fetcher from 'utils/fetcher'

Expand All @@ -24,11 +23,11 @@ import 'app-tags.css'
const Databases = withParams(DatabasesContainer)
const LIMIT = 100

class UserRoutes extends Component {
class CouchRoutes extends Component {
constructor (props) {
super(props)
this.state = {
authenticated: !!cache.userCtx.name,
userCtx: null,
loading: true,
couchUrl: parseUrl(props.match.params.couch),
dbs: null
Expand All @@ -45,47 +44,44 @@ class UserRoutes extends Component {
return
}

this.onAuthenticated(userCtx)
this.onUser(userCtx)
}

// TODO: this cache object was never used much
onAuthenticated = async (userCtx) => {
onUser = async (userCtx) => {
const { couchUrl } = this.state
Object.assign(cache.userCtx, userCtx)
const dbs = await fetcher.get(`${couchUrl}_all_dbs`, { limit: LIMIT })
this.setState({ loading: false, dbs, authenticated: true })
this.setState({ loading: false, dbs, userCtx })
}

render () {
const { authenticated, loading, couchUrl, dbs } = this.state
if (loading) {
return <Loading />
}
if (!authenticated) {
return <Login couchUrl={couchUrl} onAuthenticated={this.onAuthenticated} />
} else {
return (
<div>
<div className='page'>
<Switch>
<Route path='/:couch/:dbName/:docId/editing' component={withParams(EditDocContainer)} />
<Route exact path='/:couch/:dbName/query' component={withParams(QueryContainer)} />
<Route exact path='/:couch/_node/couchdb@localhost/_config/admins' component={withParams(AdminContainer)} />
<Route exact path='/:couch/_node/nonode@nohost/_config/admins' component={withParams(AdminContainer)} />
<Route exact path='/:couch/_node/couchdb@localhost/_config' component={withParams(ConfigContainer)} />
<Route exact path='/:couch/_node/nonode@nohost/_config' component={withParams(ConfigContainer)} />
<Route path='/:couch/:dbName/query/:queryId/:queryString' component={withParams(QueryContainer)} />
<Route path='/:couch/:dbName/query/:queryId' component={withParams(QueryContainer)} />
<Route path='/:couch/:dbName/:docId/:rev/' component={withParams(DocContainer)} />
<Route path='/:couch/:dbName/:docId' component={withParams(DocContainer)} />
<Route path='/:couch/:dbName' component={withParams(DocsContainer)} />
<Route path='/:couch/' component={props => <Databases {...props} dbs={dbs} />} />
</Switch>
</div>
<Route path='/:couch/:dbName?/:docId?' render={props => (<Nav {...props} dbs={dbs} userCtx={cache.userCtx} />)} />
const { userCtx, loading, couchUrl, dbs } = this.state

if (loading) return <Loading />

if (!userCtx) return <Login couchUrl={couchUrl} onUser={this.onUser} />

return (
<div>
<div className='page'>
<Switch>
<Route path='/:couch/:dbName/:docId/editing' component={withParams(EditDocContainer)} />
<Route exact path='/:couch/:dbName/query' component={withParams(QueryContainer)} />
<Route exact path='/:couch/_node/couchdb@localhost/_config/admins' component={withParams(AdminContainer)} />
<Route exact path='/:couch/_node/nonode@nohost/_config/admins' component={withParams(AdminContainer)} />
<Route exact path='/:couch/_node/couchdb@localhost/_config' component={withParams(ConfigContainer)} />
<Route exact path='/:couch/_node/nonode@nohost/_config' component={withParams(ConfigContainer)} />
<Route path='/:couch/:dbName/query/:queryId/:queryString' component={withParams(QueryContainer)} />
<Route path='/:couch/:dbName/query/:queryId' component={withParams(QueryContainer)} />
<Route path='/:couch/:dbName/:docId/:rev/' component={withParams(DocContainer)} />
<Route path='/:couch/:dbName/:docId' component={withParams(DocContainer)} />
<Route path='/:couch/:dbName' component={withParams(DocsContainer)} />
<Route path='/:couch/' component={props => <Databases {...props} dbs={dbs} />} />
</Switch>
</div>
)
}
<Route path='/:couch/:dbName?/:docId?' render={props => (<Nav {...props} dbs={dbs} userCtx={userCtx} />)} />
</div>
)
}
}

Expand All @@ -95,7 +91,7 @@ class App extends Component {
<Router>
<div>
<Route exact path='/' component={SetupCouchContainer} />
<Route path='/:couch/' component={UserRoutes} />
<Route path='/:couch/' component={CouchRoutes} />
</div>
</Router>
)
Expand Down
2 changes: 1 addition & 1 deletion app/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class LoginContainer extends React.Component {
this.setState({ loading: true })
try {
const response = await fetcher.login(couchUrl, this.refs.username.value, this.refs.password.value)
this.props.onAuthenticated(response)
this.props.onUser(response)
} catch (error) {
this.setState({ error, loading: false })
}
Expand Down
10 changes: 5 additions & 5 deletions app/containers/SetupCouchContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import React from 'react'
import fetcher from 'utils/fetcher'
import localStorager from 'utils/localstorager'
import {parseUrl} from 'utils/utils'
import cache from '../utils/cache'

export default class extends React.Component {
export default class SetupCouchContainer extends React.Component {
constructor () {
super()
let recentCouches = localStorager.getRecent('couchurls').sort()
Expand All @@ -22,9 +21,9 @@ export default class extends React.Component {
this.setState({inputUrl})
this.setState({ loading: true })
try {
const { userCtx } = await fetcher.checkSession(inputUrl)
// is the couch reachable?
await fetcher.get(inputUrl)
localStorager.saveRecent('couchurls', inputUrl)
Object.assign(cache.userCtx, userCtx)
this.props.history.push(inputUrl.split('//')[1])
} catch (error) {
this.setState({ error: error.toString(), loading: false })
Expand All @@ -44,6 +43,7 @@ export default class extends React.Component {

render () {
const { inputUrl, recentCouches, error, loading } = this.state

return (
<form onSubmit={this.onSubmit}>
<h1>CouchDB Lookout: Select Couch Server</h1>
Expand All @@ -67,7 +67,7 @@ export default class extends React.Component {
<br /><br />
{recentCouches.map(url => (
<div key={url}>
&nbsp; <a href='javascript:void(0)' onClick={() => this.onCouchClick(url)}>
&nbsp; <a href='#' onClick={(e) => { e.preventDefault(); this.onCouchClick(url) }}>
{url}
</a>
<br /><br />
Expand Down
7 changes: 0 additions & 7 deletions app/utils/cache.js

This file was deleted.

0 comments on commit f9e8320

Please sign in to comment.