11import React , { useEffect , useRef , useState } from "react" ;
2+ import { useLocation } from "@reach/router" ;
23import PT from "prop-types" ;
34import { connect } from "react-redux" ;
45import Modal from "../../components/Modal" ;
56import Button from "../../components/Button" ;
67import JobListing from "./JobListing" ;
78import actions from "../../actions" ;
89import { EMPTY_GIGS_TEXT } from "../../constants" ;
10+ import * as utils from "../../utils" ;
911
1012import UpdateGigProfile from "./modals/UpdateGigProfile" ;
1113import UpdateSuccess from "./modals/UpdateSuccess" ;
@@ -23,16 +25,41 @@ const MyGigs = ({
2325 updateProfile,
2426 updateProfileSuccess,
2527 getAllCountries,
28+ checkingGigs,
29+ startCheckingGigs,
2630} ) => {
31+ const location = useLocation ( ) ;
32+ const params = utils . url . parseUrlQuery ( location . search ) ;
2733 const propsRef = useRef ( ) ;
28- propsRef . current = { getMyGigs, getProfile, getAllCountries } ;
34+ propsRef . current = {
35+ getMyGigs,
36+ getProfile,
37+ getAllCountries,
38+ startCheckingGigs,
39+ params,
40+ } ;
2941
3042 useEffect ( ( ) => {
31- propsRef . current . getMyGigs ( ) ;
3243 propsRef . current . getProfile ( ) ;
3344 propsRef . current . getAllCountries ( ) ;
45+ if ( propsRef . current . params . externalId ) {
46+ propsRef . current . startCheckingGigs ( propsRef . current . params . externalId ) ;
47+ } else {
48+ propsRef . current . getMyGigs ( ) ;
49+ }
3450 } , [ ] ) ;
3551
52+ const isInitialMount = useRef ( true ) ;
53+ useEffect ( ( ) => {
54+ if ( isInitialMount . current ) {
55+ isInitialMount . current = false ;
56+ return ;
57+ }
58+ if ( ! checkingGigs ) {
59+ propsRef . current . getMyGigs ( ) ;
60+ }
61+ } , [ checkingGigs ] ) ;
62+
3663 const [ openUpdateProfile , setOpenUpdateProfile ] = useState ( false ) ;
3764 const [ openUpdateSuccess , setOpenUpdateSuccess ] = useState ( false ) ;
3865
@@ -106,9 +133,12 @@ MyGigs.propTypes = {
106133 updateProfile : PT . func ,
107134 updateProfileSuccess : PT . bool ,
108135 getAllCountries : PT . func ,
136+ checkingGigs : PT . bool ,
137+ startCheckingGigs : PT . func ,
109138} ;
110139
111140const mapStateToProps = ( state ) => ( {
141+ checkingGigs : state . myGigs . checkingGigs ,
112142 myGigs : state . myGigs . myGigs ,
113143 total : state . myGigs . total ,
114144 numLoaded : state . myGigs . numLoaded ,
@@ -122,6 +152,7 @@ const mapDispatchToProps = {
122152 getProfile : actions . myGigs . getProfile ,
123153 updateProfile : actions . myGigs . updateProfile ,
124154 getAllCountries : actions . lookup . getAllCountries ,
155+ startCheckingGigs : actions . myGigs . startCheckingGigs ,
125156} ;
126157
127158export default connect ( mapStateToProps , mapDispatchToProps ) ( MyGigs ) ;
0 commit comments