Skip to content

Commit cd6c989

Browse files
Merge pull request #153 from nqviet/issue_143
issue #143
2 parents 69dd861 + f5bfe7d commit cd6c989

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/routers/challenge-list/index.jsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { FeedbackButton, showMenu } from "@topcoder/micro-frontends-earn-app";
99
import actions from "../../actions";
1010
import * as utils from "../../utils";
1111
import store from "../../store";
12-
import { initialChallengeFilter } from "../..//reducers/filter";
12+
import { initialChallengeFilter } from "../../reducers/filter";
1313
import _ from "lodash";
1414

1515
import "react-date-range/dist/theme/default.css";
@@ -29,12 +29,19 @@ const App = () => {
2929

3030
useEffect(() => {
3131
if (!location.search) {
32-
const filterChange = utils.challenge.createEmptyChallengeFilter();
33-
store.dispatch(actions.filter.clearChallengeFilter(filterChange));
34-
store.dispatch(actions.challenges.getChallengesInit());
35-
store.dispatch(
36-
actions.challenges.getChallengesDone(initialChallengeFilter)
37-
);
32+
const currentFilter = store.getState().filter.challenge;
33+
const diff = !_.isEqual(initialChallengeFilter, currentFilter);
34+
35+
if (diff) {
36+
const params = utils.challenge.createChallengeParams(currentFilter);
37+
utils.url.updateQuery(params, true);
38+
} else {
39+
store.dispatch(actions.challenges.getChallengesInit());
40+
store.dispatch(
41+
actions.challenges.getChallengesDone(currentFilter)
42+
);
43+
}
44+
3845
return;
3946
}
4047

src/utils/lifeCycle.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ import * as utils from "../utils";
55
export default function appInit() {
66
let initialQuery;
77
let urlPath;
8+
let firstMounted = true;
89

910
function bootstrap() {
1011
return Promise.resolve().then(() => {
1112
initialQuery = window.location.search;
12-
urlPath = window.location.pathname;
13+
urlPath = utils.url.removeTrailingSlash(window.location.pathname);
1314
});
1415
}
1516

1617
async function mount() {
1718
try {
18-
if (initialQuery) {
19-
const params = utils.url.parseUrlQuery(initialQuery);
20-
const filter = utils.challenge.createChallengeFilter(params);
21-
store.dispatch(action.initApp(filter));
19+
if (firstMounted) {
20+
if (initialQuery && urlPath === '/earn/find/challenges') {
21+
const params = utils.url.parseUrlQuery(initialQuery);
22+
const filter = utils.challenge.createChallengeFilter(params);
23+
store.dispatch(action.initApp(filter));
24+
}
25+
firstMounted = false;
2226
}
2327
} catch (error) {
2428
console.error(error)

src/utils/url.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ export function parseUrlQuery(queryString) {
3737
return params
3838
}
3939

40-
export function updateQuery(params) {
40+
export function updateQuery(params, replace = false) {
4141
const oldQuery = decodeURIComponent(window.location.search);
4242
let query = buildQueryString(params);
4343
query = `?${query.substring(1).split("&").sort().join("&")}`;
4444
if (query !== oldQuery) {
45-
window.history.pushState(window.history.state, "", query);
45+
if (replace) {
46+
window.history.replaceState(window.history.state, "", query);
47+
} else {
48+
window.history.pushState(window.history.state, "", query);
49+
}
4650
}
4751
}
4852

0 commit comments

Comments
 (0)