Guest User

Untitled

a guest
Jan 10th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. import React, {Component} from 'react';
  2. import {fire} from '../firebaseConfig';
  3. import { connect} from "react-redux";
  4. import moment from 'moment';
  5. import '../index.css';
  6. import {withRouter, Redirect} from "react-router-dom";
  7. import Card from '@material-ui/core/Card';
  8. import CardContent from '@material-ui/core/CardContent';
  9. import Typography from '@material-ui/core/Typography';
  10.  
  11. const styles = {
  12. card: {
  13. minWidth: 275,
  14. margin:'40px 0px',
  15.  
  16. },
  17. p:{
  18. margin:'20px 0px',
  19. letterSpacing: '2.7px',
  20. fontSize:'0.8em',
  21. fontStyle: 'italic'
  22. },
  23. h:{
  24. letterSpacing: '5px'
  25. }
  26. };
  27.  
  28. class Dashboard extends Component{
  29.  
  30. constructor(props){
  31. super(props)
  32.  
  33. this.state = {
  34. username:"",
  35. loading: true,
  36. posts:{}
  37. }
  38.  
  39. }
  40. componentWillMount(){
  41. this.getPosts();
  42. }
  43.  
  44.  
  45.  
  46. getPosts() {
  47. const collection2 = fire.collection('posts');
  48. collection2.get().then(snapshot => {
  49. let arr = [];
  50. snapshot.forEach(doc => {
  51. arr.push(doc.data()); //Collect all the items and push it into the array
  52. })
  53. this.setState({
  54. posts: arr,
  55. })
  56. console.log(this.state.posts)
  57. })
  58.  
  59.  
  60. }
  61.  
  62.  
  63. componentDidMount(){
  64. if(this.props.userId){
  65. const collection = fire.collection('users');
  66. collection.get().then(snapshot => {
  67. snapshot.forEach(doc => {
  68. this.setState({
  69. username: doc.data().username,
  70. loading:false
  71. })
  72. });
  73. });
  74.  
  75. }
  76.  
  77. }
  78.  
  79. render(){
  80. if (!this.props.userId) return <Redirect to='/' />
  81. const { loading, posts } = this.state;
  82.  
  83. if(loading){
  84. return(
  85. <div className="loader"></div>
  86. )
  87. }
  88. return(
  89. <div className="container">
  90. <div className="row">
  91. <div className="col-md-6 mt-3">
  92. <h1>Welcome {this.state.username.toLowerCase()}</h1>
  93.  
  94. {posts.map((post, key)=> {
  95. return(
  96. <Card key={key} style={styles.card}>
  97. <CardContent>
  98.  
  99. <Typography variant="h4" component="h2" style={styles.h}>
  100. {post.description}
  101. </Typography>
  102. <Typography component="p" style={styles.p}>
  103. by: {post.username}
  104. </Typography>
  105.  
  106. <Typography component="p">
  107. by: {moment(post.createdAt.toDate()).calendar()}
  108. </Typography>
  109.  
  110. </CardContent>
  111. </Card>
  112. );
  113. })}
  114. </div>
  115. </div>
  116. </div>
  117.  
  118.  
  119. );
  120. }
  121.  
  122.  
  123. }
  124.  
  125. const mapStateToProps = (state) => ({
  126. userId: state.auths.userId
  127. })
  128.  
  129.  
  130. export default withRouter(connect(mapStateToProps, null)(Dashboard));
  131.  
  132. import { auth as firebaseAuth } from '../firebaseConfig'
  133. import {fire} from '../firebaseConfig'
  134. import { history } from '../components/Navbar';
  135.  
  136. export const SET_USER = "SET_USER";
  137.  
  138. export const signUp = (user) => { return (dispatch) => {
  139. firebaseAuth.createUserWithEmailAndPassword(user.email.trim(), user.password)
  140. .then(resp => {
  141. return fire.collection('users').doc(resp.user.uid).set({
  142. username:user.username,
  143. email: user.email,
  144. password: user.password,
  145. uid: resp.user.uid
  146.  
  147. });
  148. })
  149. .then(() => {
  150. dispatch({ type: 'SIGNUP_SUCCESS' });
  151. })
  152. .then((response) => {
  153. history.push('/dashboard');
  154. }).catch((err) => {
  155. dispatch({ type: 'SIGNUP_ERROR', err});
  156. });
  157.  
  158.  
  159. }
  160.  
  161. }
  162.  
  163. export const signIn = (user) => { return (dispatch) => {
  164.  
  165. firebaseAuth.signInWithEmailAndPassword(user.email, user.password)
  166. .then(()=> {
  167. dispatch({type: 'SIGNIN_SUCCESS'})
  168. }).then((response) => {
  169. history.push('/dashboard');
  170. }).catch( (err) => {
  171. dispatch({ type: 'SIGNIN_ERROR', err});
  172. });
  173. }
  174.  
  175. }
  176.  
  177. export const signOut = () => { return (dispatch) => {
  178.  
  179. firebaseAuth.signOut().then(() => {
  180. dispatch({ type: 'LOGOUT_SUCCESS'})
  181. })
  182. }
  183. }
  184.  
  185.  
  186. export const CurrentUser = () => dispatch => {
  187. firebaseAuth.onAuthStateChanged((user) => {
  188. if (user) {
  189. dispatch({
  190. type: SET_USER,
  191. payload: user
  192. })
  193. }
  194. });
  195.  
  196. }
  197.  
  198. export const createPost = (post) => {
  199. return (dispatch, getState) => {
  200. // async call to the database
  201. const profile = getState().firebase.profile;
  202. const authorId = getState().firebase.auth;
  203. fire.collection('posts').add({
  204. description: post.description,
  205. username: post.username,
  206. createdAt: new Date()
  207. }).then(() => {
  208. dispatch({type: 'CREATE_POST', post})
  209. }).catch((err) => {
  210. dispatch({type: 'CREATE_POST_ ERROR', err})
  211. })
  212. }
  213. }
  214.  
  215. import { SET_USER} from '../actions/';
  216. const initialState = {
  217. authError: null,
  218. isAuthenticated: false,
  219. userId: null,
  220. user: {}
  221. }
  222.  
  223. export default (state = initialState, action) => {
  224. switch (action.type) {
  225. case SET_USER:
  226. return ({
  227. userId: action.payload.uid || null,
  228. // user:action.payload,
  229. isAuthenticated: true
  230. })
  231. case 'LOGOUT_SUCCESS':
  232. console.log('signout success')
  233. return ({
  234. ...state,
  235. userId: null,
  236. isAuthenticated: false
  237. })
  238.  
  239. case 'CREATE_POST':
  240. console.log('created post', action.post)
  241. return state;
  242.  
  243. case 'CREATE_POST_ERROR':
  244. console.log('create post error', action.err)
  245. return state;
  246.  
  247. case 'SIGNUP_SUCCESS':
  248. return ({
  249. ...state,
  250. authError: null
  251. })
  252.  
  253. case 'SIGNUP_ERROR':
  254. console.log('signup error')
  255. return ({
  256. ...state,
  257. authError: action.err.message
  258. })
  259.  
  260. case 'SIGNIN_SUCCESS':
  261. console.log('signin success')
  262. return ({
  263. ...state,
  264. authError: null
  265. })
  266.  
  267. case 'SIGNIN_ERROR':
  268. console.log('signin error')
  269. return ({
  270. ...state,
  271. authError: action.err.message
  272. })
  273. default:
  274. return state
  275. }
  276. }
Add Comment
Please, Sign In to add comment