clean up musician filter codes

This commit is contained in:
Nuwan 2022-10-26 12:46:27 +05:30
parent 5c0ed0d773
commit 957c5dc3e9
2 changed files with 5 additions and 39 deletions

View File

@ -24,8 +24,6 @@ function JKPeopleFilter() {
const [genres, setGenres] = useState([]); const [genres, setGenres] = useState([]);
const dispatch = useDispatch(); const dispatch = useDispatch();
//const currentPage = useRef(0);
//const nextPage = useRef(0);
const page = useRef(0); const page = useRef(0);
const params = useRef({}); const params = useRef({});
@ -143,20 +141,12 @@ function JKPeopleFilter() {
const submitForm = event => { const submitForm = event => {
event.preventDefault(); event.preventDefault();
//currentPage.current = 0;
//nextPage.current = 0;
page.current = 0; page.current = 0;
dispatch(resetState()); dispatch(resetState());
handleSubmit(onSubmit)(); handleSubmit(onSubmit)();
setShow(false); setShow(false);
}; };
const isBeforeSecondPageLoad = () => {
//return currentPage.current === 0 && nextPage.current === 1
return page.current === 0
}
const onSubmit = (data) => { const onSubmit = (data) => {
let genres = []; let genres = [];
let joined_within_days = ''; let joined_within_days = '';
@ -175,11 +165,7 @@ function JKPeopleFilter() {
params.current = { ...data, genres, joined_within_days, active_within_days }; params.current = { ...data, genres, joined_within_days, active_within_days };
try { try {
//currentPage.current = nextPage.current
//if(currentPage.current === 0 && nextPage.current === 0){
if(page.current === 0){ if(page.current === 0){
//dispatch(fetchPeople({ data: params.current, page: nextPage.current, limit: perPageLimit }));
dispatch(fetchPeople({ data: params.current, page: page.current, limit: perPageLimit })); dispatch(fetchPeople({ data: params.current, page: page.current, limit: perPageLimit }));
page.current += 1 page.current += 1
}else{ }else{
@ -188,7 +174,6 @@ function JKPeopleFilter() {
} }
if(hasOffset){ if(hasOffset){
dispatch(preFetchPeople({ data: params.current, page: page.current, limit: perPageLimit })); dispatch(preFetchPeople({ data: params.current, page: page.current, limit: perPageLimit }));
//nextPage.current = nextPage.current + 1;
page.current += 1; page.current += 1;
} }
} }
@ -203,16 +188,11 @@ function JKPeopleFilter() {
if(people.length === 0 && page.current === 1 && !getValues("from_location")){ if(people.length === 0 && page.current === 1 && !getValues("from_location")){
//no results found. let's fetch again with from_location enabled //no results found. let's fetch again with from_location enabled
setValue('from_location', true); setValue('from_location', true);
//nextPage.current = 0
//currentPage.current = 0
page.current = 0 page.current = 0
submitPageQuery() submitPageQuery()
}else{ }else{
//nextPage.current = currentPage.current + 1
//if (isBeforeSecondPageLoad() && hasOffset) {
if (page.current === 1 && hasOffset) { if (page.current === 1 && hasOffset) {
dispatch(preFetchPeople({ data: params.current, page: page.current, limit: perPageLimit })); dispatch(preFetchPeople({ data: params.current, page: page.current, limit: perPageLimit }));
//nextPage.current = nextPage.current + 1;
page.current += 1; page.current += 1;
} }
} }
@ -223,7 +203,6 @@ function JKPeopleFilter() {
useEffect(() => { useEffect(() => {
fetchGenres(); fetchGenres();
fetchInstruments(); fetchInstruments();
//currentPage.current = 0
submitPageQuery() submitPageQuery()
}, []); }, []);
@ -232,8 +211,6 @@ function JKPeopleFilter() {
if (resetFilter) { if (resetFilter) {
clearFilterOpts(); clearFilterOpts();
dispatch(resetState()); dispatch(resetState());
//currentPage.current = 0;
//nextPage.current = 0;
page.current = 0; page.current = 0;
handleSubmit(onSubmit)(); handleSubmit(onSubmit)();
setResetFilter(false); setResetFilter(false);
@ -456,7 +433,7 @@ function JKPeopleFilter() {
people={people} people={people}
goNextPage={goNextPage} goNextPage={goNextPage}
hasNext={showLoadMore} hasNext={showLoadMore}
isLoading={loadingStatus === 'loading' && people.length !== 0} isLoading={loadingStatus === 'loading'}
/> />
</div> </div>
</Row> </Row>

View File

@ -6,7 +6,6 @@ const initialState = {
prefetched: [], prefetched: [],
status: 'idel', status: 'idel',
error: null, error: null,
//totalPages: 0,
hasOffset: false hasOffset: false
} }
@ -68,11 +67,7 @@ export const peopleSlice = createSlice({
}, },
loadPrefetched: (state, action) => { loadPrefetched: (state, action) => {
if(state.prefetched.length > 0){ if(state.prefetched.length > 0){
//const records = new Set([...state.people, ...state.prefetched]);
const records = [...state.people, ...state.prefetched]; const records = [...state.people, ...state.prefetched];
//const unique = [];
//records.map(x => unique.filter(a => a.id === x.id).length > 0 ? null : unique.push(x))
//state.people = unique
state.people = records; state.people = records;
} }
state.prefetched = [] state.prefetched = []
@ -84,13 +79,8 @@ export const peopleSlice = createSlice({
state.status = 'loading' state.status = 'loading'
}) })
.addCase(fetchPeople.fulfilled, (state, action) => { .addCase(fetchPeople.fulfilled, (state, action) => {
//const records = new Set([...state.people, ...action.payload.musicians]);
const records = [...state.people, ...action.payload.musicians]; const records = [...state.people, ...action.payload.musicians];
//const unique = [];
//records.map(x => unique.filter(a => a.id === x.id).length > 0 ? null : unique.push(x))
//state.people = unique
state.people = records state.people = records
//state.totalPages = action.payload.page_count
state.hasOffset = !!action.payload.offset state.hasOffset = !!action.payload.offset
state.status = 'succeeded' state.status = 'succeeded'
console.log('******fetchPeople.fulfilled -> state.people', state.people); console.log('******fetchPeople.fulfilled -> state.people', state.people);
@ -100,19 +90,18 @@ export const peopleSlice = createSlice({
state.error = action.error.message state.error = action.error.message
}) })
.addCase(preFetchPeople.pending, (state, action) => { .addCase(preFetchPeople.pending, (state, action) => {
state.status = 'loading'
}) })
.addCase(preFetchPeople.fulfilled, (state, action) => { .addCase(preFetchPeople.fulfilled, (state, action) => {
//const records = new Set([...state.prefetched, ...action.payload.musicians]);
const records = [...state.prefetched, ...action.payload.musicians]; const records = [...state.prefetched, ...action.payload.musicians];
//const unique = [];
//records.map(x => unique.filter(a => a.id === x.id).length > 0 ? null : unique.push(x))
//state.prefetched = unique
state.prefetched = records state.prefetched = records
//state.totalPages = action.payload.page_count
state.hasOffset = !!action.payload.offset state.hasOffset = !!action.payload.offset
state.status = 'succeeded'
console.log('******preFetchPeople.fulfilled -> state.prefetched', state.prefetched); console.log('******preFetchPeople.fulfilled -> state.prefetched', state.prefetched);
}) })
.addCase(preFetchPeople.rejected, (state, action) => { .addCase(preFetchPeople.rejected, (state, action) => {
state.error = action.error.message
state.status = 'failed'
}) })
.addCase(acceptFriendRequest.fulfilled, (state, action) => { .addCase(acceptFriendRequest.fulfilled, (state, action) => {
}) })