Skip to content
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

Self PR #1

Open
wants to merge 37 commits into
base: self-pr
Choose a base branch
from
Open

Self PR #1

wants to merge 37 commits into from

Conversation

rodriguezmanu
Copy link
Owner

@rodriguezmanu rodriguezmanu commented Sep 14, 2018

Self Pr

@@ -0,0 +1,51 @@
import React from 'react';
Copy link
Owner Author

Choose a reason for hiding this comment

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

public routes in order to prevent auth user redirect to login and signup page

/**
* Get User Handler
*/
getUserHandler = () => {
Copy link
Owner Author

Choose a reason for hiding this comment

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

get user information to edit from url query params

role: { value: role },
} = e.target;
const match = this.getUrlParams();
const checkRoleChange = user.data.id === Number(match.params.id) && user.data.role !== role;
Copy link
Owner Author

Choose a reason for hiding this comment

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

check if the current user change it role, in order to redirect to proper action

@@ -0,0 +1,109 @@
.pagination {
Copy link
Owner Author

Choose a reason for hiding this comment

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

some styling for pagination

@@ -0,0 +1,3 @@
import { createBrowserHistory } from 'history';
Copy link
Owner Author

Choose a reason for hiding this comment

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

history in order to work with connect router redux redirect methods

import auth from './auth.reducer';
import users from './users.reducer';

const rootReducer = combineReducers({
Copy link
Owner Author

Choose a reason for hiding this comment

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

Used separated reducer and mix them

@@ -0,0 +1,25 @@
import React from 'react';
Copy link
Owner Author

Choose a reason for hiding this comment

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

Separated routes file

<PrivateRoute path="/users/:id" component={EditUser} allowedRoles={adminRoles} />
<PrivateRoute path="/users" component={Users} allowedRoles={adminRoles} />
<PrivateRoute path="/home" component={Home} allowedRoles={userRoles} />
<Redirect to="/login" />
Copy link
Owner Author

Choose a reason for hiding this comment

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

any other path goes to login page

const store = createStore(
connectRouter(history)(rootReducer),
preloadedState,
composeWithDevTools(applyMiddleware(routerMiddleware(history), thunk, api, logger))
Copy link
Owner Author

Choose a reason for hiding this comment

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

for dev env, used logger

@@ -0,0 +1,5 @@
if (process.env.NODE_ENV === 'production') {
Copy link
Owner Author

Choose a reason for hiding this comment

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

used 2 env in order to change different options between them

const store = createStore(
connectRouter(history)(rootReducer),
preloadedState,
compose(applyMiddleware(routerMiddleware(history), thunk, api))
Copy link
Owner Author

Choose a reason for hiding this comment

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

logger is not used to in prod env


const initialState = {
isFetching: false,
};
Copy link
Owner Author

Choose a reason for hiding this comment

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

created initial state

let filtered;
switch (action.type) {
case GET_USERS_REQUEST:
return {
Copy link
Owner Author

Choose a reason for hiding this comment

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

every action return the state, action and isfetching in order to handle render method data

isFetching: false,
};
case LOGOUT_SUCCESS:
localStorage.removeItem('token');
Copy link
Owner Author

Choose a reason for hiding this comment

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

logout remove the token and return an empty user

isFetching: true,
};
case LOGIN_SUCCESS:
localStorage.setItem('token', JSON.stringify(action.data.access_token));
Copy link
Owner Author

Choose a reason for hiding this comment

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

login set a new jwt token

return {
...state,
data: action.data,
isAuth: true,
Copy link
Owner Author

Choose a reason for hiding this comment

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

isauth works to control private routes and menus


const initialState = {
isFetching: false,
isAuth: false,
Copy link
Owner Author

Choose a reason for hiding this comment

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

by default the user is not auth

* @param {Object} payload
* @return {Boolean}
*/
const validate = payload => {
Copy link
Owner Author

Choose a reason for hiding this comment

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

validation form by field, using regex and adding a complete array of error to work with errormessage component

* Get headers
* @return {Object} headers
*/
const getHeaders = () => {
Copy link
Owner Author

Choose a reason for hiding this comment

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

get headers method in order to apply auth jwt token for auth routes

* @param {Object} actionWith
* @param {Object} store
*/
export const api = async (callAPI, next, actionWith, store) => {
Copy link
Owner Author

Choose a reason for hiding this comment

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

used async - await feature, more readably

type: successType,
})
);
if (nextType) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

in order to dispatch next action defined in action file

</ul>
</div>
)}
{this.renderPaginate()}
Copy link
Owner Author

Choose a reason for hiding this comment

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

separate render pagination for better understanding


return (
<div className="m-2 text-center">
<div className="m-2 text-center">
Copy link
Owner Author

Choose a reason for hiding this comment

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

user filter

<div>
{users.filtered && (
<div>
<ul className="list-unstyled">
Copy link
Owner Author

Choose a reason for hiding this comment

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

user list

>
Delete User
</button>
<NavLink className="btn btn-primary" to={`/users/${item.id}`}>
Copy link
Owner Author

Choose a reason for hiding this comment

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

redirect to edit user with query param id


if (users.count <= limitUsers) {
return (
<div className="m-2">
Copy link
Owner Author

Choose a reason for hiding this comment

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

pagination external component

const { getUsers } = this.props;

getUsers(data.selected + 1);
window.scrollTo(0, 0);
Copy link
Owner Author

Choose a reason for hiding this comment

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

every time go to next page in pagination scroll to up

/**
* Filter user list action
*/
filterUserList = event => {
Copy link
Owner Author

Choose a reason for hiding this comment

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

Filter user and handle with pagination

</NavLink>
</div>
{user.data &&
!user.isFetching && (
Copy link
Owner Author

Choose a reason for hiding this comment

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

waiting for redux and api return data

@@ -0,0 +1,13 @@
import React from 'react';
Copy link
Owner Author

Choose a reason for hiding this comment

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

dummy home page to work with different roles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant