Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

fix: issue #112 #262

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_V5_USER_PROFILE: "AUTH_LOAD_V5_USER_PROFILE",
AUTH_LOAD_V5_USER_PROFILE_PENDING: "AUTH_LOAD_V5_USER_PROFILE_PENDING",
AUTH_LOAD_V5_USER_PROFILE_SUCCESS: "AUTH_LOAD_V5_USER_PROFILE_SUCCESS",
AUTH_LOAD_V5_USER_PROFILE_ERROR: "AUTH_LOAD_V5_USER_PROFILE_ERROR",

/*
Email Popup
*/
Expand Down
15 changes: 14 additions & 1 deletion src/hoc/withAuthentication/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +40,19 @@ export const authLoadTeamMembers = (teamId) => ({
},
});

/**
* Loads v5 user profile for authentication/permission purposes
*
* @returns {Promise} loaded user profile
*/
export const authLoadUserProfile = () => ({
type: ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE,
payload: async () => {
const res = await getUserProfile();
return res.data;
},
});

/**
* Clear team members for authentication/permission purposes
*
Expand Down
17 changes: 16 additions & 1 deletion src/hoc/withAuthentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
authUserSuccess,
authUserError,
authLoadTeamMembers,
authLoadUserProfile,
authClearTeamMembers,
} from "./actions";
import { decodeToken } from "tc-auth-lib";
Expand All @@ -34,6 +35,8 @@ export default function withAuthentication(Component) {
teamId,
teamMembersLoaded,
teamMembersLoadingError,
v5UserProfile,
v5UserProfileLoadingError,
} = useSelector((state) => state.authUser);
const params = useParams();

Expand Down Expand Up @@ -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.
Expand All @@ -96,8 +105,14 @@ export default function withAuthentication(Component) {
<LoadingIndicator error={authError || teamMembersLoadingError} />
))}

{/* Show component only if v5 user profile load error */}
{isLoggedIn === true && v5UserProfileLoadingError && (
<LoadingIndicator error={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) ? (
<Component {...props} />
) : null}
</>
Expand Down
16 changes: 16 additions & 0 deletions src/hoc/withAuthentication/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const initialState = {
teamMembersLoading: undefined,
teamMembersLoadingError: undefined,
teamMembersLoaded: false,
v5UserProfile: undefined,
v5UserProfileLoadingError: false,
};

const authInitialState = _.pick(initialState, [
Expand Down Expand Up @@ -95,6 +97,20 @@ const reducer = (state = initialState, action) => {
};
}

case ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE_SUCCESS: {
return {
...state,
v5UserProfile: action.payload,
v5UserProfileLoadingError: false,
};
}
case ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE_ERROR: {
return {
...state,
v5UserProfileLoadingError: action.payload,
};
}

default:
return state;
}
Expand Down
5 changes: 4 additions & 1 deletion src/routes/JobDetails/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -106,7 +108,8 @@ const JobDetails = ({ teamId, jobId }) => {
{job.status}
</DataItem>
</div>
{hasPermission(PERMISSIONS.UPDATE_JOB_NOT_OWN) && (
{(hasPermission(PERMISSIONS.UPDATE_JOB_NOT_OWN) ||
userId === job.createdBy) && (
<div styleName="actions">
<Button
target="_blank"
Expand Down
9 changes: 9 additions & 0 deletions src/services/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export const getMyTeams = (name, page = 1, perPage) => {
return axios.get(`${config.API.V5}/taas-teams?${query}`);
};

/**
* Get v5 user profile.
*
* @returns {Promise<{}>} user profile object
*/
export const getUserProfile = () => {
return axios.get(`${config.API.V5}/taas-teams/me`);
};

/**
* Get team by id.
*
Expand Down