Browse Source

finished change user in admin

Boris K 5 years ago
parent
commit
6d2d492100

+ 5 - 3
src/actions/user.js

@@ -6,9 +6,12 @@ const URL = "https://api-clinics.herokuapp.com/api/v1/users/";
 export const changeFindUserInput = payload => ({
     type:types.CHANGE_INPUT_VALUE_FIND_USER,
     payload
-})
-
+});
 
+export const changeInputValueUserForm = payload => ({
+    type:types.CHANGE_INPUT_VALUE_USER_FORM,
+    payload
+});
 
 const findUserRequest = payload => ({
     type: types.FIND_USER_REQUEST,
@@ -33,7 +36,6 @@ export const findUser = (payload) => dispatch => {
         .then(res => res.json())
         .then(res => {
             dispatch(findUserRequestSuccess(res));
-            console.log(res)
         })
         .catch(err => dispatch(findUserRequestFail(err)));
 };

+ 1 - 0
src/actionsTypes/actionsTypes.js

@@ -2,6 +2,7 @@ export const FIND_USER_REQUEST = "FIND_USER_REQUEST";
 export const FIND_USER_REQUEST_SUCCESS = "FIND_USER_REQUEST_SUCCESS";
 export const FIND_USER_REQUEST_FAIL = "FIND_USER_REQUEST_FAIL";
 export const CHANGE_INPUT_VALUE_FIND_USER = "CHANGE_INPUT_VALUE_FIND_USER";
+export const CHANGE_INPUT_VALUE_USER_FORM= "CHANGE_INPUT_VALUE_USER_FORM";
 
 export const GET_DOCTORS_REQUEST = "GET_DOCTORS_REQUEST";
 export const GET_DOCTORS_REQUEST_SUCCESS = "GET_DOCTORS_REQUEST_SUCCESS";

+ 7 - 3
src/components/Admin/Admin.js

@@ -21,7 +21,8 @@ import {
 import {
     changeFindUserInput,
     findUser,
-    deleteUser
+    deleteUser,
+    changeInputValueUserForm
 } from "../../actions/user"
 
 import Shedule from './Shedule'
@@ -62,7 +63,8 @@ export class Admin extends React.Component {
             findUser,
             deleteUser,
             userError,
-            changeUserForm
+            changeUserForm,
+            changeInputValueUserForm
         } = this.props;
 
         return (
@@ -112,6 +114,7 @@ export class Admin extends React.Component {
                             deleteUser={deleteUser}
                             error={userError}
                             changeUserForm={changeUserForm}
+                            changeInputValueUserForm={changeInputValueUserForm}
                         />} />
                     </Switch>
                 </div>
@@ -154,7 +157,8 @@ const mapDispatchToProps = {
     changeSpecialityArray,
     changeFindUserInput,
     findUser,
-    deleteUser
+    deleteUser,
+    changeInputValueUserForm
 };
 
 export default connect (mapStateToProps,mapDispatchToProps)(Admin)

+ 31 - 11
src/components/Admin/tempp.js

@@ -19,6 +19,15 @@ export default class Tempp extends React.Component {
         }
     };
 
+    changeUser = (e) => {
+        const obj = {}
+        e.preventDefault()
+        this.props.changeUserForm.map(el => {
+            obj[el.name] = el.type === 'radio' ? el.checked ? el.value : !el.value : el.value
+        });
+        console.log(obj)
+    };
+
     render(){
         const {
             user,
@@ -26,26 +35,37 @@ export default class Tempp extends React.Component {
             changeFindUserInput,
             findUser,
             deleteUser,
+            changeInputValueUserForm,
             changeUserForm,
             error
         } = this.props;
+        console.log(this.props)
         return (
            <div>
-                <input type='text' name='find_user' onKeyDown={this.enterPressed} onChange={this.changeUserInput} ></input>
+                <input type='text' name='find_user' onKeyDown={this.enterPressed} onChange={this.changeUserInput} />
                 {findUserInput && 
-                    <button id='enter' addEvent onClick={this.findUser}>Find User</button>
+                    <button id='enter' onClick={this.findUser}>Find User</button>
                 }
                 {user &&
-                <div>
-
-                    {changeUserForm.map(el => <Input key={el.id} el={el} changeInputValues={console.log} className={'hello'}/>)}
-                </div>
-
+                    <div>
+                        <form onSubmit={this.changeUser} className="form-doctors">
+                            {changeUserForm.map(el =>
+                                <Input
+                                    key={el.id}
+                                    el={el}
+                                    changeInputValues={changeInputValueUserForm}
+                                    className={el.className}
+                                />
+                            )}
+                            <button type='submit'>Change</button>
+                        </form>
+                    </div>
                 }
-               {error &&
-                   <div>
-                       <p>User not found</p>
-                   </div>
+
+                {error &&
+                    <div>
+                        <p>User not found</p>
+                    </div>
                }
            </div>
         );

+ 41 - 17
src/reducers/user.js

@@ -3,15 +3,15 @@ import * as types from '../actionsTypes/actionsTypes'
 import {adminChangeUserForm} from "../utils/formFields"
 
 const defaultState = {
-    user:null,
-    findUserInput:'',
-    changeUserForm:adminChangeUserForm,
+    user: null,
+    findUserInput: '',
+    changeUserForm: adminChangeUserForm,
     isFetching: false,
-    error:null
+    error: null
 };
 
 export const userReducer = (state = defaultState, action) => {
-    switch(action.type){
+    switch (action.type) {
 
         case types.FIND_USER_REQUEST : {
             return {
@@ -22,15 +22,22 @@ export const userReducer = (state = defaultState, action) => {
 
         case types.FIND_USER_REQUEST_SUCCESS : {
             const data = state.findUserInput.includes('@') ? action.payload.users[0] : action.payload.user;
-            state.changeUserForm.map(el => console.log(Object.keys(data).find(item => item === el.name)))
+            state.changeUserForm.map(el => console.log(Object.keys(data).find(item => item === el.name)));
             return {
                 ...state,
-                user:data,
-                changeUserForm:state.changeUserForm.map(el => Object.keys(data).find(item => item === el.name) ? {
+                user: data,
+                changeUserForm: state.changeUserForm.map(el => Object.keys(data).find(item => item === el.name) ? el.type === 'radio' ?
+                    {
+                        ...el,
+                        checked: data[`${el.name}`] === el.value
+                    } :
+                    {
                     ...el,
-                    value:data[`${el.name}`]
-                } : el),
-                error:action.payload.message,
+                    value: data[`${el.name}`]
+                    } :
+                    el
+                ),
+                error: action.payload.message,
                 isFetching: false
             };
         }
@@ -38,7 +45,7 @@ export const userReducer = (state = defaultState, action) => {
         case types.FIND_USER_REQUEST_FAIL : {
             return {
                 ...state,
-                error:action.payload,
+                error: action.payload,
                 isFetching: false
             }
         }
@@ -46,12 +53,30 @@ export const userReducer = (state = defaultState, action) => {
         case types.CHANGE_INPUT_VALUE_FIND_USER : {
             return {
                 ...state,
-                findUserInput:action.payload
+                findUserInput: action.payload
             };
         }
 
+        case types.CHANGE_INPUT_VALUE_USER_FORM : {
+            console.log(action.payload.target);
+            const data = action.payload.target;
+            return {
+                ...state,
+                changeUserForm: state.changeUserForm.map(el => el.name === data.name ? el.type === 'radio' ?
+                    {
+                        ...el,
+                        checked: !el.checked
+                    } :
+                    {
+                        ...el,
+                        value: data.value
+                    } :
+                    el
+                ),
+            };
+        }
 
-         case types.DELETE_USER_REQUEST : {
+        case types.DELETE_USER_REQUEST : {
             return {
                 ...state,
                 isFetching: true
@@ -61,7 +86,7 @@ export const userReducer = (state = defaultState, action) => {
         case types.DELETE_USER_REQUEST_SUCCESS : {
             return {
                 ...state,
-                services:action.payload,
+                services: action.payload,
                 isFetching: false
             };
         }
@@ -69,12 +94,11 @@ export const userReducer = (state = defaultState, action) => {
         case types.DELETE_USER_REQUEST_FAIL : {
             return {
                 ...state,
-                error:action.payload,
+                error: action.payload,
                 isFetching: false
             }
         }
 
-       
 
         default:
             return state

+ 9 - 9
src/utils/formFields.js

@@ -267,37 +267,37 @@ export const adminChangeUserForm =[
 	},
 	{
 		id:5,
-		type:'radio',
+		type:'text',
 		value:"",
-		name:'role',
+		name:'_id',
 		required:true
 	},
 	{
 		id:6,
 		type:'radio',
-		value:"",
+		value:true,
 		name:'role',
 		required:true
 	},
 	{
 		id:7,
 		type:'radio',
-		value:"",
-		name:'doctor',
+		value:false,
+		name:'role',
 		required:true
 	},
 	{
 		id:8,
 		type:'radio',
-		value:"",
+		value:true,
 		name:'doctor',
 		required:true
 	},
 	{
 		id:9,
-		type:'text',
-		value:"",
-		name:'_id',
+		type:'radio',
+		value:false,
+		name:'doctor',
 		required:true
 	}