Skip to content

Commit

Permalink
#2 setup redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
maruf-cefalo committed May 31, 2020
1 parent 6886d45 commit 3d1cd23
Show file tree
Hide file tree
Showing 17 changed files with 502 additions and 202 deletions.
509 changes: 345 additions & 164 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
"dependencies": {
"@material-ui/core": "latest",
"@material-ui/icons": "^4.9.1",
"axios": "^0.19.2",
"react": "latest",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "latest"
"react-scripts": "latest",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
},
"browserslist": {
"production": [
Expand Down
6 changes: 4 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//Library
import React,{Component} from 'react';
import Main from './components/MainComponent';
import {BrowserRouter} from 'react-router-dom';

//Our Code
import Main from './components/MainComponent';
class App extends Component {

render(){
Expand All @@ -11,7 +13,7 @@ class App extends Component {
</BrowserRouter>
);
}

}

export default App;
22 changes: 22 additions & 0 deletions src/actions/authActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const USER_LOADED = 'USER_LOADED';
export const LOGIN_SUCCESS = 'LOGIN_SUCCESS'

export function authenticateUser (user) {
return{
type: USER_LOADED,
user
}
}

export function handleAuthenticateUser (user) {
return (dispatch) => {

}
}

export function loginUser (user) {
return {
type: LOGIN_SUCCESS,
user,
}
}
9 changes: 9 additions & 0 deletions src/actions/errorActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const GET_ERRORS = 'GET_ERRORS'
export const CLEAR_ERRORS = 'CLEAR_ERRORS'

export function getErrors (errors) {
return {
type: GET_ERRORS,
errors,
}
}
Empty file added src/actions/userActions.js
Empty file.
1 change: 0 additions & 1 deletion src/components/MainComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import SignIn from './SignIn';

class Main extends Component {


render() {

return (
Expand Down
52 changes: 27 additions & 25 deletions src/components/SignIn.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Link from '@material-ui/core/Link';
import Grid from '@material-ui/core/Grid';
import Box from '@material-ui/core/Box';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';
import React from 'react'
import Avatar from '@material-ui/core/Avatar'
import Button from '@material-ui/core/Button'
import CssBaseline from '@material-ui/core/CssBaseline'
import TextField from '@material-ui/core/TextField'
import FormControlLabel from '@material-ui/core/FormControlLabel'
import Checkbox from '@material-ui/core/Checkbox'
import Link from '@material-ui/core/Link'
import Grid from '@material-ui/core/Grid'
import Box from '@material-ui/core/Box'
import LockOutlinedIcon from '@material-ui/icons/LockOutlined'
import Typography from '@material-ui/core/Typography'
import { makeStyles } from '@material-ui/core/styles'
import Container from '@material-ui/core/Container'

function Copyright() {
function Copyright () {
return (
<Typography variant="body2" color="textSecondary" align="center">
{'Copyright © '}
Expand All @@ -23,7 +23,7 @@ function Copyright() {
{new Date().getFullYear()}
{'.'}
</Typography>
);
)
}

const useStyles = makeStyles((theme) => ({
Expand All @@ -44,17 +44,17 @@ const useStyles = makeStyles((theme) => ({
submit: {
margin: theme.spacing(3, 0, 2),
},
}));
}))

export default function SignIn() {
const classes = useStyles();
const SignIn = () => {
const classes = useStyles()

return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<CssBaseline/>
<div className={classes.paper}>
<Avatar className={classes.avatar}>
<LockOutlinedIcon />
<LockOutlinedIcon/>
</Avatar>
<Typography component="h1" variant="h5">
Sign in
Expand Down Expand Up @@ -83,7 +83,7 @@ export default function SignIn() {
autoComplete="current-password"
/>
<FormControlLabel
control={<Checkbox value="remember" color="primary" />}
control={<Checkbox value="remember" color="primary"/>}
label="Remember me"
/>
<Button
Expand All @@ -103,15 +103,17 @@ export default function SignIn() {
</Grid>
<Grid item>
<Link href="#" variant="body2">
{"Don't have an account? Sign Up"}
{'Don\'t have an account? Sign Up'}
</Link>
</Grid>
</Grid>
</form>
</div>
<Box mt={8}>
<Copyright />
<Copyright/>
</Box>
</Container>
);
)
}

export default SignIn
25 changes: 16 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import React from 'react';
import ReactDOM from 'react-dom';
import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from '@material-ui/core/styles';
import App from './App';
import theme from './theme';
//Library Import
import React from 'react'
import ReactDOM from 'react-dom'
import CssBaseline from '@material-ui/core/CssBaseline'
import { ThemeProvider } from '@material-ui/core/styles'
import theme from './theme'
import store from './store'
import { Provider } from 'react-redux'

//Our Code Import
import App from './App'

ReactDOM.render(
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
<App />
<CssBaseline/>
<Provider store={store}>
<App/>
</Provider>
</ThemeProvider>,
document.querySelector('#root'),
);
)
8 changes: 8 additions & 0 deletions src/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import logger from './logger';
import thunk from 'redux-thunk';
import {applyMiddleware} from 'redux';

export default applyMiddleware(
thunk,
logger,
);
11 changes: 11 additions & 0 deletions src/middlewares/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const logger = (store) => (next) => (action) => {
console.group(action.type);
console.log(action);
const returnValue = next(action);
console.log(`The new state: ${JSON.stringify(store.getState(), '', ' ')}`);
console.groupEnd();

return returnValue;
};

export default logger;
27 changes: 27 additions & 0 deletions src/reducers/authReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { USER_LOADED } from '../actions/authActions'
import { LOGIN_SUCCESS} from '../actions/userActions'

const intialState = {
token: localStorage.getItem('token'),
isAuthenticated: null,
user: null
}
export default function(state = intialState, action) {
switch (action.type) {
case USER_LOADED:
return {
...state,
isAuthenticated: true,
user: action.payload
}
case LOGIN_SUCCESS:
return {
...state,
isAuthenticated: true,
...action.payload
}
default:
return state;

}
}
13 changes: 13 additions & 0 deletions src/reducers/errorReducers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GET_ERRORS, CLEAR_ERRORS } from '../actions/errorActions'

export default function(state = {}, action) {
switch (action.type) {
case GET_ERRORS:
return{
msg: action.payload.msg,
status: action.payload.status
}
default:
return state
}
}
6 changes: 6 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux'
import authReducers from './authReducers'

export default combineReducers({
auth: authReducers
})
Empty file added src/reducers/userReducers.js
Empty file.
9 changes: 9 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createStore, compose } from 'redux'
import reducers from './reducers'
import middlewares from './middlewares'

const store = createStore(reducers,
compose(middlewares, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()),
)

export default store
Empty file added src/utils/api.js
Empty file.

0 comments on commit 3d1cd23

Please sign in to comment.