Browse Source

admin-page

Marina Yakovenko 5 năm trước cách đây
mục cha
commit
b67e0a9905

+ 9 - 1
src/actionTypes/actionTypes.js

@@ -72,4 +72,12 @@ export const GET_REGISTRED_USERS_FAIL = "GET_REGISTRED_USERS_FAIL";
 
 export const GET_USERS_INFO = "GET_USERS_INFO";
 export const GET_USERS_INFO_SUCCESS = "GET_USERS_INFO_SUCCESS";
-export const GET_USERS_INFO_ERROR = "GET_USERS_INFO_ERROR";
+export const GET_USERS_INFO_ERROR = "GET_USERS_INFO_ERROR";
+
+export const POST_RESULTS_REQUEST = "POST_RESULTS_REQUEST";
+export const POST_RESULTS_REQUEST_SUCCESS = "POST_RESULTS_REQUEST_SUCCESS";
+export const POST_RESULTS_REQUEST_FAIL = "POST_RESULTS_REQUEST_FAIL";
+
+export const GET_PHOTOGALARU_EVENTS_REQUEST = "GET_PHOTOGALARU_EVENTS_REQUEST";
+export const GET_PHOTOGALARU_EVENTS_REQUEST_SUCCESS = "GET_PHOTOGALARU_EVENTS_REQUEST_SUCCESS";
+export const GET_PHOTOGALARU_EVENTS_REQUEST_FAIL = "GET_PHOTOGALARU_EVENTS_REQUEST_FAIL";

+ 35 - 3
src/actions/adminResultsActions.js

@@ -2,6 +2,7 @@ import axios from "axios";
 import * as types from "../actionTypes/actionTypes"
 
 const eventUserURL = 'https://api-marathon.herokuapp.com/api/v1/eventUsers'
