Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/components/App/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,37 @@
.App .App-Content .ItemInput {
width: 100%;
}

.User-Panel {
display: flex;
flex-direction: column;
align-items: center;
padding: 24px;
background: #50a3a2;
background: -webkit-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
background: -moz-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
background: -o-linear-gradient(top left, #50a3a2 0%, #53e3a6 100%);
background: linear-gradient(to bottom right, #50a3a2 0%, #53e3a6 100%);
}

.User-Panel .Login {
outline: 0;
background-color: white;
border: 0;
padding: 10px 15px;
color: #53e3a6;
border-radius: 3px;
width: 250px;
cursor: pointer;
font-size: 18px;
transition-duration: 0.25s;
}

.User-Panel .Login:hover {
background-color: #000000;
}

.User-Panel .Greeting {
margin-top: 20px;
font-size: 24px;
}
8 changes: 7 additions & 1 deletion src/components/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import { List, ListItem } from '@material-ui/core';
import Header from '../Header';
import ItemInput from '../ItemInput';
import './App.css';
import Login from '../Login';
import Greeting from '../Greeting';

const App = ({
items,
onButtonClick
}) => (
<div className="App">
<Header />
<div className='User-Panel'>
<Login/>
<Greeting/>
</div>
<main className="App-Content">
<List className="List">{
items.map((item, i) => <ListItem key={i}>{item}</ListItem>)
Expand All @@ -21,7 +27,7 @@ const App = ({
);

const mapStateToProps = (state) => ({
items: state.items
items: state.appReducer.items
});

export default connect(
Expand Down
18 changes: 18 additions & 0 deletions src/components/Greeting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { useSelector } from "react-redux";

const Greeting = ({
userName,
loggedIn
}) => {

userName = useSelector(state => state.loginReducer.userName);
loggedIn = useSelector(state => state.loginReducer.loggedIn);
return (
<div className="Greeting">
{loggedIn ? `Hello ${userName}. Wassup?` : `Hmm, I don't know you ${userName}. Would you mind to log in?`}
</div>
)
};

export default Greeting;
17 changes: 17 additions & 0 deletions src/components/Login/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const actions = {
login: 'LOGIN',
logOut: 'LOGOUT',
};

export const login = () => ({
type: actions.login
});

export const logOut = () => ({
type: actions.logOut
});

export default {
login,
logOut
};
23 changes: 23 additions & 0 deletions src/components/Login/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { useSelector, useDispatch } from "react-redux";
import { login, logOut } from './actions'

const Login = ({
isLoggedIn
}) => {
isLoggedIn = useSelector(state => state.loginReducer.loggedIn);
const dispatch = useDispatch();

const changeStatus = () => {
isLoggedIn ? dispatch(logOut()) : dispatch(login())
};

return (
<button onClick={() => changeStatus()}
className="Login">
{ isLoggedIn ? 'Logout' : 'Login'}
</button>
)
};

export default Login;
22 changes: 22 additions & 0 deletions src/components/Login/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { actions } from './actions';

const initialState = {
loggedIn: false,
userName: 'guest'
};

export default (state = initialState, action) => {
switch (action.type) {
case actions.login:
return {
loggedIn: true,
userName: 'not guest'
};
case actions.logOut:
return {
loggedIn: false
};
default:
return state;
}
};
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createStore } from 'redux';
import 'typeface-roboto';
import './index.css';
import App from './components/App';
import appReducer from './components/App/reducer';
import rootReducer from './rootReducer';

let devAdditions;
if (module.hot) {
Expand All @@ -17,9 +17,10 @@ if ('__REDUX_DEVTOOLS_EXTENSION__' in window) {
}

const store = createStore(
appReducer,
rootReducer,
devAdditions
);
console.log(store.getState())

ReactDOM.render(
<React.StrictMode>
Expand Down
9 changes: 9 additions & 0 deletions src/rootReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { combineReducers } from 'redux';
import appReducer from './components/App/reducer';
Copy link
Copy Markdown
Owner

@haensl haensl Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good Zozo :)
The only minor thing I would like to let you know: don't use the name Reducer in the state. If you import the reducer with the name of what it represents, your state will look nicer ;)

Example:

import app from './components/App/reducer';
import login from './components/Login/reducer';

export default combineReducers({
  app,
  login
});

This way, in the other components where you use state, it just reads nicer - e.g.

isLoggedIn =  useSelector(state => state.loginReducer.loggedIn);

becomes

isLoggedIn =  useSelector(state => state.login.loggedIn);

So, you did veeeery good! 👍 👍 👍

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @haensl
I totally agree with your recommendation
Also I will add more stuff, to practice!

import loginReducer from './components/Login/reducer';

export default combineReducers({
appReducer,
loginReducer
});