-
Notifications
You must be signed in to change notification settings - Fork 0
Firebase events #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Firebase events #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { SAVE_POST, FETCH_POSTS } from '../ActionsTYPES/TYPES' | ||
|
|
||
|
|
||
| export const savePost = (post) => | ||
| (dispatch, getState, getFirebase) => { | ||
| const firebase = getFirebase() | ||
| firebase.database().ref(`groups/${post.groupTitle}/posts/${post.postIndex}`) | ||
| .set(post) | ||
| .then(() => { | ||
| dispatch({ type: SAVE_POST, payload: 'Success' }) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'success' ? look at my reducer how it should be, if you want to update just status, u need to create property inside reducer
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will work on it |
||
| }) | ||
| .catch((err) => { | ||
| dispatch({ type: SAVE_POST, payload: err }) | ||
| }) | ||
| } | ||
|
|
||
| export const fetchPosts = () => (dispatch, getState, getFirebase) => { | ||
| const firebase = getFirebase() | ||
| const posts = firebase.database().ref('groups/') | ||
|
|
||
| posts.on('value', function (snapshot) { | ||
| console.log(snapshot.val()) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove all console logs
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
| dispatch({ type: FETCH_POSTS, payload: snapshot.val() || [] }) | ||
| }); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ export function runTheApp() { | |
| } | ||
| } | ||
|
|
||
|
|
||
| export default { | ||
| runTheApp | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import React, { Component } from 'react'; | ||
| import CreatePostModal from './components/CreatePostModal'; | ||
|
|
||
| class GroupPage extends Component { | ||
|
|
||
|
|
||
| render() { | ||
| return ( | ||
| <CreatePostModal /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default GroupPage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import React, { Component } from 'react'; | ||
| import { connect } from 'react-redux'; | ||
| import { bindActionCreators } from 'redux'; | ||
| import { withRouter } from 'react-router' | ||
| import { savePost } from '../../../actions/events'; | ||
| import ReactModal from 'react-modal'; | ||
|
|
||
| ReactModal.setAppElement('#root'); | ||
|
|
||
| class CreatePostModal extends React.Component { | ||
| constructor() { | ||
| super(); | ||
| this.state = { | ||
| showModal: false, | ||
| postContext: '' | ||
| }; | ||
|
|
||
| this.handleOpenModal = this.handleOpenModal.bind(this); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you will use arrow functions you don't need to bind actions!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know about this, but used official example, and didn't change it |
||
| this.handleCloseModal = this.handleCloseModal.bind(this); | ||
| } | ||
|
|
||
| handleOpenModal() { | ||
| this.setState({ showModal: true }); | ||
| } | ||
|
|
||
| handleCloseModal() { | ||
| const { postContext } = this.state; | ||
| const { groups } = this.props; | ||
| const { groupTitle } = this.props.match.params; | ||
|
|
||
| this.setState({ showModal: false }); | ||
| this.props.savePost({text: postContext, groupTitle, postIndex: groups[groupTitle].posts.length} ) | ||
| } | ||
|
|
||
|
|
||
| render() { | ||
| return ( | ||
| <div> | ||
| <button onClick={this.handleOpenModal}>Создать пост</button> | ||
| <ReactModal | ||
| isOpen={this.state.showModal} | ||
| contentLabel="Create Post Modal" | ||
| style={{ | ||
| content: { | ||
| background: 'rgba(255, 255, 255, 0.3)' | ||
| } | ||
| }}> | ||
| <textarea style={{width:'80%', height: '80%'}} | ||
| type="text" | ||
| //value={this.state.postContext} | ||
| onChange={(elem)=>this.setState({postContext: elem.target.value})}> | ||
| </textarea> | ||
| <div> | ||
| <button onClick={this.handleCloseModal}>Опубликовать</button> | ||
| </div> | ||
| </ReactModal> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const mapStateToProps = (state) => { | ||
| return { | ||
| groups: state.postsReducer | ||
| } | ||
| } | ||
|
|
||
| const mapDispatchToProps = (dispatch) => { | ||
| return { | ||
| savePost: bindActionCreators(savePost, dispatch), | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export default connect(mapStateToProps, mapDispatchToProps)(withRouter(CreatePostModal)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { connect } from 'react-redux'; | |
| import { bindActionCreators } from 'redux'; | ||
| import { getAllGroups } from '../../actions/senders'; | ||
| import Group from './components/group'; | ||
| import group from './components/group'; | ||
| import _ from 'lodash' | ||
|
|
||
| class Home extends Component { | ||
| constructor(){ | ||
|
|
@@ -15,18 +15,24 @@ class Home extends Component { | |
| } | ||
|
|
||
| render(){ | ||
| console.log('[Home Component][render]'); | ||
| const groups = this.props.groupStatus && this.props.groupStatus.groups ? this.props.groupStatus.groups.map(item => ( | ||
| <Group | ||
| key={item.nameUS} | ||
| title={item.nameUS} | ||
| /> | ||
| ) | ||
| ) : []; | ||
| console.log(this.props.groupStatus); | ||
| var groups = []; | ||
| if(this.props.groupStatus && this.props.groupStatus.groups) { | ||
| _.mapValues(this.props.groupStatus.groups, (item) => { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why you remove the group map?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why lodash?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because now it's object, not array. It's need for certain reasons(create readable routes and easely finding group by group name), and lodash don't create new array like native js map method, so I create new array manually.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And lodash, because lodash cool =) |
||
|
|
||
| groups.push( | ||
| <Group | ||
| key={item.index} | ||
| title={item.nameUS} | ||
| index={item.index} | ||
| /> | ||
| ) | ||
| }) | ||
| } | ||
| return( | ||
| <div> | ||
| <div style={{padding: '20px'}}> | ||
| <div> | ||
| {this.props.groupStatus.pending ? 'Loading' : groups } | ||
| {this.props.groupStatus.pending && !groups ? 'Loading' : groups } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. && !groups? - wrong logic
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. groups.length |
||
| </div> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { | ||
| FETCH_POSTS | ||
| } from '../ActionsTYPES/TYPES'; | ||
|
|
||
| const initialState = {} | ||
|
|
||
|
|
||
| function postsReducer(state = initialState, action) { | ||
| switch (action.type) { | ||
| case FETCH_POSTS: { | ||
| return action.payload | ||
| } | ||
|
|
||
| default: { | ||
| return state; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| export default postsReducer; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,16 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { Route, Switch,} from 'react-router-dom'; | ||
| import { Route, Switch, } from 'react-router-dom'; | ||
|
|
||
| import Chat from './components/Chat/Chat'; | ||
| import Programs from './components/Programs/Programs'; | ||
| import Home from './components/Home/Home'; | ||
| import GroupPage from './components/GroupPage/GroupPage'; | ||
|
|
||
| const Routs = () => ( | ||
| <Switch> | ||
| <Route exact path='/' component={Home} /> | ||
| <Route exact path='/:groupName' component={Programs} /> | ||
| <Route exact path='/:groupTitle' component={GroupPage} /> | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not the title - its category! |
||
| <Route path='/programs' component={Programs} /> | ||
| <Route path='/chat' component={Chat} /> | ||
| </Switch> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,21 @@ | ||
| import { createStore, applyMiddleware, compose } from 'redux'; | ||
| import rootReducer from '../reducers/rootReducer'; | ||
| import firebase from 'firebase'; | ||
| import firebase, { storage } from 'firebase'; | ||
| import { reactReduxFirebase, getFirebase } from 'react-redux-firebase'; | ||
|
|
||
| // REMOTE REDUCERS | ||
| import { createLogger } from 'redux-logger' | ||
| import thunk from 'redux-thunk'; | ||
| import { runTheApp } from '../actions/globalActions'; | ||
| import { fetchPosts } from '../actions/events' ; | ||
| const fbConfig = { | ||
| apiKey: "AIzaSyBxeJ64H8GH4NXT_fy5S0ATdG9w4fAKfmk", | ||
| authDomain: "masa-wall.firebaseapp.com", | ||
| databaseURL: "https://masa-wall.firebaseio.com", | ||
| projectId: "masa-wall", | ||
| storageBucket: "masa-wall.appspot.com", | ||
| messagingSenderId: "1008969413809" | ||
| } | ||
| apiKey: "AIzaSyC29RdE1-GOOrw_db5EhI0bn4hrWhR3Z1s", | ||
| authDomain: "masa-projects-posts.firebaseapp.com", | ||
| databaseURL: "https://masa-projects-posts.firebaseio.com", | ||
| projectId: "masa-projects-posts", | ||
| storageBucket: "masa-projects-posts.appspot.com", | ||
| messagingSenderId: "457533567638" | ||
| } | ||
|
|
||
| const config = { | ||
| userProfile: 'users', // firebase root where user profiles are stored | ||
|
|
@@ -45,5 +45,6 @@ const store = createStore( | |
|
|
||
|
|
||
| store.dispatch(runTheApp()) | ||
| store.dispatch(fetchPosts()) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you remember, here we add all listeners - fetch posts name is not relevant here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I will find a better place for this event listener |
||
|
|
||
| export default store; | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add more actions - pending + rejected + toInitial + success
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I understand this