Ver código fonte

get, delete users

sveta 5 anos atrás
pai
commit
a9775a9cc4

+ 16 - 0
src/actions/adminActions/deleteUser/index.js

@@ -0,0 +1,16 @@
+import * as types from '../../../constants';
+
+export const deleteUserRequest = payload => ({
+    type: types.DELETE_USER_REQUEST,
+    payload
+})
+
+export const deleteUserRequestSucces = payload => ({
+    type: types.DELETE_USER_REQUEST_SUCCESS,
+    payload
+})
+
+export const deleteUserRequestFailure = error => ({
+    type: types.DELETE_USER_REQUEST_FAILURE,
+    error
+})

+ 5 - 5
src/actions/adminActions/index.js

@@ -1,16 +1,16 @@
-import * as types from '../../constants';
+import * as types from '../../../constants';
 
 export const userGetRequest = payload => ({
-    type: types.USERS_GET_REQUEST,
+    type: types.USER_GET_REQUEST,
     payload
 })
 
 export const userGetRequestSucces = payload => ({
-    type: types.USERS_GET_REQUEST_SUCCESS,
+    type: types.USER_GET_REQUEST_SUCCESS,
     payload
 })
 
 export const userGetRequestFailure = error => ({
-    type: types.USERS_GET_REQUEST_FAILURE,
+    type: types.USER_GET_REQUEST_FAILURE,
     error
-})
+})

+ 4 - 6
src/actions/auth/users/users.js

@@ -1,10 +1,7 @@
 import * as types from '../../../constants';
 
