From 6406ab5d698af8be726b8614050b0f27c81dbc66 Mon Sep 17 00:00:00 2001 From: yoution Date: Sat, 15 May 2021 21:48:59 +0800 Subject: [PATCH 1/3] fix: issue #112 --- src/constants/index.js | 5 +++++ src/hoc/withAuthentication/actions/index.js | 15 ++++++++++++++- src/hoc/withAuthentication/index.js | 17 ++++++++++++++++- src/hoc/withAuthentication/reducers/index.js | 16 ++++++++++++++++ src/routes/JobDetails/index.jsx | 5 ++++- src/services/teams.js | 9 +++++++++ 6 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/constants/index.js b/src/constants/index.js index fa899de3..d1df02de 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -201,6 +201,11 @@ export const ACTION_TYPE = { AUTH_LOAD_TEAM_MEMBERS_ERROR: "AUTH_LOAD_TEAM_MEMBERS_ERROR", AUTH_CLEAR_TEAM_MEMBERS: "AUTH_CLEAR_TEAM_MEMBERS", + AUTH_LOAD_USER_PROFILE: "AUTH_LOAD_USER_PROFILE", + AUTH_LOAD_USER_PROFILE_PENDING: "AUTH_LOAD_USER_PROFILE_PENDING", + AUTH_LOAD_USER_PROFILE_SUCCESS: "AUTH_LOAD_USER_PROFILE_SUCCESS", + AUTH_LOAD_USER_PROFILE_ERROR: "AUTH_LOAD_USER_PROFILE_ERROR", + /* Email Popup */ diff --git a/src/hoc/withAuthentication/actions/index.js b/src/hoc/withAuthentication/actions/index.js index 904e2c25..19360030 100644 --- a/src/hoc/withAuthentication/actions/index.js +++ b/src/hoc/withAuthentication/actions/index.js @@ -2,7 +2,7 @@ * Auth User actions */ import { ACTION_TYPE } from "constants"; -import { getTeamMembers } from "services/teams"; +import { getTeamMembers, getUserProfile } from "services/teams"; /** * Action to set auth user data @@ -40,6 +40,19 @@ export const authLoadTeamMembers = (teamId) => ({ }, }); +/** + * Loads v5 user profile for authentication/permission purposes + * + * @returns {Promise} loaded user profile + */ +export const authLoadUserProfile = (teamId) => ({ + type: ACTION_TYPE.AUTH_LOAD_USER_PROFILE, + payload: async () => { + const res = await getUserProfile(); + return res.data; + }, +}); + /** * Clear team members for authentication/permission purposes * diff --git a/src/hoc/withAuthentication/index.js b/src/hoc/withAuthentication/index.js index a3d76bed..83f8a10c 100644 --- a/src/hoc/withAuthentication/index.js +++ b/src/hoc/withAuthentication/index.js @@ -19,6 +19,7 @@ import { authUserSuccess, authUserError, authLoadTeamMembers, + authLoadUserProfile, authClearTeamMembers, } from "./actions"; import { decodeToken } from "tc-auth-lib"; @@ -34,6 +35,8 @@ export default function withAuthentication(Component) { teamId, teamMembersLoaded, teamMembersLoadingError, + v5UserProfile, + v5UserProfileLoadingError, } = useSelector((state) => state.authUser); const params = useParams(); @@ -86,6 +89,12 @@ export default function withAuthentication(Component) { } }, [params.teamId, teamId, dispatch, isLoggedIn]); + useEffect(() => { + if (isLoggedIn) { + dispatch(authLoadUserProfile()); + } + }, [dispatch, isLoggedIn]); + return ( <> {/* Show loading indicator until we know if user is logged-in or no. @@ -96,8 +105,14 @@ export default function withAuthentication(Component) { ))} + {/* Show component only if v5 user profile load error */} + {isLoggedIn === true && v5UserProfileLoadingError && ( + + )} {/* Show component only if user is logged-in and if we don't need team members or we already loaded them */} - {isLoggedIn === true && (!params.teamId || teamMembersLoaded) ? ( + {isLoggedIn === true && + v5UserProfile && + (!params.teamId || teamMembersLoaded) ? ( ) : null} diff --git a/src/hoc/withAuthentication/reducers/index.js b/src/hoc/withAuthentication/reducers/index.js index 89302a18..c1699c2f 100644 --- a/src/hoc/withAuthentication/reducers/index.js +++ b/src/hoc/withAuthentication/reducers/index.js @@ -16,6 +16,8 @@ const initialState = { teamMembersLoading: undefined, teamMembersLoadingError: undefined, teamMembersLoaded: false, + v5UserProfile: undefined, + v5UserProfileLoadingError: false, }; const authInitialState = _.pick(initialState, [ @@ -95,6 +97,20 @@ const reducer = (state = initialState, action) => { }; } + case ACTION_TYPE.AUTH_LOAD_USER_PROFILE_SUCCESS: { + return { + ...state, + v5UserProfile: action.payload, + v5UserProfileLoadingError: false, + }; + } + case ACTION_TYPE.AUTH_LOAD_USER_PROFILE_ERROR: { + return { + ...state, + v5UserProfileLoadingError: action.payload, + }; + } + default: return state; } diff --git a/src/routes/JobDetails/index.jsx b/src/routes/JobDetails/index.jsx index 908432f8..6e40f805 100644 --- a/src/routes/JobDetails/index.jsx +++ b/src/routes/JobDetails/index.jsx @@ -5,6 +5,7 @@ * It gets `teamId` and `jobId` from the router. */ import React, { useEffect, useState } from "react"; +import { useSelector } from "react-redux"; import PT from "prop-types"; import _ from "lodash"; import Page from "../../components/Page"; @@ -32,6 +33,7 @@ const JobDetails = ({ teamId, jobId }) => { const [job, loadingError] = useData(getJobById, jobId); const [skills] = useData(getSkills); const [skillSet, setSkillSet] = useState(null); + const { id: userId } = useSelector((state) => state.authUser.v5UserProfile); useEffect(() => { if (!!skills && !!job) { @@ -106,7 +108,8 @@ const JobDetails = ({ teamId, jobId }) => { {job.status} - {hasPermission(PERMISSIONS.UPDATE_JOB_NOT_OWN) && ( + {(hasPermission(PERMISSIONS.UPDATE_JOB_NOT_OWN) || + userId === job.createdBy) && (