Browse Source

Merge branch 'dev' of http://gitlab.a-level.com.ua/Entony/FEA_12_CLINIC into mila

Mila-Zagrevskaya 5 years ago
parent
commit
018ca2dd23

+ 9 - 8
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,
@@ -26,15 +29,13 @@ const findUserRequestFail = payload => ({
 });
 
 export const findUser = (payload) => dispatch => {
-    console.log(payload)
     dispatch(findUserRequest());
     return fetch(`${URL}`+payload,{
         credentials:"include"
     })
         .then(res => res.json())
         .then(res => {
-            dispatch(findUserRequestSuccess(res)) 
-            console.log(res)
+            dispatch(findUserRequestSuccess(res));
         })
         .catch(err => dispatch(findUserRequestFail(err)));
 };
@@ -84,12 +85,12 @@ const putUserRequestFail = payload => ({
 });
 
 export const putUser = (payload) => dispatch => {
-    dispatch(deleteUserRequest());
+    dispatch(putUserRequest());
     return fetch(`${URL}${payload}`, {
         method: "DELETE",
         credentials: "include"
     })
         .then(res => res.json())
-        .then(res => dispatch(deleteUserRequestSuccess(res)))
-        .catch(err => dispatch(deleteUserRequestFail(err)));
+        .then(res => dispatch(putUserRequestSuccess(res)))
+        .catch(err => dispatch(putUserRequestFail(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";

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

@@ -21,7 +21,8 @@ import {
 import {
     changeFindUserInput,
     findUser,
-    deleteUser
+    deleteUser,
+    changeInputValueUserForm
 } from "../../actions/user"
 
 import Shedule from './Shedule'
@@ -60,7 +61,10 @@ export class Admin extends React.Component {
             findUserInput,
             changeFindUserInput,
             findUser,
-            deleteUser
+            deleteUser,
+            userError,
+            changeUserForm,
+            changeInputValueUserForm
         } = this.props;
 
         return (
@@ -108,6 +112,9 @@ export class Admin extends React.Component {
                             changeFindUserInput={changeFindUserInput}
                             findUser={findUser}
                             deleteUser={deleteUser}
+                            error={userError}
+                            changeUserForm={changeUserForm}
+                            changeInputValueUserForm={changeInputValueUserForm}
                         />} />
                     </Switch>
                 </div>
@@ -128,7 +135,9 @@ const mapStateToProps = state => {
         services: state.services.services,
         categories: state.services.categories,
         user:state.user.user,
-        findUserInput:state.user.findUserInput
+        findUserInput:state.user.findUserInput,
+        userError:state.user.error,
+        changeUserForm:state.user.changeUserForm,
     }
 };
 
@@ -146,12 +155,10 @@ const mapDispatchToProps = {
     putServices,
     deleteServices,
     changeSpecialityArray,
-    putServices,
-    deleteServices,
-    changeSpecialityArray,
     changeFindUserInput,
     findUser,
-    deleteUser
+    deleteUser,
+    changeInputValueUserForm
 };
 
 export default connect (mapStateToProps,mapDispatchToProps)(Admin)

+ 13 - 11
src/components/Admin/Input.js

@@ -2,19 +2,21 @@ import React from 'react';
 
 export default class Input extends React.Component {
     render(){
+        const {el,className,changeInputValues} = this.props;
         return (
             <input
-                className = "appointment admin-form"
-                id={this.props.el.id}
-                type={this.props.el.type}
-                name={this.props.el.name}
-                value={this.props.el.value}
-                placeholder={this.props.el.placeholder}
-                minLength={this.props.el.minLength}
-                maxLength={this.props.el.maxLength}
-                required={this.props.el.required}
-                pattern={this.props.el.pattern}
-                onChange={(e) => this.props.changeInputValues(e)}
+                className = {className ? className : "appointment admin-form"}
+                id={el.id}
+                type={el.type}
+                name={el.name}
+                value={el.value}
+                placeholder={el.placeholder}
+                minLength={el.minLength}
+                maxLength={el.maxLength}
+                required={el.required}
+                pattern={el.pattern}
+                checked={el.checked}
+                onChange={(e) => changeInputValues(e)}
             />
         );
     }

+ 49 - 17
src/components/Admin/tempp.js

@@ -1,14 +1,32 @@
 import React from 'react';
 
+
+import Input from "./Input"
+
 export default class Tempp extends React.Component {
 
-changeUserInput = (e) => {
+    changeUserInput = (e) => {
     this.props.changeFindUserInput(e.target.value)
-}
+    };
+
+    findUser = () => {
+        this.props.findUser(this.props.findUserInput.includes('@') ? '?email='+this.props.findUserInput : this.props.findUserInput)
+    };
+
+    enterPressed = (e) => {
+        if (e.key === 'Enter') {
+            this.findUser();
+        }
+    };
 
-findUser = () => {
-    this.props.findUser(this.props.findUserInput.includes('@') ? '?email='+this.props.findUserInput : this.props.findUserInput)
-}
+    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 {
@@ -16,27 +34,41 @@ findUser = () => {
             findUserInput,
             changeFindUserInput,
             findUser,
-            deleteUser
-        } = this.props
+            deleteUser,
+            changeInputValueUserForm,
+            changeUserForm,
+            error
+        } = this.props;
         console.log(this.props)
         return (
            <div>
-                <input type='text' onChange={this.changeUserInput}></input>
+                <input type='text' name='find_user' onKeyDown={this.enterPressed} onChange={this.changeUserInput} />
                 {findUserInput && 
-                    <button onClick={this.findUser}>Find User</button>
+                    <button id='enter' onClick={this.findUser}>Find User</button>
                 }
-                {user && 
+                {user &&
                     <div>
-                        <p>{user.firstName}</p>
-                        <p>{user.lastName}</p>
-                        <p>{user.email}</p>
-                        <p>{user.role}</p>
-                        <p>{user.doctor}</p>
-                        <p>{user.createdAt}</p>
-                        <p>{user._id}</p>
+                        <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>
+               }
            </div>
         );
     }
 };
+

+ 45 - 11
src/reducers/user.js

@@ -1,14 +1,17 @@
 import * as types from '../actionsTypes/actionsTypes'
 
+import {adminChangeUserForm} from "../utils/formFields"
+
 const defaultState = {
-    user:null,
-    findUserInput:'',
+    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 {
@@ -18,9 +21,23 @@ 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)));
             return {
                 ...state,
-                user:state.findUserInput.includes('@') ? action.payload.users[0] : action.payload.user,
+                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,
                 isFetching: false
             };
         }
@@ -28,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
             }
         }
@@ -36,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
@@ -51,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
             };
         }
