Browse Source

created new action/reucer for shedule, added auto renew data with all post/put/delete

Boris K 5 years ago
parent
commit
9a53d59804

+ 3 - 164
src/actions/actions.js

@@ -38,13 +38,6 @@ export const changeSpecialityArray = payload => ({
     payload
 });
 
-// -----------------------------------------------------------------------------------------------------------------
-
-export const setSheduleDoctor = payload => ({
-    type: types.CHANGE_SHEDULE_DOCTOR,
-    payload
-});
-
 // -----------------------------------------------------------------------------------------------------------------
 // -----------------------------------------------------------------------------------------------------------------
 
@@ -76,34 +69,6 @@ export const getDoctors = () => dispatch => {
 
 // -----------------------------------------------------------------------------------------------------------------
 
-// const getServicesRequest = payload => ({
-//     type: types.GET_SERVICES_REQUEST,
-//     payload
-// });
-//
-// const getServicesRequestSuccess = payload => ({
-//     type: types.GET_SERVICES_REQUEST_SUCCESS,
-//     payload
-// });
-//
-// const getServicesRequestFail = payload => ({
-//     type: types.GET_SERVICES_REQUEST_FAIL,
-//     payload
-// });
-//
-// export const getServices = () => dispatch => {
-//     dispatch(getServicesRequest());
-//     return fetch(`${URL}services`,{
-//         credentials:"include"
-//     })
-//         .then(res => res.json())
-//         .then(res => dispatch(getServicesRequestSuccess(res)))
-//         .catch(err => dispatch(getServicesRequestFail(err)));
-// };
-
-// -----------------------------------------------------------------------------------------------------------------
-// -----------------------------------------------------------------------------------------------------------------
-
 const postDoctorsRequest = payload => ({
     type: types.POST_DOCTORS_REQUEST,
     payload
@@ -130,77 +95,12 @@ export const postDoctors = (payload) => dispatch => {
         body: JSON.stringify(payload)
     })
         .then(res => res.json())
-        .then(res => dispatch(postDoctorsRequestSuccess(res)))
+        .then(res => dispatch(postDoctorsRequestSuccess(res))).then(dispatch(getDoctors()))
         .catch(err => dispatch(postDoctorsRequestFail(err)));
 };
 
 // -----------------------------------------------------------------------------------------------------------------
 
-const postServicesRequest = payload => ({
-    type: types.POST_SERVICES_REQUEST,
-    payload
-});
-
-const postServicesRequestSuccess = payload => ({
-    type: types.POST_SERVICES_REQUEST_SUCCESS,
-    payload
-});
-
-const postServicesRequestFail = payload => ({
-    type: types.POST_SERVICES_REQUEST_FAIL,
-    payload
-});
-
-export const postServices = (payload) => dispatch => {
-    dispatch(postServicesRequest());
-    return fetch(`${URL}services`, {
-        method: "POST",
-        credentials: "include",
-        headers: {
-            "Content-Type": "application/json"
-        },
-        body: JSON.stringify(payload)
-    })
-        .then(res => res.json())
-        .then(res =>  dispatch(postServicesRequestSuccess(res)))
-        .catch(err => dispatch(postServicesRequestFail(err)))
-};
-
-// -----------------------------------------------------------------------------------------------------------------
-
-const postSheduleRequest = payload => ({
-    type: types.POST_SHEDULE_REQUEST,
-    payload
-});
-
-const postSheduleSuccess = payload => ({
-    type: types.POST_SHEDULE_REQUEST_SUCCESS,
-    payload
-});
-
-const postSheduleFail = payload => ({
-    type: types.POST_SHEDULE_REQUEST_FAIL,
-    payload
-});
-
-export const postShedule = (payload) => dispatch => {
-    dispatch(postSheduleRequest());
-    return fetch(`${URL}shedule`, {
-        method: "POST",
-        credentials:"include",
-        headers: {
-            'Content-Type': 'application/json'
-        },
-        body: JSON.stringify(payload)
-    })
-        .then(res => res.json())
-        .then(res => dispatch(postSheduleSuccess(res))).then(dispatch(getDoctors()))
-        .catch(err => dispatch(postSheduleFail(err)));
-};
-
-// -----------------------------------------------------------------------------------------------------------------
-// -----------------------------------------------------------------------------------------------------------------
-
 const putDoctorsRequest = payload => ({
     type: types.PUT_DOCTORS_REQUEST,
     payload
@@ -227,73 +127,12 @@ export const putDoctors = (payload) => dispatch => {
         body: JSON.stringify(payload.data)
     })
         .then(res => res.json())
-        .then(res => dispatch(putDoctorsRequestSuccess(res)))
+        .then(res => dispatch(putDoctorsRequestSuccess(res))).then(dispatch(getDoctors()))
         .catch(err => dispatch(putDoctorsRequestFail(err)));
 };
 
 // -----------------------------------------------------------------------------------------------------------------
 
-const putServicesRequest = payload => ({
-    type: types.PUT_SERVICES_REQUEST,
-    payload
-});
-
-const putServicesRequestSuccess = payload => ({
-    type: types.PUT_SERVICES_REQUEST_SUCCESS,
-    payload
-});
-
-const putServicesRequestFail = payload => ({
-    type: types.PUT_SERVICES_REQUEST_FAIL,
-    payload
-});
-
-export const putServices = (payload) => dispatch => {
-    dispatch(putServicesRequest());
-    return fetch(`${URL}services/${payload.id}`, {
-        method: "PUT",
-        credentials: "include",
-        headers: {
-            "Content-Type": "application/json"
-        },
-        body: JSON.stringify(payload.data)
-    })
-        .then(res => res.json())
-        .then(res => dispatch(putServicesRequestSuccess(res)))
-        .catch(err => dispatch(putServicesRequestFail(err)));
-};
-
-// -----------------------------------------------------------------------------------------------------------------
-// -----------------------------------------------------------------------------------------------------------------
-
-const deleteServicesRequest = payload => ({
-    type: types.DELETE_DOCTORS_REQUEST,
-    payload
-});
-
-const deleteServicesRequestSuccess = payload => ({
-    type: types.DELETE_DOCTORS_REQUEST_SUCCESS,
-    payload
-});
-
-const deleteServicesRequestFail = payload => ({
-    type: types.DELETE_DOCTORS_REQUEST_FAIL,
-    payload
-});
-
-export const deleteServices = (payload) => dispatch => {
-    dispatch(deleteServicesRequest());
-    return fetch(`${URL}services/${payload}`, {
-        method: "DELETE",
-        credentials: "include"
-    })
-        .then(res => res.json())
-        .then(res => dispatch(deleteServicesRequestSuccess(res)))
-        .catch(err => dispatch(deleteServicesRequestFail(err)));
-};
-
-// -----------------------------------------------------------------------------------------------------------------
-
 const deleteDoctorsRequest = payload => ({
     type: types.DELETE_DOCTORS_REQUEST,
     payload
@@ -316,7 +155,7 @@ export const deleteDoctors = (payload) => dispatch => {
         credentials: "include"
     })
         .then(res => res.json())
-        .then(res => dispatch(deleteDoctorsRequestSuccess(res)))
+        .then(res => dispatch(deleteDoctorsRequestSuccess(res))).then(dispatch(getDoctors()))
         .catch(err => dispatch(deleteDoctorsRequestFail(err)));
 };
 

+ 93 - 0
src/actions/services.js

@@ -2,6 +2,7 @@ import * as types from "../actionsTypes/actionsTypes";
 
 const URL = "https://api-clinics.herokuapp.com/api/v1/";
 
+
 const getServicesRequest = payload => ({
     type: types.GET_SERVICES_REQUEST,
     payload
@@ -27,7 +28,99 @@ export const getServices = () => dispatch => {
         .catch(err => dispatch(getServicesRequestFail(err)));
 };
 
+// -----------------------------------------------------------------------------------------------------------------
+
+const postServicesRequest = payload => ({
+    type: types.POST_SERVICES_REQUEST,
+    payload
+});
+
+const postServicesRequestSuccess = payload => ({
+    type: types.POST_SERVICES_REQUEST_SUCCESS,
+    payload
+});
+
+const postServicesRequestFail = payload => ({
+    type: types.POST_SERVICES_REQUEST_FAIL,
+    payload
+});
+
+export const postServices = (payload) => dispatch => {
+    dispatch(postServicesRequest());
+    return fetch(`${URL}services`, {
+        method: "POST",
+        credentials: "include",
+        headers: {
+            "Content-Type": "application/json"
+        },
+        body: JSON.stringify(payload)
+    })
+        .then(res => res.json())
+        .then(res =>  dispatch(postServicesRequestSuccess(res))).then(dispatch(getServices()))
+        .catch(err => dispatch(postServicesRequestFail(err)))
+};
+
+// -----------------------------------------------------------------------------------------------------------------
+
+const putServicesRequest = payload => ({
+    type: types.PUT_SERVICES_REQUEST,
+    payload
+});
+
+const putServicesRequestSuccess = payload => ({
+    type: types.PUT_SERVICES_REQUEST_SUCCESS,
+    payload
+});
+
+const putServicesRequestFail = payload => ({
+    type: types.PUT_SERVICES_REQUEST_FAIL,
+    payload
+});
+
+export const putServices = (payload) => dispatch => {
+    dispatch(putServicesRequest());
+    return fetch(`${URL}services/${payload.id}`, {
+        method: "PUT",
+        credentials: "include",
+        headers: {
+            "Content-Type": "application/json"
+        },
+        body: JSON.stringify(payload.data)
+    })
+        .then(res => res.json())
+        .then(res => dispatch(putServicesRequestSuccess(res))).then(dispatch(getServices()))
+        .catch(err => dispatch(putServicesRequestFail(err)));
+};
+
+// -----------------------------------------------------------------------------------------------------------------
+
+const deleteServicesRequest = payload => ({
+    type: types.DELETE_DOCTORS_REQUEST,
+    payload
+});
+
+const deleteServicesRequestSuccess = payload => ({
+    type: types.DELETE_DOCTORS_REQUEST_SUCCESS,
+    payload
+});
+
+const deleteServicesRequestFail = payload => ({
+    type: types.DELETE_DOCTORS_REQUEST_FAIL,
+    payload
+});
+
+export const deleteServices = (payload) => dispatch => {
+    dispatch(deleteServicesRequest());
+    return fetch(`${URL}services/${payload}`, {
+        method: "DELETE",
+        credentials: "include"
+    })
+        .then(res => res.json())
+        .then(res => dispatch(deleteServicesRequestSuccess(res))).then(dispatch(getServices()))
+        .catch(err => dispatch(deleteServicesRequestFail(err)));
+};
 
+// -----------------------------------------------------------------------------------------------------------------
 
 const getCategoriesRequest = payload => ({
     type: types.GET_CATEGORIES_REQUEST,

+ 46 - 0
src/actions/shedule.js

@@ -0,0 +1,46 @@
+import * as types from '../actionsTypes/actionsTypes'
+import {getDoctors} from "./actions";
+
+
+export const setSheduleDoctor = payload => ({
+    type: types.CHANGE_SHEDULE_DOCTOR,
+    payload
+});
+
+// -----------------------------------------------------------------------------------------------------------------
+
+const postSheduleRequest = payload => ({
+    type: types.POST_SHEDULE_REQUEST,
+    payload
+});
+
+// -----------------------------------------------------------------------------------------------------------------
+
+const postSheduleSuccess = payload => ({
+    type: types.POST_SHEDULE_REQUEST_SUCCESS,
+    payload
+});
+
+// -----------------------------------------------------------------------------------------------------------------
+
+const postSheduleFail = payload => ({
+    type: types.POST_SHEDULE_REQUEST_FAIL,
+    payload
+});
+
+// -----------------------------------------------------------------------------------------------------------------
+
+export const postShedule = (payload) => dispatch => {
+    dispatch(postSheduleRequest());
+    return fetch(`${URL}shedule`, {
+        method: "POST",
+        credentials:"include",
+        headers: {
+            'Content-Type': 'application/json'
+        },
+        body: JSON.stringify(payload)
+    })
+        .then(res => res.json())
+        .then(res => dispatch(postSheduleSuccess(res))).then(dispatch(getDoctors()))
+        .catch(err => dispatch(postSheduleFail(err)));
+};

+ 12 - 6
src/components/Admin/Admin.js

@@ -3,8 +3,6 @@ import {connect} from 'react-redux'
 import {Link} from 'react-router-dom'
 import {Switch, Route} from "react-router-dom";
 import {
-    setSheduleDoctor,
-    postShedule,
     changeInputValueDoctorForm,
     changeInputValueServiceForm,
     postDoctors,
@@ -12,9 +10,6 @@ import {
     changeSelectedServiceId,
     putDoctors,
     deleteDoctors,
-    postServices,
-    putServices,
-    deleteServices,
     changeSpecialityArray
 } from "../../actions/actions";
 
@@ -26,6 +21,17 @@ import {
     putUser
 } from "../../actions/user"
 
+import {
+    postServices,
+    putServices,
+    deleteServices
+} from "../../actions/services"
+
+import {
+    setSheduleDoctor,
+    postShedule,
+} from "../../actions/shedule"
+
 import Shedule from './Shedule'
 import ChangeUser from './ChangeUser'
 import ChangeServicesDoctors from './ChangeServices-Doctors'
@@ -129,7 +135,7 @@ export class Admin extends React.Component {
 const mapStateToProps = state => {
     return {
         doctors:state.app.doctors,
-        postNewShedule:state.app.postNewShedule,
+        postNewShedule:state.shedule.postNewShedule,
         postNewDoctor:state.app.postNewDoctor,
         postNewService:state.app.postNewService,
         changeDoctorId:state.app.changeDoctorId,

+ 2 - 3
src/components/Admin/Shedule.js

@@ -11,7 +11,7 @@ export default class Shedule extends React.Component {
     };
 
     post = (e) => {
-        e.preventDefault()
+        e.preventDefault();
         let current = new Date(this.state.startDate);
         let end = new Date (this.state.endDate);
         while (current.toISOString().split('T')[0] <= end.toISOString().split('T')[0]){
@@ -32,8 +32,7 @@ export default class Shedule extends React.Component {
         this.setState ( { endDate:e.target.value } )
     };
     setDoctor = (e) => {
-        console.log (e)
-        this.props.setSheduleDoctor(e)
+        this.props.setSheduleDoctor({data:e,doctors:this.props.doctors})
   
     };
 

+ 3 - 1
src/reducers/index.js

@@ -6,6 +6,7 @@ import auth from './auth';
 import {appointmentReducer} from "./appointment";
 import {servicesReducer} from "./services";
 import {userReducer} from "./user";
+import {sheduleReducer} from "./shedule";
 
 
 
@@ -15,5 +16,6 @@ export default combineReducers({
     calendar:calendarReducer,
     appointment:appointmentReducer,
     services:servicesReducer,
-    user:userReducer
+    user:userReducer,
+    shedule:sheduleReducer
 })

+ 0 - 53
src/reducers/reducers.js

@@ -5,15 +5,6 @@ import {postNewDoctorForm,postNewServiceForm} from '../utils/formFields'
 const defaultState = {
     doctors:[],
 
-    orders:[],
-    users:[],
-    reviews: [],
-
-    postNewShedule:{
-        data:null,
-        doctor:null
-    },
-
     postNewDoctor:postNewDoctorForm,
     postNewService:postNewServiceForm,
     changeDoctorId:null,
@@ -22,7 +13,6 @@ const defaultState = {
 
     isFetching:false,
     error: null,
-
 };
 
 // -----------------------------------------------------------------------------------------------------------------
@@ -97,19 +87,6 @@ export const appReducer = (state = defaultState,action) => {
             };
         }
 
-// -----------------------------------------------------------------------------------------------------------------
-
-        case types.CHANGE_SHEDULE_DOCTOR : {
-            let doctor = state.doctors.find(el=>el.name === action.payload);
-            return {
-                ...state,
-                postNewShedule: {
-                    ...state.postNewShedule,
-                    doctor:doctor._id
-                },
-            };
-        }
-
 // -----------------------------------------------------------------------------------------------------------------
 // -----------------------------------------------------------------------------------------------------------------
 
@@ -136,8 +113,6 @@ export const appReducer = (state = defaultState,action) => {
             }
         }
 
-
-
 // -----------------------------------------------------------------------------------------------------------------
 // -----------------------------------------------------------------------------------------------------------------
 
@@ -164,34 +139,6 @@ export const appReducer = (state = defaultState,action) => {
             }
         }
 
-// -----------------------------------------------------------------------------------------------------------------
-
-        case types.POST_SHEDULE_REQUEST : {
-            return {
-                ...state,
-                isFetching: true
-            };
-        }
-
-        case types.POST_SHEDULE_REQUEST_SUCCESS : {
-            return {
-                ...state,
-                postNewShedule:{
-                    ...state.postNewShedule,
-                    data:null,
-                },
-                isFetching: false
-            }
-        }
-
-        case types.POST_SHEDULE_REQUEST_FAIL : {
-            return {
-                ...state,
-                error: action.payload,
-                isFetching: false
-            }
-        }
-
 // -----------------------------------------------------------------------------------------------------------------
 
         case types.POST_SERVICES_REQUEST : {

+ 55 - 0
src/reducers/shedule.js

@@ -0,0 +1,55 @@
+import * as types from '../actionsTypes/actionsTypes'
+
+const defaultState = {
+    postNewShedule:{
+        data:null,
+        doctor:null
+    },
+    isFetching: false,
+    error:null
+};
+
+export const sheduleReducer = (state = defaultState, action) => {
+    switch(action.type){
+
+        case types.CHANGE_SHEDULE_DOCTOR : {
+            let doctor = action.payload.doctors.find(el=>el.name === action.payload.data);
+            return {
+                ...state,
+                postNewShedule: {
+                    ...state.postNewShedule,
+                    doctor:doctor._id
+                },
+            };
+        }
+
+        case types.POST_SHEDULE_REQUEST : {
+            return {
+                ...state,
+                isFetching: true
+            };
+        }
+
+        case types.POST_SHEDULE_REQUEST_SUCCESS : {
+            return {
+                ...state,
+                postNewShedule:{
+                    ...state.postNewShedule,
+                    data:null,
+                },
+                isFetching: false
+            }
+        }
+
+        case types.POST_SHEDULE_REQUEST_FAIL : {
+            return {
+                ...state,
+                error: action.payload,
+                isFetching: false
+            }
+        }
+
+        default:
+            return state
+    }
+};