2
2
* Main App component
3
3
*/
4
4
import React , { useState , useLayoutEffect , useEffect , useRef } from "react" ;
5
- import { Router , useLocation , redirectTo } from "@reach/router" ;
5
+ import { Router , useLocation , Redirect } from "@reach/router" ;
6
6
import Challenges from "./containers/Challenges" ;
7
7
import Filter from "./containers/Filter" ;
8
+ import MyGigs from "./containers/MyGigs" ;
8
9
import Menu from "./components/Menu" ;
9
10
import { disableSidebarForRoute } from "@topcoder/micro-frontends-navbar-app" ;
10
- import Button from "./components/Button" ;
11
11
import * as constants from "./constants" ;
12
12
import actions from "./actions" ;
13
13
import * as utils from "./utils" ;
14
14
import store from "./store" ;
15
15
import { initialChallengeFilter } from "./reducers/filter" ;
16
16
import _ from "lodash" ;
17
+ import { usePreviousLocation } from "./utils/hooks" ;
18
+ import { useSelector } from "react-redux" ;
17
19
18
20
import "react-date-range/dist/theme/default.css" ;
19
21
import "react-date-range/dist/styles.css" ;
@@ -23,6 +25,7 @@ import "./styles/main.scss";
23
25
24
26
const App = ( ) => {
25
27
const [ selectedMenuItem , setSelectedMenuItem ] = useState ( null ) ;
28
+ const isLoggedIn = useSelector ( ( state ) => state . lookup . isLoggedIn ) ;
26
29
27
30
useLayoutEffect ( ( ) => {
28
31
disableSidebarForRoute ( "/earn/*" ) ;
@@ -31,42 +34,58 @@ const App = () => {
31
34
const menu = (
32
35
< Menu
33
36
menu = { constants . NAV_MENU }
34
- icons = { constants . NAV_MENU_ICONS }
35
37
selected = { selectedMenuItem }
36
38
onSelect = { ( item ) => {
37
39
setSelectedMenuItem ( item ) ;
38
40
} }
41
+ isLoggedIn = { isLoggedIn }
39
42
/>
40
43
) ;
41
44
42
45
const location = useLocation ( ) ;
46
+ const previousLocation = usePreviousLocation ( ) ;
43
47
44
48
const getChallengesDebounced = useRef ( _ . debounce ( ( f ) => f ( ) , 500 ) ) ;
45
49
46
50
useEffect ( ( ) => {
47
- if ( ! location . search ) {
48
- store . dispatch ( actions . challenges . getChallenges ( initialChallengeFilter ) ) ;
49
- return ;
50
- }
51
+ store . dispatch ( actions . lookup . checkIsLoggedIn ( ) ) ;
52
+ } , [ ] ) ;
53
+
54
+ useEffect ( ( ) => {
55
+ if ( location . pathname === "/earn/find/challenges" ) {
56
+ if ( ! location . search ) {
57
+ store . dispatch (
58
+ actions . challenges . getChallenges ( initialChallengeFilter )
59
+ ) ;
60
+ return ;
61
+ }
51
62
52
- //if (location.pathname === "/earn/find/challenges") {
53
- const params = utils . url . parseUrlQuery ( location . search ) ;
54
- const toUpdate = utils . challenge . createChallengeFilter ( params ) ;
63
+ const params = utils . url . parseUrlQuery ( location . search ) ;
64
+ const toUpdate = utils . challenge . createChallengeFilter ( params ) ;
55
65
56
- if ( ! toUpdate . types ) toUpdate . types = [ ] ;
57
- if ( ! toUpdate . tracks ) toUpdate . tracks = [ ] ;
58
- if ( ! toUpdate . bucket ) toUpdate . bucket = "" ;
66
+ if ( ! toUpdate . types ) toUpdate . types = [ ] ;
67
+ if ( ! toUpdate . tracks ) toUpdate . tracks = [ ] ;
68
+ if ( ! toUpdate . bucket ) toUpdate . bucket = "" ;
59
69
60
- const updatedFilter = { ...initialChallengeFilter , ...toUpdate } ;
61
- const currentFilter = store . getState ( ) . filter . challenge ;
62
- const diff = ! _ . isEqual ( updatedFilter , currentFilter ) ;
63
- if ( diff ) {
64
- store . dispatch ( actions . filter . updateFilter ( updatedFilter ) ) ;
70
+ const updatedFilter = { ...initialChallengeFilter , ...toUpdate } ;
71
+ const currentFilter = store . getState ( ) . filter . challenge ;
72
+ const diff = ! _ . isEqual ( updatedFilter , currentFilter ) ;
73
+ if ( diff ) {
74
+ store . dispatch ( actions . filter . updateFilter ( updatedFilter ) ) ;
75
+ }
76
+ getChallengesDebounced . current ( ( ) =>
77
+ store . dispatch ( actions . challenges . getChallenges ( updatedFilter ) )
78
+ ) ;
79
+ }
80
+ } , [ location ] ) ;
81
+
82
+ const varsRef = useRef ( ) ;
83
+ varsRef . current = { previousLocation } ;
84
+
85
+ useEffect ( ( ) => {
86
+ if ( location . pathname !== varsRef . current . previousLocation . pathname ) {
87
+ window . scrollTo ( 0 , 0 ) ;
65
88
}
66
- getChallengesDebounced . current ( ( ) =>
67
- store . dispatch ( actions . challenges . getChallenges ( updatedFilter ) )
68
- ) ;
69
- //}
70
89
} , [ location ] ) ;
71
90
72
91
useEffect ( ( ) => {
@@ -82,29 +101,34 @@ const App = () => {
82
101
} , [ location ] ) ;
83
102
84
103
return (
85
- < div className = "layout" >
86
- < aside className = "sidebar" >
87
- < div className = "sidebar-content" >
88
- { menu }
89
- < hr />
90
- < Filter />
91
- </ div >
92
- < div className = "sidebar-footer" >
93
- < a
94
- className = "button button-primary"
95
- href = "https://github.com/topcoder-platform/micro-frontends-earn-app/issues/new?assignees=& labels = & template = bug_report . md & title = "
96
- target = "_blank"
97
- >
98
- GIVE APPLICATION FEEDBACK
99
- </ a >
104
+ < >
105
+ < div className = "layout" >
106
+ < aside className = "sidebar" >
107
+ < div className = "sidebar-content" >
108
+ { menu }
109
+ < hr />
110
+ < Filter />
111
+ </ div >
112
+ < div className = "sidebar-footer" >
113
+ < a
114
+ className = "button button-primary"
115
+ href = "https://github.com/topcoder-platform/micro-frontends-earn-app/issues/new?assignees=& labels = & template = bug_report . md & title = "
116
+ target = "_blank"
117
+ >
118
+ GIVE APPLICATION FEEDBACK
119
+ </ a >
120
+ </ div >
121
+ </ aside >
122
+ < div className = "content" >
123
+ < Router >
124
+ < Challenges path = "/earn/find/challenges" />
125
+ < MyGigs path = "/earn/my-gigs" />
126
+ < Redirect from = "/earn/*" to = "/earn/find/challenges" noThrow />
127
+ </ Router >
100
128
</ div >
101
- </ aside >
102
- < div className = "content" >
103
- < Router >
104
- < Challenges path = "/earn/*" />
105
- </ Router >
106
129
</ div >
107
- </ div >
130
+ < div id = "tooltips-container-id" />
131
+ </ >
108
132
) ;
109
133
} ;
110
134
0 commit comments