Browse Source

work on users role exams pages ,...

mahyar 5 năm trước cách đây
mục cha
commit
62b88d7e89

+ 1 - 1
src/components/exams/examDetails.js

@@ -66,7 +66,7 @@ export default function ExamDetails(props) {
                 body: JSON.stringify({answers})
             })
             .then( response => success("جواب ها ارسال شد "))
-            .catch( e => error(String(e.text)))
+            .catch( e => error(String(e.message ? e.message : e.text)) )
         }
     }
 

+ 2 - 2
src/components/exams/exams.js

@@ -14,7 +14,7 @@ const TakenDescription = (props) => (
         >
             {
                 props.item.map( (i,k) => (
-                    <Descriptions.Item span={4}><NavLink className="text-decoration-none" to={`/exams/${i.id}`} >{i.title}</NavLink></Descriptions.Item>
+                    <Descriptions.Item key={k} span={4}><NavLink className="text-decoration-none" to={`/exams/${i.id}`} >{i.title}</NavLink></Descriptions.Item>
                 ) )
             }
         </Descriptions>
@@ -29,7 +29,7 @@ const EnrolledDescription = (props) => (
         >
             {
                 props.item.map( (i,k) => (
-                    <Descriptions.Item span={4}><NavLink className="text-decoration-none" to={`/exams/${i.id}`} >{i.title}</NavLink></Descriptions.Item>
+                    <Descriptions.Item key={k} span={4}><NavLink className="text-decoration-none" to={`/exams/${i.id}`} >{i.title}</NavLink></Descriptions.Item>
                 ) )
             }
         </Descriptions>

+ 10 - 3
src/components/login/login.js

@@ -1,9 +1,9 @@
 import React from 'react';
 import {store} from "../../context/store";
-import {USER_LOGGED_IN, USER_LOGGED_OUT} from "../../context/actionType";
+import {SET_USER_ROLE, USER_LOGGED_IN, USER_LOGGED_OUT} from "../../context/actionType";
 import {fetcher} from "../../utils/common";
 import { error } from "../../utils/message";
-import {LOGIN_PATH} from "../../utils/constants";
+import {LOGIN_PATH, USER_ROLE} from "../../utils/constants";
 import {Form, Input, Button} from "antd";
 
 
@@ -19,12 +19,19 @@ export default function Login(props) {
             })
             .then((response) => {
                 dispatch({ type: USER_LOGGED_IN, payload: response })
-                setTimeout( () => props.history.push("/"), 1000 )
+                userRole()
             } )
             .catch( e => error( e.message ? e.message : undefined ) )
         }
     }
 
+    const userRole = () => {
+        fetcher(USER_ROLE).then(res => {
+            dispatch({type: SET_USER_ROLE, payload: res.role});
+            setTimeout( () => props.history.push("/"), 1000 );
+        }).catch(e => error( e.message ? e.message : undefined ))
+    }
+
     React.useEffect( () => {
         if (isLoggedIn) setTimeout( () => props.history.push("/"), 1000 )
     }, [])

+ 2 - 0
src/components/public/home.js

@@ -1,7 +1,9 @@
 import React from 'react';
+import {store} from "../../context/store";
 
 
 export default function Home(props) {
+    const {userRole} = React.useContext(store)
 
     return (
         <div className="d-flex justify-content-center align-items-center">

+ 22 - 16
src/components/public/sider.js

@@ -7,7 +7,7 @@ import { Menu } from 'antd';
 
 export default function SideMenu(props) {
 
-    const {isLoggedIn, userName, dispatch} = useContext(store);
+    const {isLoggedIn, userName, userRole, dispatch} = useContext(store);
 
     const onLogout = () => {
         dispatch({type: USER_LOGGED_OUT})
@@ -39,21 +39,27 @@ export default function SideMenu(props) {
                         آزمون ها
                     </NavLinkRouter>
                 </Menu.Item>
-                <Menu.Item key="2">
-                    <NavLinkRouter className="text-white text-decoration-none" to="/sets">
-                        امتحانات
-                    </NavLinkRouter>
-                </Menu.Item>
-                <Menu.Item key="3">
-                    <NavLinkRouter className="text-white text-decoration-none" to="/questions">
-                        سوالات
-                    </NavLinkRouter>
-                </Menu.Item>
-                <Menu.Item key="4">
-                    <NavLinkRouter className="text-white text-decoration-none" to="/tenants">
-                        مدارس
-                    </NavLinkRouter>
-                </Menu.Item>
+                {
+                    userRole !== "student" && (
+                        <>
+                        <Menu.Item key="2">
+                            <NavLinkRouter className="text-white text-decoration-none" to="/sets">
+                                امتحانات
+                            </NavLinkRouter>
+                        </Menu.Item>
+                        <Menu.Item key="3">
+                            <NavLinkRouter className="text-white text-decoration-none" to="/questions">
+                                سوالات
+                            </NavLinkRouter>
+                        </Menu.Item>
+                        <Menu.Item key="4">
+                            <NavLinkRouter className="text-white text-decoration-none" to="/tenants">
+                                مدارس
+                            </NavLinkRouter>
+                        </Menu.Item>
+                        </>
+                    )
+                }
             </Menu>
         </div>
     )

+ 1 - 0
src/context/actionType.js

@@ -1,6 +1,7 @@
 module.exports = {
     USER_LOGGED_IN: "USER_LOGGED_IN",
     USER_LOGGED_OUT: "USER_LOGGED_OUT",
+    SET_USER_ROLE: "SET_USER_ROLE",
     SET_ACTIVE_PROJECT: "SET_ACTIVE_PROJECT",
     SET_ACTIVE_TAG_PROJECT: "SET_ACTIVE_TAG_PROJECT",
     SET_USER_PROFILE_NAME: "SET_USER_PROFILE_NAME",

+ 5 - 0
src/context/reducer.js

@@ -24,6 +24,11 @@ export default (state, action) => {
           userName: null,
         };
 
+      case actionType.SET_USER_ROLE:
+        return {
+          ...state,
+          userRole: action.payload,
+        }
       case actionType.SET_ACTIVE_PROJECT:
         return{
           ...state,

+ 1 - 0
src/context/store.js

@@ -7,6 +7,7 @@ const getInitialState = () => {
         isLoggedIn: Boolean(Token()),
         userName: null,
         postList: [],
+        userRole: null,
     };
 }
 

+ 1 - 0
src/utils/constants.js

@@ -9,6 +9,7 @@ module.exports = {
     BASE_URL: "http://preforms.ir/api",
     PREFIX:"",
     LOGIN_PATH: "/token/",
+    USER_ROLE: "/portal/users/current",
     SETS_LIST: "/portal/sets",
     SETS_DETAIL: (id) => `/portal/sets/${id}`,
     QUESTIONS_LIST: "/portal/questions",