@@ -59,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

+ 71 - 0
src/utils/formFields.js

@@ -232,6 +232,77 @@ export const postNewServiceForm =[
 	}
 ];
 
+export const adminChangeUserForm =[
+	{
+		id:1,
+		type:'text',
+		value:"",
+		name:'firstName',
+		placeholder:'Введите Имя',
+		required:true
+	},
+	{
+		id:2,
+		type:'text',
+		value:"",
+		name:'lastName',
+		placeholder:'Введите Фамилию',
+		required:true
+	},
+	{
+		id:3,
+		type:'email',
+		value:"",
+		name:'email',
+		placeholder:'Введите E-mail',
+		required:true
+	},
+	{
+		id:4,
+		type:'phone',
+		value:"",
+		name:'phone',
+		placeholder:'Введите номер телефона',
+		required:true
+	},
+	{
+		id:5,
+		type:'text',
+		value:"",
+		name:'_id',
+		required:true
+	},
+	{
+		id:6,
+		type:'radio',
+		value:true,
+		name:'role',
+		required:true
+	},
+	{
+		id:7,
+		type:'radio',
+		value:false,
+		name:'role',
+		required:true
+	},
+	{
+		id:8,
+		type:'radio',
+		value:true,
+		name:'doctor',
+		required:true
+	},
+	{
+		id:9,
+		type:'radio',
+		value:false,
+		name:'doctor',
+		required:true
+	}
+
+];
+
 export const route = [
 	{
 		id: 1,