22 * Main App component
33 */
44import React , { useState , useLayoutEffect , useEffect , useRef } from "react" ;
5- import { Router , useLocation , redirectTo } from "@reach/router" ;
5+ import { Router , useLocation , Redirect } from "@reach/router" ;
66import Challenges from "./containers/Challenges" ;
77import Filter from "./containers/Filter" ;
8+ import MyGigs from "./containers/MyGigs" ;
89import Menu from "./components/Menu" ;
910import { disableSidebarForRoute } from "@topcoder/micro-frontends-navbar-app" ;
10- import Button from "./components/Button" ;
1111import * as constants from "./constants" ;
1212import actions from "./actions" ;
1313import * as utils from "./utils" ;
1414import store from "./store" ;
1515import { initialChallengeFilter } from "./reducers/filter" ;
1616import _ from "lodash" ;
17+ import { usePreviousLocation } from "./utils/hooks" ;
18+ import { useSelector } from "react-redux" ;
1719
1820import "react-date-range/dist/theme/default.css" ;
1921import "react-date-range/dist/styles.css" ;
@@ -23,6 +25,7 @@ import "./styles/main.scss";
2325
2426const App = ( ) => {
2527 const [ selectedMenuItem , setSelectedMenuItem ] = useState ( null ) ;
28+ const isLoggedIn = useSelector ( ( state ) => state . lookup . isLoggedIn ) ;
2629
2730 useLayoutEffect ( ( ) => {
2831 disableSidebarForRoute ( "/earn/*" ) ;
@@ -31,42 +34,58 @@ const App = () => {
3134 const menu = (
3235 < Menu
3336 menu = { constants . NAV_MENU }
34- icons = { constants . NAV_MENU_ICONS }
3537 selected = { selectedMenuItem }
3638 onSelect = { ( item ) => {
3739 setSelectedMenuItem ( item ) ;
3840 } }
41+ isLoggedIn = { isLoggedIn }
3942 />
4043 ) ;
4144
4245 const location = useLocation ( ) ;
46+ const previousLocation = usePreviousLocation ( ) ;
4347
4448 const getChallengesDebounced = useRef ( _ . debounce ( ( f ) => f ( ) , 500 ) ) ;
4549
4650 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+ }
5162
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 ) ;
5565
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 = "" ;
5969
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 ) ;
6588 }
66- getChallengesDebounced . current ( ( ) =>
67- store . dispatch ( actions . challenges . getChallenges ( updatedFilter ) )
68- ) ;
69- //}
7089 } , [ location ] ) ;
7190
7291 useEffect ( ( ) => {
@@ -82,29 +101,34 @@ const App = () => {
82101 } , [ location ] ) ;
83102
84103 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 >
100128 </ div >
101- </ aside >
102- < div className = "content" >
103- < Router >
104- < Challenges path = "/earn/*" />
105- </ Router >
106129 </ div >
107- </ div >
130+ < div id = "tooltips-container-id" />
131+ </ >
108132 ) ;
109133} ;
110134
0 commit comments