|
1 | 1 | import react, { Component } from 'react' |
2 | 2 | import PropTypes from 'prop-types' |
3 | 3 | import { connect } from 'react-redux' |
4 | | -import { |
5 | | - firebaseConnect, |
6 | | - isLoaded, |
7 | | - isEmpty, |
8 | | - pathToJS, |
9 | | - dataToJS |
10 | | -} from 'react-redux-firebase' |
11 | | - |
12 | | -import logo from './logo.svg' |
13 | | -import TodoItem from './TodoItem' |
14 | | -import './App.css' |
| 4 | +import { isLoaded, isEmpty, pathToJS } from 'react-redux-firebase' |
| 5 | +import TodosView from './Todos' |
| 6 | +import LoginView from './Todos' |
15 | 7 |
|
16 | 8 | class App extends Component { |
17 | 9 | static propTypes = { |
18 | | - todos: PropTypes.object |
| 10 | + auth: PropTypes.object |
19 | 11 | } |
20 | 12 |
|
21 | 13 | render () { |
22 | | - const { todos } = this.props |
| 14 | + const { auth } = this.props |
| 15 | + |
| 16 | + // handle initial loading of auth |
| 17 | + if (!isLoaded(auth)) { |
| 18 | + return <div>Loading...</div> |
| 19 | + } |
23 | 20 |
|
24 | | - const todosList = (!isLoaded(todos)) |
25 | | - ? 'Loading' |
26 | | - : (isEmpty(todos)) |
27 | | - ? 'Todo list is empty' |
28 | | - : Object.keys(todos).map((key) => ( |
29 | | - <TodoItem key={key} id={key} todo={todos[key]} /> |
30 | | - )) |
31 | | - return ( |
32 | | - <div className='App'> |
33 | | - <div className='App-header'> |
34 | | - <h2>react-redux-firebase Auth Based Query Demo</h2> |
35 | | - <img src={logo} className='App-logo' alt='logo' /> |
36 | | - </div> |
37 | | - <div className='App-todos'> |
38 | | - <h4> |
39 | | - Loaded From |
40 | | - <span className='App-Url'> |
41 | | - <a href='https://react-redux-firebase.firebaseio.com/'> |
42 | | - react-redux-firebase.firebaseio.com |
43 | | - </a> |
44 | | - </span> |
45 | | - </h4> |
46 | | - <h4>Todos List</h4> |
47 | | - {todosList} |
48 | | - </div> |
49 | | - </div> |
50 | | - ) |
| 21 | + if (isEmpty(auth)) { |
| 22 | + return <LoginView /> |
| 23 | + } |
| 24 | + |
| 25 | + return <TodosView auth={auth} /> |
51 | 26 | } |
52 | 27 | } |
53 | 28 |
|
54 | | -const authWrappedComponent = connect( |
| 29 | +export default connect( |
55 | 30 | ({ firebase }) => ({ |
56 | 31 | auth: pathToJS(firebase, 'auth') |
57 | 32 | }) |
58 | 33 | )(App) |
59 | | - |
60 | | -const fbWrappedComponent = firebaseConnect( |
61 | | - ({ auth }) => ([ |
62 | | - `/todos#orderByChild=uid&equalTo=${auth ? auth.uid : ''}` |
63 | | - /* object notation equivalent |
64 | | - { |
65 | | - path: '/todos', |
66 | | - queryParams: ['orderByChild=uid', `equalTo=${auth ? auth.uid : ''}`] |
67 | | - } |
68 | | - */ |
69 | | - ]) |
70 | | -)(authWrappedComponent) |
71 | | - |
72 | | -export default connect( |
73 | | - ({ firebase }) => ({ |
74 | | - todos: dataToJS(firebase, 'todos') |
75 | | - }) |
76 | | -)(fbWrappedComponent) |
0 commit comments