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

Commit 910927b

Browse files
cache data done
1 parent 9e08983 commit 910927b

File tree

4 files changed

+245
-30
lines changed

4 files changed

+245
-30
lines changed

src/App.jsx

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,22 @@ const App = () => {
8787
useEffect(() => {
8888
if (location.pathname === "/earn/my-gigs" && isLoggedIn) {
8989
if (!location.search) {
90+
const cachedGigs = store.getState().myGigs[initialGigFilter.status];
91+
if (cachedGigs.myGigs && cachedGigs.myGigs.length !== 0) {
92+
return;
93+
}
9094
store.dispatch(actions.filter.updateGigFilter(initialGigFilter));
9195

9296
store.dispatch(
93-
actions.myGigs.getMyGigs(
97+
actions.myGigs.getMyOpenGigs(
9498
constants.GIGS_FILTER_STATUSES_PARAM[initialGigFilter.status]
9599
)
96100
);
97101
return;
98102
}
99103
const params = utils.url.parseUrlQuery(location.search);
100104
if (_.keys(params).length == 1 && params.externalId) {
105+
store.dispatch(actions.myGigs.startCheckingGigs(params.externalId));
101106
return;
102107
}
103108
const updatedGigFilter = {
@@ -108,13 +113,50 @@ const App = () => {
108113
if (diff) {
109114
store.dispatch(actions.filter.updateGigFilter(updatedGigFilter));
110115
}
111-
getDataDebounced.current(() =>
112-
store.dispatch(
113-
actions.myGigs.getMyGigs(
114-
constants.GIGS_FILTER_STATUSES_PARAM[updatedGigFilter.status]
115-
)
116-
)
117-
);
116+
const cachedGigs = store.getState().myGigs[updatedGigFilter.status];
117+
if (cachedGigs.myGigs && cachedGigs.myGigs.length !== 0) {
118+
return;
119+
}
120+
getDataDebounced.current(() => {
121+
if (
122+
updatedGigFilter.status == constants.GIGS_FILTER_STATUSES.ACTIVE_JOBS
123+
) {
124+
store.dispatch(
125+
actions.myGigs.getMyActiveGigs(
126+
constants.GIGS_FILTER_STATUSES_PARAM[updatedGigFilter.status]
127+
)
128+
);
129+
}
130+
if (
131+
updatedGigFilter.status == constants.GIGS_FILTER_STATUSES.OPEN_JOBS
132+
) {
133+
store.dispatch(
134+
actions.myGigs.getMyOpenGigs(
135+
constants.GIGS_FILTER_STATUSES_PARAM[updatedGigFilter.status]
136+
)
137+
);
138+
}
139+
if (
140+
updatedGigFilter.status ==
141+
constants.GIGS_FILTER_STATUSES.COMPLETED_JOBS
142+
) {
143+
store.dispatch(
144+
actions.myGigs.getMyCompletedGigs(
145+
constants.GIGS_FILTER_STATUSES_PARAM[updatedGigFilter.status]
146+
)
147+
);
148+
}
149+
if (
150+
updatedGigFilter.status ==
151+
constants.GIGS_FILTER_STATUSES.ARCHIVED_JOBS
152+
) {
153+
store.dispatch(
154+
actions.myGigs.getMyArchivedGigs(
155+
constants.GIGS_FILTER_STATUSES_PARAM[updatedGigFilter.status]
156+
)
157+
);
158+
}
159+
});
118160
}
119161
}, [location, isLoggedIn]);
120162

src/actions/myGigs.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,38 @@ async function getMyGigs(status = "open_jobs", page = 1, perPage = PER_PAGE) {
1616
return service.getMyGigs(status, page, perPage);
1717
}
1818

19+
async function getMyActiveGigs(
20+
status = "active_jobs",
21+
page = 1,
22+
perPage = PER_PAGE
23+
) {
24+
return service.getMyGigs(status, page, perPage);
25+
}
26+
27+
async function getMyOpenGigs(
28+
status = "open_jobs",
29+
page = 1,
30+
perPage = PER_PAGE
31+
) {
32+
return service.getMyGigs(status, page, perPage);
33+
}
34+
35+
async function getMyCompletedGigs(
36+
status = "completed_jobs",
37+
page = 1,
38+
perPage = PER_PAGE
39+
) {
40+
return service.getMyGigs(status, page, perPage);
41+
}
42+
43+
async function getMyArchivedGigsDone(
44+
status = "archived_jobs",
45+
page = 1,
46+
perPage = PER_PAGE
47+
) {
48+
return service.getMyGigs(status, page, perPage);
49+
}
50+
1951
/**
2052
* Action to load more pages of my gigs
2153
* @param {number} nextPage page to fetch
@@ -54,6 +86,10 @@ async function startCheckingGigs(externalId) {
5486
}
5587

5688
export default createActions({
89+
GET_MY_ACTIVE_GIGS: getMyActiveGigs,
90+
GET_MY_OPEN_GIGS: getMyOpenGigs,
91+
GET_MY_COMPLETED_GIGS: getMyCompletedGigs,
92+
GET_MY_ARCHIVED_GIGS: getMyArchivedGigsDone,
5793
GET_MY_GIGS: getMyGigs,
5894
LOAD_MORE_MY_GIGS: loadMoreMyGigs,
5995
GET_PROFILE: getProfile,

src/containers/MyGigs/index.jsx

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import Loading from "../../components/Loading";
88
import Empty from "../../components/Empty";
99
import JobListing from "./JobListing";
1010
import actions from "../../actions";
11-
import * as utils from "../../utils";
11+
import * as constants from "../../constants";
12+
// import * as utils from "../../utils";
1213

1314
import UpdateGigProfile from "./modals/UpdateGigProfile";
1415
import UpdateSuccess from "./modals/UpdateSuccess";
@@ -21,6 +22,10 @@ const MyGigs = ({
2122
loadMore,
2223
total,
2324
numLoaded,
25+
myActiveGigs,
26+
myOpenGigs,
27+
myCompletedGigs,
28+
myArchivedGigs,
2429
profile,
2530
getProfile,
2631
updateProfile,
@@ -29,26 +34,22 @@ const MyGigs = ({
2934
checkingGigs,
3035
startCheckingGigs,
3136
gigStatus,
37+
loadingMyGigs,
3238
}) => {
33-
const location = useLocation();
34-
const params = utils.url.parseUrlQuery(location.search);
39+
// const location = useLocation();
40+
// const params = utils.url.parseUrlQuery(location.search);
3541
const propsRef = useRef();
3642
propsRef.current = {
3743
getMyGigs,
3844
getProfile,
3945
getAllCountries,
4046
startCheckingGigs,
41-
params,
47+
// params,
4248
};
4349

4450
useEffect(() => {
4551
propsRef.current.getProfile();
4652
propsRef.current.getAllCountries();
47-
if (propsRef.current.params.externalId) {
48-
propsRef.current.startCheckingGigs(propsRef.current.params.externalId);
49-
} else {
50-
// propsRef.current.getMyGigs();
51-
}
5253
}, []);
5354

5455
const isInitialMount = useRef(true);
@@ -64,6 +65,22 @@ const MyGigs = ({
6465

6566
const [openUpdateProfile, setOpenUpdateProfile] = useState(false);
6667
const [openUpdateSuccess, setOpenUpdateSuccess] = useState(false);
68+
const [currentGigs, setCurrentGigs] = useState({});
69+
70+
useEffect(() => {
71+
if (gigStatus == constants.GIGS_FILTER_STATUSES.ACTIVE_JOBS) {
72+
setCurrentGigs(myActiveGigs);
73+
}
74+
if (gigStatus == constants.GIGS_FILTER_STATUSES.OPEN_JOBS) {
75+
setCurrentGigs(myOpenGigs);
76+
}
77+
if (gigStatus == constants.GIGS_FILTER_STATUSES.COMPLETED_JOBS) {
78+
setCurrentGigs(myCompletedGigs);
79+
}
80+
if (gigStatus == constants.GIGS_FILTER_STATUSES.ARCHIVED_JOBS) {
81+
setCurrentGigs(myArchivedGigs);
82+
}
83+
}, [gigStatus, myActiveGigs, myOpenGigs, myCompletedGigs, myArchivedGigs]);
6784

6885
useEffect(() => {
6986
if (updateProfileSuccess) {
@@ -99,19 +116,22 @@ const MyGigs = ({
99116
</Button>
100117
</div>
101118
</h1>
102-
{!checkingGigs && myGigs && myGigs.length == 0 && (
103-
<Empty gigStatus={gigStatus} />
104-
)}
105-
{!checkingGigs && myGigs && myGigs.length > 0 && (
106-
<JobListing
107-
gigStatus={gigStatus}
108-
jobs={myGigs}
109-
loadMore={loadMore}
110-
total={total}
111-
numLoaded={numLoaded}
112-
/>
113-
)}
114-
{checkingGigs && <Loading />}
119+
{!checkingGigs &&
120+
!loadingMyGigs &&
121+
currentGigs.myGigs &&
122+
currentGigs.myGigs.length == 0 && <Empty gigStatus={gigStatus} />}
123+
{!checkingGigs &&
124+
currentGigs.myGigs &&
125+
currentGigs.myGigs.length > 0 && (
126+
<JobListing
127+
gigStatus={gigStatus}
128+
jobs={currentGigs.myGigs}
129+
loadMore={loadMore}
130+
total={currentGigs.total}
131+
numLoaded={currentGigs.numLoaded}
132+
/>
133+
)}
134+
{checkingGigs || (loadingMyGigs && !currentGigs.myGigs && <Loading />)}
115135
</div>
116136
<Modal open={openUpdateProfile}>
117137
<UpdateGigProfile
@@ -150,6 +170,11 @@ MyGigs.propTypes = {
150170
getAllCountries: PT.func,
151171
checkingGigs: PT.bool,
152172
startCheckingGigs: PT.func,
173+
myActiveGigs: PT.shape(),
174+
myOpenGigs: PT.shape(),
175+
myCompletedGigs: PT.shape(),
176+
myArchivedGigs: PT.shape(),
177+
loadingMyGigs: PT.bool,
153178
};
154179

155180
const mapStateToProps = (state) => ({
@@ -158,6 +183,11 @@ const mapStateToProps = (state) => ({
158183
myGigs: state.myGigs.myGigs,
159184
total: state.myGigs.total,
160185
numLoaded: state.myGigs.numLoaded,
186+
loadingMyGigs: state.myGigs.loadingMyGigs,
187+
myActiveGigs: state.myGigs[constants.GIGS_FILTER_STATUSES.ACTIVE_JOBS],
188+
myOpenGigs: state.myGigs[constants.GIGS_FILTER_STATUSES.OPEN_JOBS],
189+
myCompletedGigs: state.myGigs[constants.GIGS_FILTER_STATUSES.COMPLETED_JOBS],
190+
myArchivedGigs: state.myGigs[constants.GIGS_FILTER_STATUSES.ARCHIVED_JOBS],
161191
profile: state.myGigs.profile,
162192
updateProfileSuccess: state.myGigs.updatingProfileSucess,
163193
});

0 commit comments

Comments
 (0)