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

Commit 89e819b

Browse files
authored
Merge pull request #323 from topcoder-platform/role-quick-fixes
Role quick fixes - navigation to landing and keeping roles selected in localStorage until they are submitted
2 parents 59410d7 + 4eeba9d commit 89e819b

File tree

12 files changed

+126
-127
lines changed

12 files changed

+126
-127
lines changed
Loading
+22-16
Loading
+26-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { ACTION_TYPE } from "constants";
22

3-
export const clearSearchedRoles = () => ({
3+
const updateLocalStorage = (state) => {
4+
try {
5+
localStorage.setItem("rolesState", JSON.stringify(state));
6+
} catch {
7+
console.error("Unable to set localStorage");
8+
}
9+
};
10+
11+
const clearRoles = () => ({
412
type: ACTION_TYPE.CLEAR_SEARCHED_ROLES,
513
});
614

7-
export const addSearchedRole = (searchedRole) => ({
15+
const addRole = (searchedRole) => ({
816
type: ACTION_TYPE.ADD_SEARCHED_ROLE,
917
payload: searchedRole,
1018
});
1119

12-
export const addRoleSearchId = (id) => ({
20+
const addPreviousSearchId = (id) => ({
1321
type: ACTION_TYPE.ADD_ROLE_SEARCH_ID,
1422
payload: id,
1523
});
@@ -18,3 +26,18 @@ export const replaceSearchedRoles = (roles) => ({
1826
type: ACTION_TYPE.REPLACE_SEARCHED_ROLES,
1927
payload: { roles, lastRoleId: roles[roles.length - 1].searchId },
2028
});
29+
30+
export const clearSearchedRoles = () => (dispatch, getState) => {
31+
dispatch(clearRoles());
32+
updateLocalStorage(getState().searchedRoles);
33+
};
34+
35+
export const addSearchedRole = (searchedRole) => (dispatch, getState) => {
36+
dispatch(addRole(searchedRole));
37+
updateLocalStorage(getState().searchedRoles);
38+
};
39+
40+
export const addRoleSearchId = (id) => (dispatch, getState) => {
41+
dispatch(addPreviousSearchId(id));
42+
updateLocalStorage(getState().searchedRoles);
43+
};

src/routes/CreateNewTeam/components/NoMatchingProfilesResultCard/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function NoMatchingProfilesResultCard() {
2828
<p styleName="cost">$1,200</p>
2929
<p>/Week</p>
3030
</div>
31-
<Link to="/taas/myteams/createnewteam" state={{ keepAddedRoles: true }}>
31+
<Link to="/taas/myteams/createnewteam">
3232
<Button type="secondary" styleName="button">
3333
Modify Search Criteria
3434
</Button>

src/routes/CreateNewTeam/components/SearchAndSubmit/index.jsx

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
import { Router } from "@reach/router";
22
import React from "react";
3+
import { useSelector } from "react-redux";
34
import SearchContainer from "../SearchContainer";
45
import SubmitContainer from "../SubmitContainer";
56

67
function SearchAndSubmit(props) {
8+
const { addedRoles, previousSearchId } = useSelector(
9+
(state) => state.searchedRoles
10+
);
11+
712
return (
813
<Router>
9-
<SearchContainer path="/" {...props} />
10-
<SubmitContainer path="result" {...props} />
14+
<SearchContainer
15+
path="/"
16+
addedRoles={addedRoles}
17+
previousSearchId={previousSearchId}
18+
{...props}
19+
/>
20+
<SubmitContainer path="result" addedRoles={addedRoles} {...props} />
1121
</Router>
1222
);
1323
}

src/routes/CreateNewTeam/components/SearchContainer/index.jsx

+6-30
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import React, { useCallback, useState } from "react";
99
import PT from "prop-types";
1010
import _ from "lodash";
11-
import { useDispatch, useSelector } from "react-redux";
11+
import { useDispatch } from "react-redux";
1212
import AddedRolesAccordion from "../AddedRolesAccordion";
1313
import Completeness from "../Completeness";
1414
import SearchCard from "../SearchCard";
@@ -19,50 +19,25 @@ import { setCurrentStage } from "utils/helpers";
1919
import { addRoleSearchId, addSearchedRole } from "../../actions";
2020
import "./styles.module.scss";
2121

22-
/**
23-
* Converts an array of role search objects to two data
24-
* lists which can be set as sessionStorage items
25-
*
26-
* @param {object[]} arrayOfObjects array of role objects
27-
*/
28-
const storeStrings = (arrayOfObjects) => {
29-
const objectOfArrays = arrayOfObjects.reduce(
30-
(acc, curr) => ({
31-
searchId: [...acc.searchId, curr.searchId],
32-
name: [...acc.name, curr.name],
33-
}),
34-
{ searchId: [], name: [] }
35-
);
36-
37-
sessionStorage.setItem("searchIds", objectOfArrays.searchId.join(","));
38-
sessionStorage.setItem("roleNames", objectOfArrays.name.join(","));
39-
};
40-
4122
function SearchContainer({
4223
stages,
4324
setStages,
4425
isCompletenessDisabled,
4526
toRender,
4627
searchObject,
4728
completenessStyle,
48-
reloadRolesPage,
4929
navigate,
30+
addedRoles,
31+
previousSearchId,
5032
}) {
51-
const { addedRoles, previousSearchId } = useSelector(
52-
(state) => state.searchedRoles
53-
);
54-
5533
const [searchState, setSearchState] = useState(null);
5634
const [matchingRole, setMatchingRole] = useState(null);
57-
const [addAnotherModalOpen, setAddAnotherModalOpen] = useState(false);
58-
const [submitDone, setSubmitDone] = useState(true);
5935

6036
const dispatch = useDispatch();
6137

6238
const onSubmit = useCallback(() => {
63-
storeStrings(addedRoles);
6439
navigate("result", { state: { matchingRole } });
65-
}, [addedRoles, navigate, matchingRole]);
40+
}, [navigate, matchingRole]);
6641

6742
const search = () => {
6843
setCurrentStage(1, stages, setStages);
@@ -135,8 +110,9 @@ SearchContainer.propTypes = {
135110
searchObject: PT.object,
136111
toRender: PT.node,
137112
completenessStyle: PT.string,
138-
reloadRolesPage: PT.func,
139113
navigate: PT.func,
114+
addedRoles: PT.array,
115+
previousSearchId: PT.string,
140116
};
141117

142118
export default SearchContainer;

src/routes/CreateNewTeam/components/SubmitContainer/index.jsx

+22-43
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
* Requires authentication to complete submission process
55
* and contains a series of popups to lead user through the flow.
66
*/
7-
import React, { useCallback, useEffect, useState } from "react";
7+
import React, {
8+
useCallback,
9+
useEffect,
10+
useLayoutEffect,
11+
useState,
12+
} from "react";
813
import PT from "prop-types";
9-
import { useDispatch, useSelector } from "react-redux";
14+
import { useDispatch } from "react-redux";
1015
import _ from "lodash";
1116
import { toastr } from "react-redux-toastr";
1217
import { navigate } from "@reach/router";
@@ -19,46 +24,19 @@ import ConfirmationModal from "../ConfirmationModal";
1924
import withAuthentication from "../../../../hoc/withAuthentication";
2025
import "./styles.module.scss";
2126
import { setCurrentStage } from "utils/helpers";
22-
import { clearSearchedRoles, replaceSearchedRoles } from "../../actions";
27+
import { clearSearchedRoles } from "../../actions";
2328
import { postTeamRequest } from "services/teams";
2429
import SuccessCard from "../SuccessCard";
2530

26-
const retrieveRoles = () => {
27-
const searchIdString = sessionStorage.getItem("searchIds");
28-
const nameString = sessionStorage.getItem("roleNames");
29-
30-
if (!searchIdString || !nameString) return [];
31-
const searchIds = searchIdString.split(",");
32-
const names = nameString.split(",");
33-
if (searchIds.length !== names.length) return [];
34-
35-
const roles = [];
36-
for (let i = 0; i < searchIds.length; i++) {
37-
roles.push({
38-
searchId: searchIds[i],
39-
name: names[i],
40-
});
41-
}
42-
43-
return roles;
44-
};
45-
46-
const clearSessionKeys = () => {
47-
sessionStorage.removeItem("searchIds");
48-
sessionStorage.removeItem("roleNames");
49-
};
50-
5131
function SubmitContainer({
5232
stages,
5333
setStages,
5434
completenessStyle,
55-
reloadRolesPage,
5635
location,
36+
addedRoles,
5737
}) {
5838
const matchingRole = location?.state?.matchingRole;
5939

60-
const { addedRoles } = useSelector((state) => state.searchedRoles);
61-
6240
const [addAnotherOpen, setAddAnotherOpen] = useState(true);
6341
const [teamDetailsOpen, setTeamDetailsOpen] = useState(false);
6442
const [teamObject, setTeamObject] = useState(null);
@@ -68,11 +46,17 @@ function SubmitContainer({
6846

6947
useEffect(() => {
7048
setCurrentStage(2, stages, setStages);
71-
const storedRoles = retrieveRoles();
72-
if (storedRoles) {
73-
if (!addedRoles || storedRoles.length > addedRoles.length) {
74-
dispatch(replaceSearchedRoles(storedRoles));
75-
}
49+
if (!addedRoles || addedRoles.length === 0) {
50+
navigate("/taas/myteams/createnewteam");
51+
}
52+
// eslint-disable-next-line react-hooks/exhaustive-deps
53+
}, []);
54+
55+
// redirects user if they enter the page URL directly
56+
// without adding any roles.
57+
useLayoutEffect(() => {
58+
if (!addedRoles || addedRoles.length === 0) {
59+
navigate("/taas/myteams/createnewteam");
7660
}
7761
// eslint-disable-next-line react-hooks/exhaustive-deps
7862
}, []);
@@ -83,11 +67,7 @@ function SubmitContainer({
8367
};
8468

8569
const addAnother = () => {
86-
if (reloadRolesPage) {
87-
setCurrentStage(0, stages, setStages);
88-
reloadRolesPage();
89-
}
90-
navigate("/taas/myteams/createnewteam/role");
70+
navigate("/taas/myteams/createnewteam");
9171
};
9272

9373
const assembleTeam = (formData) => {
@@ -121,7 +101,6 @@ function SubmitContainer({
121101
postTeamRequest(teamObject)
122102
.then((res) => {
123103
const projectId = _.get(res, ["data", "projectId"]);
124-
clearSessionKeys();
125104
dispatch(clearSearchedRoles());
126105
navigate(`/taas/myteams/${projectId}`);
127106
})
@@ -171,8 +150,8 @@ SubmitContainer.propTypes = {
171150
stages: PT.array,
172151
setStages: PT.func,
173152
completenessStyle: PT.string,
174-
reloadRolesPage: PT.bool,
175153
location: PT.object,
154+
addedRoles: PT.array,
176155
};
177156

178157
export default withAuthentication(SubmitContainer);

src/routes/CreateNewTeam/components/SuccessCard/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function SuccessCard() {
3030
Please use the button to the right to submit your request, or the
3131
button below to search for additional roles.
3232
</p>
33-
<Link to="/taas/myteams/createnewteam" state={{ keepAddedRoles: true }}>
33+
<Link to="/taas/myteams/createnewteam">
3434
<Button type="secondary" styleName="button">
3535
Continue Search
3636
</Button>

src/routes/CreateNewTeam/index.jsx

+1-19
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
/**
22
* Create New Team
33
*
4-
* Gets location state from router to pass
5-
* along to search pages
6-
*
74
* Landing page for creating new teams
85
* by selecting a role, inputting skills,
96
* or inputting a job description
107
*/
118
import React, { useEffect } from "react";
129
import { navigate } from "@reach/router";
1310
import _ from "lodash";
14-
import PT from "prop-types";
15-
import { useDispatch } from "react-redux";
1611
import Page from "components/Page";
1712
import PageHeader from "components/PageHeader";
1813
import LandingBox from "./components/LandingBox";
1914
import IconMultipleActionsCheck from "../../assets/images/icon-multiple-actions-check-2.svg";
2015
import IconListQuill from "../../assets/images/icon-list-quill.svg";
2116
import IconOfficeFileText from "../../assets/images/icon-office-file-text.svg";
22-
import { clearSearchedRoles } from "./actions";
23-
24-
function CreateNewTeam({ location: { state: locationState } }) {
25-
const dispatch = useDispatch();
26-
27-
useEffect(() => {
28-
if (!locationState || !locationState.keepAddedRoles) {
29-
dispatch(clearSearchedRoles());
30-
}
31-
});
3217

18+
function CreateNewTeam() {
3319
const goToRoute = (path) => {
3420
navigate(path);
3521
};
@@ -65,8 +51,4 @@ function CreateNewTeam({ location: { state: locationState } }) {
6551
);
6652
}
6753

68-
CreateNewTeam.propTypes = {
69-
locationState: PT.object,
70-
};
71-
7254
export default CreateNewTeam;

0 commit comments

Comments
 (0)