-// all actions for signing up
-
-export const usersGetRequest= payload => ({
+export const usersGetRequest = () => ({
     type: types.USERS_GET_REQUEST,
-    payload
 })
 
 export const usersGetRequestSucces = payload => ({
@@ -13,6 +10,7 @@ export const usersGetRequestSucces = payload => ({
 })
 
 export const usersGetRequestFailure = error => ({
-    type: types.USERS_GET_REQUEST_SUCCESS_FAILURE ,
+    type: types.USERS_GET_REQUEST_FAILURE,
     error
-})
+})
+

+ 1 - 2
src/actions/auth/index.js

@@ -1,11 +1,10 @@
 import * as signInActions from './signIn';
 import * as signUpActions from './signUp';
 import * as signOutActions from './signOut';
-import * as usersGetActions from './signOut';
+
 
 export default {
     ...signInActions,
     ...signUpActions,
     ...signOutActions,
-    ...usersGetActions
 }

+ 6 - 0
src/components/common/protectedRoute/config.js

@@ -2,6 +2,7 @@ import React from 'react';
 import * as routes from './../../../constants/routes';
 import { lazy } from 'react';
 
+const DeleteUsers = lazy(()=>import('../../user-components/admin-components/createDeleteUser/index'))
 const LandingPage =    lazy(() => import('./../../public-components/landingPage'));
 const HomePage =       lazy(() => import('./../../user-components/homePage'));
 const SignInPage =     lazy(() => import('./../../../containers/auth/SignInPage'));
@@ -48,6 +49,11 @@ export default [
         access: 'user-only',
         component: CardsPage
     },
+    {
+        path: routes.DELETE_USER ,
+        access: 'admin-only',
+        component: DeleteUsers
+    },
     {
         access: 'public',
         component: NotFound

+ 43 - 0
src/components/user-components/admin-components/createDeleteUser/deleteUser/index.js

@@ -0,0 +1,43 @@
+import React from 'react';
+import { connect } from 'react-redux';
+import { Field, reduxForm } from 'redux-form';
+import { bindActionCreators } from 'redux'
+
+import formInput from '../../../../common/formInput/index';
+import {userGetRequest} from '../../../../../actions/adminActions/getUser';
+import {deleteUserRequest} from '../../../../../actions/adminActions/deleteUser'
+
+
+
+class DeleteUser extends React.Component {
+    onClick =() =>{
+        const { deleteUserRequest, data:{login}} = this.props;
+        console.log(login)
+        deleteUserRequest({
+            login
+        })
+    }
+
+    // handleClick = () => {
+    //     this.setState((prevState) => ({ clicked: !prevState.clicked }));
+    // }
+    render() {
+    const {data} =this.props
+    console.log(data)
+        return (<div>
+                    {data.login}
+                <button onClick ={this.onClick}>
+                    Delete
+                    </button>
+        </div>
+        )
+    }
+}
+
+
+
+
+const mapDispatchToProps = dispatch => bindActionCreators({ deleteUserRequest }, dispatch);
+
+
+export default connect( null,mapDispatchToProps)(DeleteUser)

+ 61 - 0
src/components/user-components/admin-components/createDeleteUser/formGetUsers/index.js

@@ -0,0 +1,61 @@
+import React from 'react';
+import { connect } from 'react-redux';
+import { Field, reduxForm } from 'redux-form';
+import { bindActionCreators } from 'redux'
+
+import formInput from '../../../../common/formInput/index';
+import {userGetRequest} from '../../../../../actions/adminActions/getUser'
+import DeleteUser from '../deleteUser/index'
+
+
+
+class FormGetUsers extends React.Component {
+    submit = ({ login}) => {
+        console.log('handleSubmit->submit', this.props)
+        const { userGetRequest }  = this.props;
+
+        console.log('hghjgjhg',login);
+
+        userGetRequest({
+            login
+        })
+    }
+
+    // handleClick = () => {
+    //     this.setState((prevState) => ({ clicked: !prevState.clicked }));
+    // }
+    render() {
+        const { handleSubmit, getUser:{data} } = this.props
+        const {submit} =this
+        console.log(data)
+        return (<div>
+            <form className="page" onSubmit = {handleSubmit(submit)}>
+                <Field name="login" type="email" placeholder="Enter new login" component={formInput} />
+                <button >Search</button>
+            </form >
+            {
+                data !== null?
+                       ( <div>
+                <DeleteUser data ={data}></DeleteUser>
+                        </div>
+                       ):
+                       <div>dddd</div>
+            }
+        </div>
+        )
+    }
+}
+
+const
+    mapStateToProps = state => ({
+        users: state.users,
+        getUser:state.getUser
+    });
+
+
+const mapDispatchToProps = dispatch => bindActionCreators({userGetRequest }, dispatch);
+
+
+export default connect(mapStateToProps, mapDispatchToProps)(reduxForm({
+    form: "FormGetUsers",
+})(FormGetUsers))

+ 36 - 12
src/components/user-components/admin-components/createDeleteUser/index.js

@@ -1,29 +1,53 @@
 import React from 'react';
 import { connect } from 'react-redux';
 import { Field, reduxForm } from 'redux-form';
+import { bindActionCreators } from 'redux'
 
 import formInput from '../../..//common/formInput';
- submit=()=>{
-     
- }
+import { usersGetRequest } from '../../../../actions/adminActions/getUsers/index'
+import FormGetUsers from './formGetUsers/index'
 
-class createDeleteUser extends React.Component {
 
+class createDeleteUser extends React.Component {
+    componentDidMount() {
+        const { usersGetRequest } = this.props;
+        usersGetRequest();
+    }
     render() {
-        const {  handleSubmit  } = this.props;
+        const { handleSubmit, users:{data} } = this.props;
+        console.log(data)
         return (
-            <form  className="page">
-                <Field name="login"  type="email" placeholder="Enter new login" component={formInput} />
-                <button >Search</button>
-            </form >
+            <div className="page page--bottom-only profile-page">
+                <section className="container section section--about">
+            <FormGetUsers></FormGetUsers>
+        </section>
+        <section className="container section section--about">
+            {data&&data.map(str=>
+            <div className="container section section--about">
+            <ul >
+                <h5>LOGIN</h5>
+                {str.login}
+                </ul>
+            <ul >
+                <h5>PASSWORD</h5>
+            {str.password}
+                </ul>
+                </div>)}
+            </section>
+        </div>
         )
     }
 }
 
+const
+    mapStateToProps = state => ({
+        users: state.users
+    });
+
 
-const mapDispatchToProps = dispatch => bindActionCreators({ userGetRequest }, dispatch);
+const mapDispatchToProps = dispatch => bindActionCreators({ usersGetRequest }, dispatch);
 
 
-export default connect(mapDispatchToProps)(reduxForm({
-    form: "createDeleteUser",   
+export default connect(mapStateToProps, mapDispatchToProps)(reduxForm({
+    form: "createDeleteUser",
 })(createDeleteUser))

+ 1 - 1
src/components/user-components/profilePage/LoginFild/index.js

@@ -37,7 +37,7 @@ export default class ChangeLogin extends React.Component {
                 ?
                 (
                     <React.Fragment>
-                        <div>{children}</div>
+                        <div >{children}</div>
                         <button className="link link--btn right"onClick={handleClick}>Change Email</button>
                     </React.Fragment>
                 )

+ 5 - 4
src/components/user-components/profilePage/index.js

@@ -49,9 +49,9 @@ class profilePage extends React.Component {
 
 
     render() {
-        const { user:{data}, userChangeRequest,passwordChangeRequest } = this.props;
+        const { user:{data},users, userChangeRequest,passwordChangeRequest } = this.props;
 
-        // console.log('User', user);
+        console.log('User', users);
         // console.log('Props', this.props);
         // console.log('State', this.state);
 
@@ -59,7 +59,7 @@ class profilePage extends React.Component {
             <div className="page page--bottom-only profile-page">
                 <section className="container section section--about">
                     {/* <h2 className="section__element section__element--header">{data.login}</h2> */}
-        
+
                     <div className="sedtion__element section__element--login">
                         <h3>Email</h3>
                         {/* <p>{user.login}<button className="link link--btn right" >Change login</button></p> */}
@@ -102,7 +102,8 @@ class profilePage extends React.Component {
 
 const
     mapStateToProps = state => ({
-        user: state.user
+        user: state.user,
+        users:state.users
     });
 
 const mapDispatchToProps = dispatch => bindActionCreators({ userChangeRequest, passwordChangeRequest }, dispatch);

+ 10 - 1
src/constants/index.js

@@ -15,7 +15,7 @@ export const SIGN_OUT = 'SIGN_OUT';
 
 export const USERS_GET_REQUEST = 'USERS_GET_REQUEST';
 export const USERS_GET_REQUEST_SUCCESS = 'USERS_GET_REQUEST_SUCCESS';
-export const USERS_GET_REQUEST_SUCCESS_FAILURE = 'USERS_GET_REQUEST_FAILURE';
+export const USERS_GET_REQUEST_FAILURE = 'USERS_GET_REQUEST_FAILURE';
 
 export const TOKEN_AUTH = 'TOKEN_AUTH';
 
@@ -33,3 +33,12 @@ export const GET_CARDS_URL = 'https://quiz.maxcrc.de/api/v1/quiz';
 export const GET_CARDS = 'GET_CARD';
 export const GET_CARDS_SUCCESS = 'GET_CARDS_SUCCESS';
 export const GET_CARDS_FAILURE = 'GET_CARDS_FAILURE';
+
+export const USER_GET ='http://quiz.maxcrc.de/api/v1/user?'
+export const USER_GET_REQUEST = 'USER_GET_REQUEST';
+export const USER_GET_REQUEST_SUCCESS = 'USER_GET_REQUEST_SUCCESS';
+export const USER_GET_REQUEST_FAILURE = 'USER_GET_REQUEST_FAILURE';
+
+export const DELETE_USER_REQUEST = 'DELETE_USER_REQUEST';
+export const DELETE_USER_REQUEST_SUCCESS = 'DELETE_USER_REQUEST_SUCCESS';
+export const DELETE_USER_REQUEST_FAILURE = 'DELETE_USER_REQUEST_FAILURE';

+ 30 - 0
src/reducers/getUser/index.js

@@ -0,0 +1,30 @@
+import * as types from "../../constants/index";
+import initialState from '../initialState';
+
+export default function getUser(state = initialState.getUser, {type, payload: data, error }) {
+    switch (type) {
+        case types.USER_GET_REQUEST: {
+            return {
+                ...state,
+                isFetching: true
+            }
+        }
+        case types.USER_GET_REQUEST_SUCCESS: {
+            return {
+                ...state,
+                isFetching: false,
+                data
+            }
+        }
+        case types.USER_GET_REQUEST_FAILURE: {
+            return {
+                ...state,
+                isFetching: false,
+                error
+            }
+        }
+        default: {
+            return state
+        }
+    }
+}

+ 5 - 1
src/reducers/index.js

@@ -6,6 +6,8 @@ import user from './user';
 import changeEmail from './userFields/changeEmail/index';
 import changePassword from './userFields/changePassword/index'
 import cards from './cards';
+import users from './users'
+import getUser from './getUser'
 
 import { reducer as form } from 'redux-form';
 
@@ -16,7 +18,9 @@ const combinedReducers = combineReducers({
     user,
     changeEmail,
     changePassword,
-    cards
+    cards,
+    users,
+    getUser
 })
 
 export default combinedReducers;

+ 10 - 0
src/reducers/initialState/index.js

@@ -14,5 +14,15 @@ export default {
         data: null,
         error: null,
         isFetching: null
+    },
+    users: {
+        data: null,
+        error: null,
+        isFetching: null
+    },
+    getUser:{
+        data:null,
+        error: null,
+        isFetching: null
     }
 }

+ 30 - 0
src/reducers/users/index.js

@@ -0,0 +1,30 @@
+import * as types from "../../constants/index";
+import initialState from '../initialState';
+
+export default function getUsers(state = initialState.users, {type, payload: data, error }) {
+    switch (type) {
+        case types.USERS_GET_REQUEST: {
+            return {
+                ...state,
+                isFetching: true
+            }
+        }
+        case types.USERS_GET_REQUEST_SUCCESS: {
+            return {
+                ...state,
+                isFetching: false,
+                data
+            }
+        }
+        case types.USERS_GET_REQUEST_FAILURE: {
+            return {
+                ...state,
+                isFetching: false,
+                error
+            }
+        }
+        default: {
+            return state
+        }
+    }
+}

+ 1 - 2
src/saga/auth/index.js

@@ -3,12 +3,11 @@ import * as types from "./../../constants"
 
 import signIn from './signIn';
 import signUp from './signUp';
-import getUsers from './users/users';
+
 
 export default function*() {
     yield takeEvery(types.SIGN_IN_REQUEST, signIn);
     yield takeEvery(types.SIGN_UP_REQUEST, signUp);
-    yield takeEvery(types.USERS_GET_REQUEST, getUsers);
 }
 
 

+ 0 - 19
src/saga/auth/users/users.js

@@ -1,19 +0,0 @@
-import { put, call } from "redux-saga/effects";
-import * as actions from './../../../actions/auth/users/users'
-import axios from 'axios';
-
-
-
-export default function* ({ payload: requestBody }) {
-    try {
-        const payload = yield call(() =>
-            axios.get(`https://quiz.maxcrc.de/api/v1/user`)
-                .then(({ data }) => data)
-        );
-        
-        yield put(actions.usersGetRequestSucces(payload));
-    }
-    catch ({ message }) {
-        yield put(actions.usersGetRequestFailure(message))
-    }
-}

+ 18 - 0
src/saga/userFields/deleteUser/index.js

@@ -0,0 +1,18 @@
+import { put, call } from "redux-saga/effects";
+import * as actions from './../../../actions/adminActions/deleteUser'
+import axios from 'axios';
+import { USER_GET } from '../../../constants/index'
+
+export default function* ({payload:{login}}) {
+    try {
+        console.log("payload inside the saga", login);
+        const payloads = yield call(() =>
+            axios.delete(`${USER_GET}login=${login}`)
+                .then((data) => data)
+        );
+        yield put(actions.deleteUserRequestSucces(payloads));
+    }
+    catch ({ message }) {
+        yield put(actions.deleteUserRequestFailure(message))
+    }
+}

+ 18 - 0
src/saga/userFields/getUser/index.js

@@ -0,0 +1,18 @@
+import { put, call } from "redux-saga/effects";
+import * as actions from '../../../actions/adminActions/getUser/index'
+import axios from 'axios';
+import { USER_GET } from '../../../constants/index'
+
+export default function* ({payload:{login}}) {
+    try {
+        console.log("payload inside the saga",login);
+        const payloads = yield call(() =>
+            axios.get(`${USER_GET}login=${login}`)
+                .then(({data: [payload]}) => payload)
+        );
+        yield put(actions.userGetRequestSucces(payloads));
+    }
+    catch ({ message }) {
+        yield put(actions.userGetRequestFailure(message))
+    }
+}

+ 4 - 4
src/saga/userFields/createDeleteUser/index.js

@@ -1,5 +1,5 @@
 import { put, call } from "redux-saga/effects";
-import * as actions from './../../../actions/adminActions/index'
+import * as actions from '../../../actions/adminActions/getUsers/index'
 import axios from 'axios';
 import { SIGN_UP_URL } from '../../../constants/index'
 
@@ -8,11 +8,11 @@ export default function* (payload) {
         console.log("payload inside the saga", payload);
         const payloads = yield call(() =>
             axios.get(SIGN_UP_URL)
-                .then(({ data }) => data)
+                .then(({data}) => data)
         );
-        yield put(actions.userGetRequestSucces(payloads));
+        yield put(actions.usersGetRequestSucces(payloads));
     }
     catch ({ message }) {
-        yield put(actions.userGetRequestFailure(message))
+        yield put(actions.usersGetRequestFailure(message))
     }
 }

+ 7 - 2
src/saga/userFields/index.js

@@ -3,9 +3,14 @@ import * as types from "./../../constants"
 
 import changeEmail from './changeEmail/index';
 import changePassword from "./changePassword";
-
+import getUsers from './getUsers'
+import getUser from './getUser'
+import deleteUser from './deleteUser'
 
 export default function*() {
     yield takeEvery(types.USERS_CHANGE_REQUEST, changeEmail);
-    yield takeEvery(types.PASSWORD_CHANGE_REQUEST, changePassword)
+    yield takeEvery(types.PASSWORD_CHANGE_REQUEST, changePassword);
+    yield takeEvery(types.USERS_GET_REQUEST, getUsers);
+    yield takeEvery(types.USER_GET_REQUEST, getUser);
+    yield takeEvery(types.DELETE_USER_REQUEST, deleteUser);
 }

+ 1 - 1
src/state/index.js

@@ -14,7 +14,7 @@ const logger = createLogger({
 const store = createStore(
     combinedReducers,
     initialState,
-    applyMiddleware(sagaMiddleware)
+    applyMiddleware(sagaMiddleware, logger)
 );
 export default store;
 

+ 4 - 0
src/styles/components/_profilePage.scss

@@ -8,6 +8,10 @@
         flex-basis: 400px;
         margin: 10px;
         padding: 10px;
+
+        h3{
+            margin: 25px 0
+        }
         &__element {
             &--image {
                 border: 3px solid $color-lightsteelblue;