+const resultsURL = 'https://api-marathon.herokuapp.com/api/v1/results'
 
 //GET REGISTRED USERS
 const getRegistredUsersRequest = payload => ({
@@ -10,12 +11,12 @@ const getRegistredUsersRequest = payload => ({
 });
 
 const getRegistredUsersRequestSuccess = payload => ({
-	type: types.GET_EVENTS_REQUEST_SUCCESS,
+	type: types.GET_REGISTRED_USERS_SUCCESS,
 	payload
 });
 
 const getRegistredUsersRequestFail = payload => ({
-	type: types.GET_EVENTS_REQUEST_FAIL,
+	type: types.GET_REGISTRED_USERS_FAIL,
 	payload
 });
 
@@ -26,4 +27,35 @@ export const getRegistredUsers = (_id) => dispatch => {
 		.then(res => {dispatch(getRegistredUsersRequestSuccess(res))
         })
 		.catch(err => dispatch(getRegistredUsersRequestFail(err)));
-};
+};
+
+//POST
+const postResultsRequest = payload => ({
+	type: types.POST_RESULTS_REQUEST,
+	payload
+   });
+   const postResultsRequestSuccess = payload => ({
+	type: types.POST_RESULTS_REQUEST_SUCCESS,
+	payload
+   });
+   const postResultsRequestFail = payload => ({
+	type: types.POST_RESULTS_REQUEST_FAIL,
+	payload
+   });
+   
+   const postResults = payload => {
+	return async dispatch => {
+	 dispatch(postResultsRequest());
+	 try {
+	  const { data } = await axios.post(resultsURL, payload);
+	  dispatch(postResultsRequestSuccess(data));
+	  console.log("Results Posted", payload)
+	 } catch (error) {
+	  dispatch(postResultsRequestFail(error));
+	 }
+	};
+   };
+
+   export const resultsPromiseAll = payload => {
+	return dispatch => Promise.all(payload.map(elem => dispatch ( postResults(elem))))
+   }

+ 1 - 1
src/components/adminPhotogalary/adminPhotogalaryReduxForm.js

@@ -47,7 +47,7 @@ class PhotogalaryReduxForm extends Component {
                     ...prevState.addPhotogalaryInitialValue,
                     [name]: value
                 },
-                filteredEventList: [{id: 1, title: "Choose event"}].concat(
+                filteredEventList: [{_id: 1, title: "Choose event"}].concat(
                     this.props.eventList.filter(el => el.eventType === value)
                 )
             };

+ 0 - 3
src/components/adminResults/adminResultsForm.js

@@ -7,8 +7,6 @@ import { customSelect } from "../customFields/customSelect/customSelect";
 class AdminResultsForm extends Component {
 
     render() {
-        // const { filteredEventList, addPhotogalaryInitialValue } = this.state;
-        // console.log(this.state )
 
         const {
             handleSubmit,
@@ -62,7 +60,6 @@ class AdminResultsForm extends Component {
                         <div className = "results-form__user-data_email">{elem.email}</div>
                         <div className = "results-form__user-data_country">{elem.userCountry}</div>
                         <div className = "results-form__user-data_distance">{elem.distance}</div>
-                        {/* <input id = {elem._id} className="input-box -wide" value = '' onChange = {onChangeInputTimeHandler}/> */}
                         <Field
                             id = {elem._id}
                             name={elem._id}

+ 10 - 52
src/conteiners/adminResultsPage/adminResultsPage.js

@@ -1,54 +1,42 @@
 import React, { Component } from "react";
 import { connect } from "react-redux";
-// import * as actions from "../../actions/photogalaryActions";
 import {getEvents, getEventByTitle} from "../../actions/adminMainPageActions";
-import {getRegistredUsers} from "../../actions/adminResultsActions";
+import {getRegistredUsers, resultsPromiseAll} from "../../actions/adminResultsActions";
 import AdminHeader from "../../components/adminHeader/adminHeader";
 import ResultsForm from "../../components/adminResults/adminResultsForm";
 import ConfirmationMessage from "../../components/confirmationMessage/confirmationMessage";
 
 class AdminResultstPage extends Component {
     state = {
-        // resultState: [
-        //     {
-        //         eventUser: {},
-        //         time: ''
-        //     }
-        // ],
         confirmationMessageFlag: false
      };
 
      componentDidMount() {
         this.props.getEvents();
-        // this.props.getRegistredUsers()
     }
 
     showConfirmationMessage = () => {
         this.setState({confirmationMessageFlag: true })
-        document.location.reload(true);
     };
     closeConfirmationMessage = () => {
         this.setState({ confirmationMessageFlag: false })
     };
 
     submitHandler = values => {
-        console.log('this.props.eventusers', this.props.eventusers)
-        console.log('submit values', values)
-        const results = Object.keys(values)
-            .filter(elem => elem !== 'eventType')
+        const results = Object.keys(values).slice(2)
             .map(elem => elem = {
-                eventUser: this.props.eventusers.find(
-                    user => user._id === elem), time: values[elem]
-            }
-            )
-
-        console.log('submit results', results)
+                eventUser: elem, 
+                event: this.props.eventusers.find( user => user._id === elem).event[0],
+                time: values[elem]
+            })
+       
+        this.props.resultsPromiseAll(results)
+        this.showConfirmationMessage()
     }
 
     onChangeTypeHandler = e => {
         const { value } = e.target;
         this.props.getEvents(value)
-        // console.log('onChangeTypeHandler',  value)
     };
 
     onChangeTitleHandler = e => {
@@ -56,30 +44,8 @@ class AdminResultstPage extends Component {
         this.props.getEventByTitle(value).then((res) => {
             this.props.getRegistredUsers(res.payload.data.events[0]._id)
         })
-        // console.log('titile eventList', this.props.eventList)
-        // this.props.getRegistredUsers(this.props.eventList_)
-        // console.log('onChangeTitleHandler', value)
     };
 
-    // onChangeInputTimeHandler = e => {
-    //     const { value, id, name } = e.target;
-    //     console.log('onChangeInputTimeHandler value', value, id)
-    //         this.setState(prevState => {
-    //             console.log('prevState resultState', prevState)
-    //             return {
-    //                 ...prevState,
-    //                 resultState: [
-    //                     ...prevState.resultState,
-    //                     {
-    //                     eventuser: this.props.eventusers.find( elem => elem._id === id),
-    //                     time: value,
-    //                     id: id
-    //                     }
-    //                 ]
-    //             };
-    //         });
-    // }
-
     render() {
         const {
             eventTypes,
@@ -87,11 +53,6 @@ class AdminResultstPage extends Component {
             eventusers,
         } = this.props
 
-        console.log('page props', this.props)
-        // console.log('eventList', eventList)
-        // console.log('gallery', this.props.gallery)
-
-		// console.log("eventTypes", this.state.eventTypes );
 		return (
             <>
                 <AdminHeader />
@@ -103,7 +64,6 @@ class AdminResultstPage extends Component {
                             eventList={eventList}
                             submitHandler = {this.submitHandler}
                             onChangeInputTimeHandler = {this.onChangeInputTimeHandler}
-                            // eventByTitle ={eventByTitle}
                             eventusers={eventusers}
                             onChangeTypeHandler = {this.onChangeTypeHandler}
                             onChangeTitleHandler = {this.onChangeTitleHandler}
@@ -125,12 +85,10 @@ const mapStateToProps = state => ({
     eventByTitle: state.adminMainPageReducer.eventByTitle,
     eventTypes: state.adminMainPageReducer.eventTypes,
     eventusers: state.adminResultsReduser.eventusers,
-    // eventFormInitialValue: state.adminMainPageReducer.eventFormInitialValue,
     addPhotogalaryInitialValue: state.photogalaryReducer.addPhotogalaryInitialValue,
-    // gallery: state.photogalaryReducer.gallery
 });
 
 export default connect(
     mapStateToProps,
-    {getEvents, getEventByTitle, getRegistredUsers}
+    {getEvents, getEventByTitle, getRegistredUsers, resultsPromiseAll}
 )(AdminResultstPage);

+ 1 - 1
src/conteiners/login/Login.js

@@ -10,7 +10,7 @@ import LoginForm from '../../components/login-form/LoginForm';
 
 export class Login extends React.Component {
     render() {
-        const { postLoginSubmit, message } = this.props;
+        const { postLoginSubmit, message, user } = this.props;
         return (
             <div className="login-page">
                 <div className="form-login">

+ 14 - 1
src/reducers/adminResultsReduser.js

@@ -9,7 +9,7 @@ export default (state = initialState, action) => {
 		case types.GET_REGISTRED_USERS: {
 			return state;
 		}
-		case types.GET_EVENTS_REQUEST_SUCCESS: {
+		case types.GET_REGISTRED_USERS_SUCCESS: {
 			const { data } = action.payload;
             const eventusers = data.eventusers
             // console.log('state', state)
@@ -20,6 +20,19 @@ export default (state = initialState, action) => {
 			return state;
 		}
 
+		//POST
+		case types.POST_RESULTS_REQUEST_SUCCESS: {
+			console.log('reducer results success', action.payload)
+			return {
+			...state,
+			addEventMessage: "Results have been added"
+			}
+		}
+		case types.POST_RESULTS_REQUEST_FAIL: {
+			console.log('reducer results fail', action.payload)
+			return { ...state, error: action.payload }
+		}
+
         default:
 			return state; 
     }

+ 2 - 2
src/router.js

@@ -136,10 +136,10 @@ const route = [
 
 const Router = withRouter(({ history, user }) => {
 	useEffect(() => {
-		const userl = localStorage.getItem("user");
+		const userl = JSON.parse(localStorage.user).user; 
 
 		if (userl) {
-			const userRole = JSON.parse(userl).user.role;
+			const userRole = userl.role;
 
 			if (userRole === 'admin') {
 				history.push("/admin");