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

View File

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