Skip to content

Commit

Permalink
Added HookClock, UserPage, and HelloUser components. Bug existing for…
Browse files Browse the repository at this point in the history
… importing and employing HelloUser
  • Loading branch information
jourdiw committed Feb 24, 2019
1 parent 4783aa8 commit ac1f3d4
Show file tree
Hide file tree
Showing 10 changed files with 1,783 additions and 116 deletions.
1,802 changes: 1,703 additions & 99 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"private": true,
"proxy": "http://localhost:3001",
"dependencies": {
"create-react-app": "^2.1.5",
"express": "^4.16.4",
"isomorphic-unfetch": "^3.0.0",
"react": "^16.8.1",
"react-dom": "^16.8.1",
"next": "^8.0.1",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-redux": "^6.0.0",
"react-router-dom": "^4.3.1",
"react-scripts": "^2.1.5",
Expand Down
6 changes: 3 additions & 3 deletions src/components/AppHeader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Menu } from "semantic-ui-react";
import { Link } from "react-router-dom";
import AuthButton from "auth/components/AuthButton";
// import Clock from "../Clock";
import Clock from "../HookClock"
import HelloUser from '../HelloUser'
import Clock from "../HookClock";
// import HelloUser from '../HelloUser';

const AppHeader = ({ title, displayClock, ...props }) => (
<Menu fixed="top" inverted {...props}>
Expand All @@ -21,7 +21,7 @@ const AppHeader = ({ title, displayClock, ...props }) => (
TV Shows
</Menu.Item>
<Menu.Item>
<HelloUser />
{/* <HelloUser /> */}
</Menu.Item>
<Menu.Item>
<AuthButton />
Expand Down
15 changes: 15 additions & 0 deletions src/components/HelloUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import { connect } from 'net';

const HelloUser = ({ username }) =>
username ? (
<p>Hello, {username}</p>
) : (
<p>No user detected</p>
);

// export default HelloUser
export default connect(
(state) => ({ username: state.auth.username }),
null
)(HelloUser);
18 changes: 18 additions & 0 deletions src/components/HookClock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { useState, useEffect } from 'react'

const Clock = () => {
const [date, setDate] = useState(new Date())
useEffect(()=> {
const timer = setInterval(
() => {
setDate(new Date())
}, 1000
)
return () => {
clearInterval(timer)
}
}, [])
return <span>{date.toLocaleTimeString()}</span>
}

export default Clock
6 changes: 6 additions & 0 deletions src/modules/auth/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const AUTH_SUCCESS = "@auth/AUTH_SUCCESS";
export const AUTH_DELETE = "@auth/AUTH_DELETE";
export const GREET_USER = "GREET_USER"

export const authSuccess = token => ({
type: AUTH_SUCCESS,
Expand All @@ -9,3 +10,8 @@ export const authSuccess = token => ({
export const authDelete = token => ({
type: AUTH_DELETE
});

export const updateUserGreeting = username => ({
type: GREET_USER,
payload: username
})
4 changes: 2 additions & 2 deletions src/modules/auth/pages/AuthCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class AuthCode extends Component {
this.setState({ loading: false });

if (json.access_token) {
this.setState({ error: false, success: true });
this.setState({ error: false, success: true, loading: false });
this.props.handleAuthSuccess(json.access_token);
setTimeout(() => history.replace("/auth/user"), 1000);
} else {
this.setState({ error: "Invalid code", success: false });
this.setState({ error: "Invalid code", success: false, loading: false });
}
} catch (err) {
console.error(err);
Expand Down
18 changes: 10 additions & 8 deletions src/modules/auth/pages/UserPage.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { Component } from "react";

import { connect } from "react-redux";

// const UserPage = () => ;
import { updateUserGreeting } from "../actions"

class UserPage extends Component {
state = {
Expand All @@ -22,6 +21,7 @@ class UserPage extends Component {
user,
loading: false
});
this.props.handleUserGreeting(user.login)
} else {
history.replace("/auth/github");
}
Expand All @@ -48,9 +48,11 @@ class UserPage extends Component {
}
}

export default connect(
// mapStateToProps:
state => ({
...state.auth
})
)(UserPage);
const mapDispatchToProps = dispatch => ({
handleUserGreeting: (username) => dispatch(updateUserGreeting(username)),
})

export default connect (
(state) => ({ ...state.auth}),
mapDispatchToProps
)(UserPage)
7 changes: 5 additions & 2 deletions src/modules/auth/reducer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AUTH_SUCCESS, AUTH_DELETE } from "./actions";
import { AUTH_SUCCESS, AUTH_DELETE, GREET_USER } from "./actions";

const initialState = {
token: null,
authenticated: false
authenticated: false,
username: null
};

const reducer = (state = initialState, action) => {
Expand All @@ -11,6 +12,8 @@ const reducer = (state = initialState, action) => {
return { ...state, token: action.payload, authenticated: true };
case AUTH_DELETE:
return { ...initialState };
case GREET_USER:
return { ...state, username: action.payload}
default:
return state;
}
Expand Down
17 changes: 17 additions & 0 deletions src/modules/common/components/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React, { useState, useEffect } from "react"


const Counter = () => {
const [count, setCount] = useState(0)
// const [apiDate, setApiData] = useState({ loading: false, value: null})
useEffect(()=> {document.title = `${count} hits`;})
// ... which is componentDidMount & componentDidUpdate
// if you return within useEffect, it is the same as componentWillUnmount
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
)
}
export default Counter

0 comments on commit ac1f3d4

Please sign in to comment.