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 all 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, getV5UserProfile } 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 authLoadV5UserProfile = () => ({
type: ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE,
payload: async () => {
const res = await getV5UserProfile();
return res.data;
},
});

/**
* Clear team members for authentication/permission purposes
*
Expand Down
19 changes: 16 additions & 3 deletions 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,
authLoadV5UserProfile,
authClearTeamMembers,
} from "./actions";
import { decodeToken } from "tc-auth-lib";
Expand All @@ -34,6 +35,9 @@ export default function withAuthentication(Component) {
teamId,
teamMembersLoaded,
teamMembersLoadingError,
v5UserProfile,
v5UserProfileLoading,
v5UserProfileLoadingError,
} = useSelector((state) => state.authUser);
const params = useParams();

Expand Down Expand Up @@ -86,18 +90,27 @@ export default function withAuthentication(Component) {
}
}, [params.teamId, teamId, dispatch, isLoggedIn]);

useEffect(() => {
if (isLoggedIn) {
dispatch(authLoadV5UserProfile());
}
}, [dispatch, isLoggedIn]);

return (
<>
{/* Show loading indicator until we know if user is logged-in or no.
Also, show loading indicator if we need to know team members but haven't loaded them yet.
or load v5 user profile but haven't loaded them yet.
In we got error during this process, show error */}
{isLoggedIn === null ||
(params.teamId && !teamMembersLoaded && (
<LoadingIndicator error={authError || teamMembersLoadingError} />
((params.teamId && !teamMembersLoaded || v5UserProfileLoading || v5UserProfileLoadingError)&& (
<LoadingIndicator error={authError || teamMembersLoadingError || 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
25 changes: 25 additions & 0 deletions src/hoc/withAuthentication/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const initialState = {
teamMembersLoading: undefined,
teamMembersLoadingError: undefined,
teamMembersLoaded: false,
v5UserProfile: undefined,
v5UserProfileLoading: false,
v5UserProfileLoadingError: false,
};

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

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

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 getV5UserProfile = () => {
return axios.get(`${config.API.V5}/taas-teams/me`);
};

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